Created
May 15, 2021 22:52
-
-
Save JordanMarr/41c0be35746f83e8ef796b74a9da5d99 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