MadsKirkFoged / EngineeringUnits

Working with units made easy with automatic unit-check and converting between units
MIT License
41 stars 10 forks source link

DotNet Serialization Not Restoring Original Value, Unlike Newtonsoft #62

Open CPMOliveira opened 1 week ago

CPMOliveira commented 1 week ago

Description: I encountered an issue when serializing and deserializing objects using the System.Text.Json. The value is not restored to its original state upon deserialization, unlike when using Newtonsoft.Json, which works as expected. This is critical when dealing with specific value types.

Example:

Length L = new(1d, LengthUnit.Meter);

// Using System.Text.Json
string L_Json_DotNet = System.Text.Json.JsonSerializer.Serialize(L);
Length L_Unit_DotNet = System.Text.Json.JsonSerializer.Deserialize<Length>(L_Json_DotNet)!;
Length.Equals(L, L_Unit_DotNet); // False

// Using Newtonsoft.Json
string L_Json_Newtonsoft = Newtonsoft.Json.JsonConvert.SerializeObject(L);
Length L_Unit_Newtonsoft = Newtonsoft.Json.JsonConvert.DeserializeObject<Length>(L_Json_Newtonsoft)!;
Length.Equals(L, L_Unit_Newtonsoft); // True

Expected Behavior: System.Text.Json serialization should restore the original object value, just like Newtonsoft.Json.

Environment: EngineeringUnits Version: 2.2.98 .NET Version: 9.0 Preview 7

MadsKirkFoged commented 1 week ago

Hi CPMOliveira, Thanks for reaching out!

You are right that we are yet to create support for System.Text.Json Its on the backlog and now that we actually got a request for it, I will move it up the list.

If you got the skills and time you are also welcome to try if you can implement it yourself?

MadsKirkFoged commented 1 week ago

It is actually Fractions that does not have support for System.Text.Json which it a system we use for conversions between units.