-
-
Save AlexZeitler/b012f1de4f8b3939c7c598f7ac419889 to your computer and use it in GitHub Desktop.
JSON.NET SCU Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Single case union | |
[<Struct>] type MHPI = MHPI of double | |
// An entity that contains SCU | |
type Entity = { | |
Foo: string | |
Bar: MHPI | |
} | |
// JSON.NET Converter | |
type MhpiConverter() = | |
inherit JsonConverter() | |
override x.CanConvert(t) = | |
t.ReflectedType = typedefof<MHPI> | |
override x.WriteJson(writer, value, serializer) = | |
let value = | |
let mhpi = value :?> MHPI | |
let (MHPI value) = mhpi | |
value | |
serializer.Serialize(writer, value) | |
override x.ReadJson(reader, t, existingValue, serializer) = | |
raise (NotImplementedException()) | |
// Serialize | |
let entity = { Foo = "foo"; Bar = MHPI 1.0 } | |
let json = JsonConvert.SerializeObject(entity, MhpiConverter()) | |
// Results | |
{ "Foo": "foo", "Bar": 1.0 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment