Comments (7)
You haven't shown settings
, but I'm assuming that's the JsonSerializerSettings
that's configured for Noda Time. You're passing that into DeserializeObject
but not into SerializeObject
.
Just change the second line to
var json = JsonConvert.SerializeObject(instantInAClass, settings);
and I'd expect it to be fine.
from nodatime.serialization.
Doh! You're right. This was an admittedly rushed attempt at a repro for a problem I have in my code. I do provide the settings on both deserialize and serialize (checked in the debugger to see the converter is registered) but I can't get the instant to round trip. I'll update with more information in the morning.
from nodatime.serialization.
Okay, we'll leave the issue open for the moment. Good luck reproducing it. (Does it surface in the same way in your real code, or does it deserialize, but lose information?)
from nodatime.serialization.
Ok, this is the error I get, using a sample program written in LINQPad.
void Main()
{
try
{
var jsonSettings = new JsonSerializerSettings();
jsonSettings.Converters.Add(NodaConverters.InstantConverter);
var foo = new InstantTestClass() { Now = SystemClock.Instance.GetCurrentInstant() };
var json = JsonConvert.SerializeObject(foo, jsonSettings);
Console.WriteLine(json);
var newJson = JsonConvert.DeserializeObject<InstantTestClass>(json, jsonSettings);
Console.WriteLine(newJson);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
// Define other methods and classes here
public class InstantTestClass
{
public Instant Now {get;set;}
}
The output is
{"Now":"2018-03-07T15:28:51.0033534Z"}
NodaTime.Utility.InvalidNodaDataException: Unexpected token parsing Instant. Expected String, got Date.
at NodaTime.Serialization.JsonNet.NodaPatternConverter`1.ReadJsonImpl(JsonReader reader, JsonSerializer serializer)
at NodaTime.Serialization.JsonNet.NodaConverterBase`1.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at UserQuery.Main() in C:\Users\<user>\AppData\Local\Temp\LINQPad5\_psvxrofa\query_xyushf.cs:line 44
The exception is slightly different than what my original test generated.
Versions in use:
Json.NET: 11.0.1
NodaTime: 2.2.4
NodaTime.Serialization.JsonNet: 2.0.0
from nodatime.serialization.
The problem there is that you're just adding the InstantConverter without disabling Json.NET's own Date handling.
If you call the ConfigureForNodaTime
extension method instead, it should work - or you can do it yourself here:
jsonSettings.DateParseHandling = DateParseHandling.None;
If that's not the problem you saw in your production code though, we may still be a little way from it...
from nodatime.serialization.
👍 That fixed it! Thanks @jskeet for such a fast response.
from nodatime.serialization.
Phew - glad it wasn't an actual bug :)
from nodatime.serialization.
Related Issues (20)
- Work out what to do about the SystemTextJson DelegatingConverterBase docs
- Extension: Please add simplified time format for Time component converters HOT 3
- System.Text.Json and Custom Formats in Model Binding HOT 15
- Serialization of Nullable<AnnualDate> with System.Text.Json HOT 1
- Add case-insensitive deserialization for JSON HOT 3
- Posting ZonedDateTime Json return Bad Request HOT 8
- NodaTime v3 support HOT 2
- [Question] When are you going to release NodaTime.Serialization.SystemTextJson? HOT 3
- Different formats used in TypeConverters and serialization for Durations HOT 6
- Handling clients without lenient parsers HOT 3
- Add docs pointing to protobuf-net
- "JsonException: The JSON value could not be converted to NodaTime.Instant" NodaTime issue with ASP.NET Core 3.1 Razor Page web application HOT 5
- Add Protobuf support for NodaTime.DateTimeZone HOT 4
- Support for Temporal JS serialization format HOT 8
- Empty String Conversion to Nullable missing when switching from NodaTime.Serialization.JsonNet to NodaTime.Serialization.SystemTextJson HOT 8
- Security issue NodaTime.Serialization.SystemTextJson HOT 3
- HTTP POST - InvalidNodaDataException for null values (NewtonsoftJson) HOT 5
- Add attributes that apply the "standard" JsonConverters HOT 5
- SystemTextJson package updates have not been published to Nuget HOT 3
- System.Text.Json should expose a JsonConverterFactory for defaults HOT 5
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 nodatime.serialization.