Support for System.Text.Json?
Hi,
Over at EngineeringUnits we are trying create support for System.Text.Json but when I looking into it, it turns out that its because Fractions does not have support for it:
Fraction f = Fraction.FromDecimal(1);
// Using System.Text.Json
string f_Json_DotNet = System.Text.Json.JsonSerializer.Serialize(f);
Fraction f_DotNet = System.Text.Json.JsonSerializer.Deserialize<Fraction>(f_Json_DotNet)!;
//Shows as 0
// Using Newtonsoft.Json
string f_Json_Newtonsoft = Newtonsoft.Json.JsonConvert.SerializeObject(f);
Fraction f_Newtonsoft = Newtonsoft.Json.JsonConvert.DeserializeObject<Fraction>(f_Json_Newtonsoft)!;
//Shows as 1
Is it something that you have looked into before or have plans on doing? I might have a look into your code to see if there is an easy fix :-)
Hello @MadsKirkFoged
Sorry for the late reply. Similar to the still open pull requests from @lipchev (sorry!), I don't have much time for my GitHub projects at the moment 🥹. It's a misery.
Serialization is a difficult topic. My first question would be, what do you want to achieve?
Should the unit be transmitted in a human-readable format? Is the result evaluated by a (non dotnet) language and should therefore be transferred in a structured way, as a complex data type? Is it about performance - should the components perhaps even be stored in a (document-) database?
To give a few examples, the company I work for prefers human-readable formats - at least when using web technologies and/or JSON serialization.
So instead of transfering something like this:
{
"Value" : {
"Numerator" : "2",
"Denominator" : "4",
"NormalizationNotApplied" : true
},
"Unit" : {
"Numerator" : "km",
"Denominator" : "h",
}
}
we would prefer "2/4 km/h" or "1/2 km/h" or even "0.5 km/h".
What do you need?
I have a feature branch that comes with two different json converters:
See this branch.
The desired converter can either be registered globally (not very user-friendly) or via the
[JsonConverter(typeof(StructuralFractionJsonConverter))]
attribute at the property containing the fraction value.
I'm really keen on having (at least an example of) a nuget containing a protobuf definition (shared types or whatever it was called). Since there wouldn't be any humans involved, the protos should be straightforward, but I'm not sure how to "package" the whole thing...
The two serializers will be available beginning with version 8.1.0 when using NET8 or greater.