Comments (6)
@LeaFrock Try something like this:
private static readonly JsonSerializerOptions s_ThingOptions = new()
{
Converters = { new JsonMicrosoftDateTimeWithOffsetConverter() }
};
public Thing ParseThingFromJson(string json)
{
return JsonSerializer.Deserialize<Thing>(json, s_ThingOptions);
}
public sealed class JsonMicrosoftDateTimeWithOffsetConverter : JsonConverterFactory
{
private readonly JsonMicrosoftDateTimeOffsetConverter _DateTimeOffsetFactory = new();
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(DateTime);
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
return new Converter(
(JsonConverter<DateTimeOffset>)_DateTimeOffsetFactory.CreateConverter(typeof(DateTimeOffset), options));
}
private class Converter : JsonConverter<DateTime>
{
private readonly JsonConverter<DateTimeOffset> _DateTimeOffsetConverter;
public Converter(JsonConverter<DateTimeOffset> dateTimeOffsetConverter)
{
_DateTimeOffsetConverter = dateTimeOffsetConverter;
}
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
DateTimeOffset dateTimeOffset = _DateTimeOffsetConverter.Read(ref reader, typeToConvert, options);
return dateTimeOffset.LocalDateTime;
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
=> _DateTimeOffsetConverter.Write(writer, value, options);
}
}
What that will do is round-trip your values using local time.
If you read a value from the DB like \/Date(1580803200000-0500)\/
on a system in the pacific timezone and then immediately wrote it back, it would be changed to \/Date(1580803200000-0800)\/
. The reason for that is DateTime
can't store the offset. It either has to be UTC or Local.
If all your systems are in the same time zone, that should work fine. If your system are not, I recommend switching your ORM to DateTimeOffset
because that is what it is for 😄 Sounds like the current design might be bugged.
from core.
@Malincoln9 Are you able to switch the target type to DateTimeOffset
? The JsonMicrosoftDateTimeConverter
doesn't understand the time offset in your JSON ("-0600") but JsonMicrosoftDateTimeOffsetConverter
can handle that.
Example from the tests:
from core.
I was able to make that work. Would have liked to keep it DateTime, but thanks.
from core.
Same issue, but I cannot switch the target type to DateTimeOffset
. The situation is that, the DateTime
part of our some database entities is stored like /Date(...+..)/
in redis, but the class
is defined by ORM as DateTime
instead of DateTimeOffset
(DBFirst). I have to be compatible with some old apps using redis too, and not affect ORM in order to avoid other problems.
Any workaround?
from core.
Spoke offline with @CodeBlanch who is going to create a PR against this issue.
After looking at the DataContractJson docs, we see that "/Date(700000+0500)/" is actually a DateTime format and not DateTimeOffset format. So JsonMicrosoftDateTimeConverter should be handling this case. If there is any +/- offset it's deserialized to local time; if not, it's deserialized to UTC
"The part that consists of "+0500" in the example is optional and indicates that the time is of the Local kind - that is, should be converted to the local time zone on deserialization. If it is absent, the time is deserialized as Utc. The actual number ("0500" in this example) and its sign (+ or -) are ignored."
from core.
3.0.0-beta2 has been published which updates JsonMicrosoftDateTimeConverter
to follow the spec linked above.
from core.
Related Issues (20)
- Regex for JsonMicrosoftDateTimeConverter seems to be wrong HOT 2
- DateTime format doesn't match .Net Framework's JavaScriptSerializer or Json.Net's DateFormatHandling.MicrosoftDateFormat HOT 1
- Add option for ignoring invalid enum string values HOT 5
- JsonStringEnumMemberConverter should use JsonPropertyName if present
- Add System.Text.Json TypeConverter to JsonConverter adapter HOT 1
- Aliases for Enum deserialization HOT 2
- Macross.Json.Extensions 2.1.0 Nuget HOT 4
- Security Issue in Macross.Json.Extensions
- Unable to have spaces in JsonPropertyName HOT 3
- JsonStringEnumMemberConverter: Enums cannot be used as dictionary keys HOT 1
- JsonTimeSpanConverter used to handle partial formats. HOT 1
- Unable to deserialize enum from [FromQuery] with (or without) JsonStringEnumMemberConverter HOT 3
- JsonStringEnumMemberConverterOptionsAttribute & custom naming policy is not working together
- suggestion: backward compatibility to deserialize same enum type option to itself
- not working deserialazer in controller HOT 3
- Case sensitive enum deserialize
- JsonStringEnumMemberConverter must be defined on each property and does not work with arrays of enums HOT 1
- In v2.0 release, vulnerability has been found in the referenced System.Text.Encodings.Web
- JsonStringEnumMemberConverter can not NativeAOT
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.