Skip to content

Commit

Permalink
Use invariant culture for (de)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinM85 committed Jul 16, 2024
1 parent 7c61913 commit dd7b82c
Show file tree
Hide file tree
Showing 18 changed files with 1,602 additions and 53 deletions.
26 changes: 25 additions & 1 deletion src/abstractions/Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
// ------------------------------------------------------------------------------

using System;
using System.Security;

namespace Microsoft.Kiota.Abstractions
{
/// <summary>
/// Model to represent only the date component of a DateTime
/// </summary>
public struct Time
public struct Time:IEquatable<Time>
{
#if NET6_0_OR_GREATER
/// <summary>
Expand All @@ -26,6 +27,29 @@ public struct Time
/// <returns>A new <see cref="TimeOnly"/> structure whose hours, minutes, seconds and milliseconds are equal to those of the supplied time.</returns>
public static implicit operator TimeOnly(Time time) => new(time.DateTime.Hour, time.DateTime.Minute, time.DateTime.Second, time.DateTime.Millisecond);
#endif
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the other parameter; otherwise, false.</returns>
public bool Equals(Time other) => (Hour, Minute, Second) == (other.Hour, other.Minute, other.Second);

/// <inheritdoc />
public override bool Equals(object? o) => (o is Time other) && Equals(other);

/// <inheritdoc />
public override int GetHashCode()
{
#if NET6_0_OR_GREATER
return HashCode.Combine(Hour, Minute, Second);
#else
int hash = 17;
hash = hash * 23 + Hour.GetHashCode();
hash = hash * 23 + Minute.GetHashCode();
hash = hash * 23 + Second.GetHashCode();
return hash;
#endif
}
/// <summary>
/// Create a new Time from hours, minutes, and seconds.
/// </summary>
Expand Down
19 changes: 10 additions & 9 deletions src/serialization/form/FormParseNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Xml;
using Microsoft.Kiota.Abstractions;
Expand Down Expand Up @@ -143,21 +144,21 @@ public IEnumerable<T> GetCollectionOfPrimitiveValues<T>()
}
}
/// <inheritdoc/>
public DateTimeOffset? GetDateTimeOffsetValue() => DateTimeOffset.TryParse(DecodedValue, out var result) ? result : null;
public DateTimeOffset? GetDateTimeOffsetValue() => DateTimeOffset.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? result : null;
/// <inheritdoc/>
public Date? GetDateValue() => DateTime.TryParse(DecodedValue, out var result) ? new Date(result) : null;
public Date? GetDateValue() => DateTime.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? new Date(result) : null;
/// <inheritdoc/>
public decimal? GetDecimalValue() => decimal.TryParse(DecodedValue, out var result) ? result : null;
public decimal? GetDecimalValue() => decimal.TryParse(DecodedValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc/>
public double? GetDoubleValue() => double.TryParse(DecodedValue, out var result) ? result : null;
public double? GetDoubleValue() => double.TryParse(DecodedValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc/>
public float? GetFloatValue() => float.TryParse(DecodedValue, out var result) ? result : null;
public float? GetFloatValue() => float.TryParse(DecodedValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc/>
public Guid? GetGuidValue() => Guid.TryParse(DecodedValue, out var result) ? result : null;
/// <inheritdoc/>
public int? GetIntValue() => int.TryParse(DecodedValue, out var result) ? result : null;
public int? GetIntValue() => int.TryParse(DecodedValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc/>
public long? GetLongValue() => long.TryParse(DecodedValue, out var result) ? result : null;
public long? GetLongValue() => long.TryParse(DecodedValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc/>
public T GetObjectValue<T>(ParsableFactory<T> factory) where T : IParsable
{
Expand Down Expand Up @@ -205,7 +206,7 @@ private void AssignFieldValues<T>(T item) where T : IParsable
}

/// <inheritdoc/>
public sbyte? GetSbyteValue() => sbyte.TryParse(DecodedValue, out var result) ? result : null;
public sbyte? GetSbyteValue() => sbyte.TryParse(DecodedValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;

/// <inheritdoc/>
public string GetStringValue() => DecodedValue;
Expand All @@ -222,7 +223,7 @@ private void AssignFieldValues<T>(T item) where T : IParsable
}

/// <inheritdoc/>
public Time? GetTimeValue() => DateTime.TryParse(DecodedValue, out var result) ? new Time(result) : null;
public Time? GetTimeValue() => DateTime.TryParse(DecodedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? new Time(result) : null;

#if NET5_0_OR_GREATER
IEnumerable<T?> IParseNode.GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>()
Expand Down
3 changes: 1 addition & 2 deletions src/serialization/form/FormSerializationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ private void WriteAnyValue(string? key, object value)
default:
WriteStringValue(key, value.ToString());// works for Date and String types
break;

}
}

Expand Down Expand Up @@ -134,7 +133,7 @@ public void WriteCollectionOfPrimitiveValues<T>(string? key, IEnumerable<T>? val
public void WriteDateTimeOffsetValue(string? key, DateTimeOffset? value)
{
if(value.HasValue)
WriteStringValue(key, value.Value.ToString("o"));
WriteStringValue(key, value.Value.ToString("o", CultureInfo.InvariantCulture));
}
/// <inheritdoc/>
public void WriteDateValue(string? key, Date? value)
Expand Down
7 changes: 4 additions & 3 deletions src/serialization/json/JsonParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.Json;
Expand Down Expand Up @@ -155,7 +156,7 @@ public JsonParseNode(JsonElement node, KiotaJsonSerializationContext jsonSeriali
if(string.IsNullOrEmpty(dateTimeOffsetStr))
return null;

if(DateTimeOffset.TryParse(dateTimeOffsetStr, out dateTimeOffset))
if(DateTimeOffset.TryParse(dateTimeOffsetStr, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeOffset))
return dateTimeOffset;

return _jsonNode.Deserialize(_jsonSerializerContext.DateTimeOffset);
Expand Down Expand Up @@ -185,7 +186,7 @@ public JsonParseNode(JsonElement node, KiotaJsonSerializationContext jsonSeriali
if(string.IsNullOrEmpty(dateString))
return null;

if(DateTime.TryParse(dateString, out var result))
if(DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
return new Date(result);

return _jsonNode.Deserialize(_jsonSerializerContext.Date);
Expand All @@ -201,7 +202,7 @@ public JsonParseNode(JsonElement node, KiotaJsonSerializationContext jsonSeriali
if(string.IsNullOrEmpty(dateString))
return null;

if(DateTime.TryParse(dateString, out var result))
if(DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
return new Time(result);

return _jsonNode.Deserialize(_jsonSerializerContext.Time);
Expand Down
19 changes: 10 additions & 9 deletions src/serialization/text/TextParseNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Helpers;
Expand Down Expand Up @@ -45,31 +46,31 @@ public TextParseNode(string? text)
public IEnumerable<T> GetCollectionOfPrimitiveValues<T>() => throw new InvalidOperationException(NoStructuredDataMessage);
#endif
/// <inheritdoc />
public DateTimeOffset? GetDateTimeOffsetValue() => DateTimeOffset.TryParse(Text, out var result) ? result : null;
public DateTimeOffset? GetDateTimeOffsetValue() => DateTimeOffset.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? result : null;
/// <inheritdoc />
public Date? GetDateValue() => DateTime.TryParse(Text, out var result) ? new Date(result) : null;
public Date? GetDateValue() => DateTime.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? new Date(result) : null;
/// <inheritdoc />
public decimal? GetDecimalValue() => decimal.TryParse(Text, out var result) ? result : null;
public decimal? GetDecimalValue() => decimal.TryParse(Text, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc />
public double? GetDoubleValue() => double.TryParse(Text, out var result) ? result : null;
public double? GetDoubleValue() => double.TryParse(Text, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc />
public float? GetFloatValue() => float.TryParse(Text, out var result) ? result : null;
public float? GetFloatValue() => float.TryParse(Text, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc />
public Guid? GetGuidValue() => Guid.TryParse(Text, out var result) ? result : null;
/// <inheritdoc />
public int? GetIntValue() => int.TryParse(Text, out var result) ? result : null;
public int? GetIntValue() => int.TryParse(Text, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc />
public long? GetLongValue() => long.TryParse(Text, out var result) ? result : null;
public long? GetLongValue() => long.TryParse(Text, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc />
public T GetObjectValue<T>(ParsableFactory<T> factory) where T : IParsable => throw new InvalidOperationException(NoStructuredDataMessage);
/// <inheritdoc />
public sbyte? GetSbyteValue() => sbyte.TryParse(Text, out var result) ? result : null;
public sbyte? GetSbyteValue() => sbyte.TryParse(Text, NumberStyles.Number, CultureInfo.InvariantCulture, out var result) ? result : null;
/// <inheritdoc />
public string? GetStringValue() => Text;
/// <inheritdoc />
public TimeSpan? GetTimeSpanValue() => string.IsNullOrEmpty(Text) ? null : XmlConvert.ToTimeSpan(Text);
/// <inheritdoc />
public Time? GetTimeValue() => DateTime.TryParse(Text, out var result) ? new Time(result) : null;
public Time? GetTimeValue() => DateTime.TryParse(Text, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result) ? new Time(result) : null;
/// <inheritdoc />
#if NET5_0_OR_GREATER
public IEnumerable<T?> GetCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() where T : struct, Enum
Expand Down
17 changes: 9 additions & 8 deletions src/serialization/text/TextSerializationWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Xml;
using Microsoft.Kiota.Abstractions;
Expand Down Expand Up @@ -59,33 +60,33 @@ public Stream GetSerializedContent()
/// <inheritdoc />
public void WriteByteArrayValue(string? key, byte[]? value) => WriteStringValue(key, value?.Length > 0 ? Convert.ToBase64String(value) : string.Empty);
/// <inheritdoc />
public void WriteByteValue(string? key, byte? value) => WriteStringValue(key, value?.ToString());
public void WriteByteValue(string? key, byte? value) => WriteStringValue(key, value?.ToString(CultureInfo.InvariantCulture));
/// <inheritdoc />
public void WriteCollectionOfObjectValues<T>(string? key, IEnumerable<T>? values) where T : IParsable => throw new InvalidOperationException(TextParseNode.NoStructuredDataMessage);
/// <inheritdoc />
public void WriteCollectionOfPrimitiveValues<T>(string? key, IEnumerable<T>? values) => throw new InvalidOperationException(TextParseNode.NoStructuredDataMessage);
/// <inheritdoc />
public void WriteDateTimeOffsetValue(string? key, DateTimeOffset? value) => WriteStringValue(key, value.HasValue ? value.Value.ToString() : null);
public void WriteDateTimeOffsetValue(string? key, DateTimeOffset? value) => WriteStringValue(key, value.HasValue ? value.Value.ToString("o", CultureInfo.InvariantCulture) : null);
/// <inheritdoc />
public void WriteDateValue(string? key, Date? value) => WriteStringValue(key, value?.ToString());
/// <inheritdoc />
public void WriteDecimalValue(string? key, decimal? value) => WriteStringValue(key, value?.ToString());
public void WriteDecimalValue(string? key, decimal? value) => WriteStringValue(key, value?.ToString(CultureInfo.InvariantCulture));
/// <inheritdoc />
public void WriteDoubleValue(string? key, double? value) => WriteStringValue(key, value?.ToString());
public void WriteDoubleValue(string? key, double? value) => WriteStringValue(key, value?.ToString(CultureInfo.InvariantCulture));
/// <inheritdoc />
public void WriteFloatValue(string? key, float? value) => WriteStringValue(key, value?.ToString());
public void WriteFloatValue(string? key, float? value) => WriteStringValue(key, value?.ToString(CultureInfo.InvariantCulture));
/// <inheritdoc />
public void WriteGuidValue(string? key, Guid? value) => WriteStringValue(key, value?.ToString());
/// <inheritdoc />
public void WriteIntValue(string? key, int? value) => WriteStringValue(key, value?.ToString());
public void WriteIntValue(string? key, int? value) => WriteStringValue(key, value?.ToString(CultureInfo.InvariantCulture));
/// <inheritdoc />
public void WriteLongValue(string? key, long? value) => WriteStringValue(key, value?.ToString());
public void WriteLongValue(string? key, long? value) => WriteStringValue(key, value?.ToString(CultureInfo.InvariantCulture));
/// <inheritdoc />
public void WriteNullValue(string? key) => WriteStringValue(key, "null");
/// <inheritdoc />
public void WriteObjectValue<T>(string? key, T? value, params IParsable?[] additionalValuesToMerge) where T : IParsable => throw new InvalidOperationException(TextParseNode.NoStructuredDataMessage);
/// <inheritdoc />
public void WriteSbyteValue(string? key, sbyte? value) => WriteStringValue(key, value?.ToString());
public void WriteSbyteValue(string? key, sbyte? value) => WriteStringValue(key, value?.ToString(CultureInfo.InvariantCulture));
/// <inheritdoc />
public void WriteStringValue(string? key, string? value)
{
Expand Down
Loading

0 comments on commit dd7b82c

Please sign in to comment.