-
Notifications
You must be signed in to change notification settings - Fork 391
Enum based json serialization #990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If you think it is still actually needed, that is. |
Sure, I think it would be useful to have. But now that you remind me of the enum schema, I just had a thought. Maybe this should all be refactored to:
Schema: {
"Value" : 1.2,
"UnitAbbreviation": "mg", // Either this..
"Unit": "Milligram" // ..or this, not both.
"Quantity": "Mass" // Renamed from Type.
} What do you think? |
Also, supporting IComparable could be yet another option, instead of having a separate serializer for it. I just think it is simpler from the consumer perspective to have a single converter with options than multiple special purpose converters. |
In my previous implementation I had it setup as an abstract converter and specific implementations for the different schemas (this is probably what I would have proposed for Pros:
Cons:
I think it would be better if we picked up a stable name for the
I prefer |
Do we need a common interface though? I'm not sure I see the benefit, but I see drawbacks with it. Let's say we arrive at 3 strategies, just an example:
Let's say we implement implement all 3 in newtonsoft, the last 2 in system.text.json and only enum based for protobuf. It would then probably throw an NotImplementedException in some cases, which I don't like. Also, let's imagine with protobuf, there is a fourth strategy that only makes sense for binary formats or maybe even solely for protobuf. I can't think of an example, but in this case the enum would have to be extended with a protobuf-only strategy. It feels weird. I think it's better that each converter offers its own options based on what it actually can do, and we name similar schemes the same so you can recognize them across converters. So compatible JSON schemas would have similarly named options. Something like this (ignore the dummy schemas I implemented and ignore the converter names): void Main()
{
var person = new { Weight = Mass.FromKilograms(80) };
Newtonsoft.Json.JsonConvert.SerializeObject(person, new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
Converters =
{
new NewtonsoftUnitsNetJsonConverter(new NewtonsoftUnitsNetJsonConverterOptions { Schema = NewtonsoftUnitsNetJsonSchema.Enum })
}
}).Dump("newtonsoft");
System.Text.Json.JsonSerializer.Serialize(person, new JsonSerializerOptions
{
WriteIndented = true,
Converters =
{
new SystemTextUnitsNetJsonConverter(new SystemTextUnitsNetJsonConverterOptions { Schema = SystemTextUnitsNetJsonSchema.Enum })
}
})
.Dump("system.text.json");
}
public enum NewtonsoftUnitsNetJsonSchema { Legacy, Enum, Abbreviation }
public enum SystemTextUnitsNetJsonSchema { Legacy, Enum, Abbreviation }
public class NewtonsoftUnitsNetJsonConverterOptions
{
public NewtonsoftUnitsNetJsonSchema Schema { get; set; } = NewtonsoftUnitsNetJsonSchema.Enum;
}
public class SystemTextUnitsNetJsonConverterOptions
{
public SystemTextUnitsNetJsonSchema Schema { get; set;} = SystemTextUnitsNetJsonSchema.Enum;
}
public class NewtonsoftUnitsNetJsonConverter : Newtonsoft.Json.JsonConverter
{
public NewtonsoftUnitsNetJsonConverter(NewtonsoftUnitsNetJsonConverterOptions options) { }
public override bool CanConvert(Type objectType) { return objectType == typeof(Mass); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
return Mass.Parse(reader.ReadAsString(), CultureInfo.InvariantCulture);
}
public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
var quantity = (Mass)value;
writer.WriteValue(quantity.ToString(CultureInfo.InvariantCulture));
}
}
public class SystemTextUnitsNetJsonConverter : System.Text.Json.Serialization.JsonConverter<Mass>
{
public SystemTextUnitsNetJsonConverter(SystemTextUnitsNetJsonConverterOptions options) { }
public override Mass Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) => Mass.Parse(reader.GetString(), CultureInfo.InvariantCulture);
public override void Write(
Utf8JsonWriter writer,
Mass quantity,
JsonSerializerOptions options) => writer.WriteStringValue(quantity.ToString(CultureInfo.InvariantCulture));
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Now that #985 is out- lets discuss the enum based schema:
a) Similar to the
AbbreviatedUnitsConverter
:{"Value" : 1.2, "Unit": "Milligram", "Type": "Mass"}
b) Similar to the
UnitsNetIQuantityJsonConverter
:{"Value" : 1.2, "Unit": "MassUnit.Milligram"}
The text was updated successfully, but these errors were encountered: