Browse Source

Use CultureInfo.CurrentUICulture of DateTime JSON converter.

pull/6196/head
maliming 6 years ago
parent
commit
71bfe2af8d
  1. 10
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs
  2. 2
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverter.cs
  3. 8
      framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs
  4. 20
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs
  5. 2
      framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/JsonConverters/EntityJsonConverter.cs

10
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs

@ -26,15 +26,15 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
if (reader.TokenType == JsonTokenType.String)
{
var s = reader.GetString();
if (DateTime.TryParseExact(s, _options.DefaultDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var d1))
if (DateTime.TryParseExact(s, _options.DefaultDateTimeFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d1))
{
return _clock.Normalize(d1);
}
throw new JsonException($"{s} cannot parse to DateTime({_options.DefaultDateTimeFormat})!");
throw new JsonException($"'{s}' can't parse to DateTime({_options.DefaultDateTimeFormat})!");
}
throw new JsonException("reader TokenType is not String!");
throw new JsonException("Reader's TokenType is not String!");
}
if (reader.TryGetDateTime(out var d2))
@ -42,7 +42,7 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
return _clock.Normalize(d2);
}
throw new JsonException("reader can't get datetime!");
throw new JsonException("Can't get datetime from the reader!");
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
@ -53,7 +53,7 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
}
else
{
writer.WriteStringValue(_clock.Normalize(value).ToString(_options.DefaultDateTimeFormat));
writer.WriteStringValue(_clock.Normalize(value).ToString(_options.DefaultDateTimeFormat, CultureInfo.CurrentUICulture));
}
}
}

2
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverter.cs

@ -30,7 +30,7 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
return extensibleObject;
}
throw new JsonException("RootElement ValueKind is not Object!");
throw new JsonException("RootElement's ValueKind is not Object!");
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)

8
framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs

@ -26,15 +26,15 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
if (reader.TokenType == JsonTokenType.String)
{
var s = reader.GetString();
if (DateTime.TryParseExact(s, _options.DefaultDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var d1))
if (DateTime.TryParseExact(s, _options.DefaultDateTimeFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d1))
{
return _clock.Normalize(d1);
}
throw new JsonException($"{s} cannot parse to DateTime({_options.DefaultDateTimeFormat})!");
throw new JsonException($"'{s}' can't parse to DateTime({_options.DefaultDateTimeFormat})!");
}
throw new JsonException("reader TokenType is not String!");
throw new JsonException("Reader's TokenType is not String!");
}
if (reader.TryGetDateTime(out var d2))
@ -59,7 +59,7 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters
}
else
{
writer.WriteStringValue(_clock.Normalize(value.Value).ToString(_options.DefaultDateTimeFormat));
writer.WriteStringValue(_clock.Normalize(value.Value).ToString(_options.DefaultDateTimeFormat, CultureInfo.CurrentUICulture));
}
}
}

20
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs

@ -152,7 +152,7 @@ namespace Volo.Abp.Json
}
[Fact]
public void Serialize_Deserialize_ExtensibleObject_With_ExtraProperties_String()
public void Serialize_Deserialize_ExtensibleObject_Without_String()
{
var json = "{\"name\":\"test\"}";
var extensibleObject = JsonSerializer.Deserialize<TestExtensibleObjectClass>(json);
@ -160,6 +160,24 @@ namespace Volo.Abp.Json
extensibleObject.ExtraProperties.ShouldBeEmpty();
}
[Fact]
public void Serialize_Deserialize_ExtensibleObject_Without_Empty()
{
var json = "{\"name\":\"test\",\"extraProperties\":{}}";
var extensibleObject = JsonSerializer.Deserialize<TestExtensibleObjectClass>(json);
extensibleObject.ExtraProperties.ShouldNotBeNull();
extensibleObject.ExtraProperties.ShouldBeEmpty();
}
[Fact]
public void Serialize_Deserialize_ExtensibleObject_Without_Null()
{
var json = "{\"name\":\"test\",\"extraProperties\":null}";
var extensibleObject = JsonSerializer.Deserialize<TestExtensibleObjectClass>(json);
extensibleObject.ExtraProperties.ShouldNotBeNull();
extensibleObject.ExtraProperties.ShouldBeEmpty();
}
[Fact]
public void Serialize_Deserialize_With_Datetime()
{

2
framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/JsonConverters/EntityJsonConverter.cs

@ -27,7 +27,7 @@ namespace Volo.Abp.MemoryDb.JsonConverters
return entity;
}
throw new JsonException("RootElement ValueKind is not Object!");
throw new JsonException("RootElement's ValueKind is not Object!");
}
public override void Write(Utf8JsonWriter writer, TEntity value, JsonSerializerOptions options)

Loading…
Cancel
Save