You'll have to give ServiceStack a custom serializer. It would like this, using a smaller Shape type for brevity:
open ServiceStack.Text
type Shape =
| Circle of float
| Empty
JsConfig<Shape>.SerializeFn
<- Func<_,_> (function
| Circle r -> sprintf "C %f" r
| Empty -> sprintf "E")
JsConfig<Shape>.DeSerializeFn
<- Func<_,_> (fun s ->
match s.Split [| |] with
| [| "C"; r |] -> Circle (float r)
| [| "E" |] -> Empty)
let shapes = [| Circle 8.0 |]
let json = JsonSerializer.SerializeToString(shapes)
let shapes1 = JsonSerializer.DeserializeFromString<Shape[]>(json)
let is_ok = shapes = shapes1
This code does not have proper exception-propagation in the deserializer: you'd want to handle the match
not matching, and float
might raise a System.FormatException
.