Coder Social home page Coder Social logo

Comments (7)

jskeet avatar jskeet commented on June 30, 2024

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.

thegreatco avatar thegreatco commented on June 30, 2024

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.

jskeet avatar jskeet commented on June 30, 2024

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.

thegreatco avatar thegreatco commented on June 30, 2024

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.

jskeet avatar jskeet commented on June 30, 2024

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.

thegreatco avatar thegreatco commented on June 30, 2024

👍 That fixed it! Thanks @jskeet for such a fast response.

from nodatime.serialization.

jskeet avatar jskeet commented on June 30, 2024

Phew - glad it wasn't an actual bug :)

from nodatime.serialization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.