I am in the process of upgrading to ASP.NET Core 3.1, after switching to System.Text.Json noticed strange result variation in floating point values. Upon investigation, I realized System.Text.Json converts a double value to int. For example, consider a simple contract:
public class RowVector
{
[JsonPropertyName("x")]
public double X { get; set; }
[JsonPropertyName("y")]
public double Y { get; set; }
[JsonPropertyName("z")]
public double Z { get; set; }
}
var rowVector = new RowVector { X = 1.0, Y = 213.9, Z = 112.0 };
var serializedData = JsonSerializer.Serialize(rowVector);
Serialized-Data output with System.Text.Json :{"x":1,"y":213.9,"z":112}
Here x and z are int values, whereas in Newtonsoft.Json,
Serialized-Data output with Newtonsoft.Json :{"X":1.0,"Y":213.9,"Z":112.0}
Here x and z are retained as double.
So with system.text.json, upon deserialization these values are int and this leads to variation in our processing calculation, any idea why this is implemented in such a way in System.Text.Json?
RowVector
class. This is not where your issue is. – Restraineddecimal
– Omnibus