Skip to content

Instantly share code, notes, and snippets.

@JordanMarr
Created May 15, 2021 22:52
Show Gist options
  • Save JordanMarr/41c0be35746f83e8ef796b74a9da5d99 to your computer and use it in GitHub Desktop.
Save JordanMarr/41c0be35746f83e8ef796b74a9da5d99 to your computer and use it in GitHub Desktop.
JSON.NET SCU Converter
// 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