open System.Runtime.Serialization.Formatters
open System.ComponentModel.DataAnnotations
open System.Globalization
open Microsoft.FSharp.Reflection
open Newtonsoft.Json.Linq
open Newtonsoft.Json.Converters
open Newtonsoft.Json.Serialization
type SingleOrArrayFSharpListConverter<'T>() =
override this.CanConvert(objectType) = objectType = typeof<list<'T>>
override this.ReadJson(reader, objectType, existingValue, serializer) =
let token = JToken.Load (reader)
if token.Type = JTokenType.Array then
(token.ToObject<list<'T>> ()) :> obj
[token.ToObject<'T>()] :> obj
override this.CanWrite = false
override this.WriteJson(writer, value, serializer) = raise (new NotImplementedException())
[<JsonProperty("locality")>]
[<JsonConverter(typeof<SingleOrArrayFSharpListConverter<Locality>>)>]
type PostCodeLocality = {
let p1 = JsonConvert.DeserializeObject<PostCodeLocality>(json0)
let p2 : PostCodeLocality = { localities = { locality = [{category="category"; id=101; location="location"; postcode=22;state="ZZ"; latitude=81.0M; longitude=202M }] } }
let json1 = JsonConvert.SerializeObject(p1, Formatting.Indented)
let json2 = JsonConvert.SerializeObject(p2, Formatting.Indented)
if (json1 <> json2) then raise (Exception("json1 != json2"))
let p3 = JsonConvert.DeserializeObject<PostCodeLocality>(json2)
let json3 = JsonConvert.SerializeObject(p3, Formatting.Indented)
if (json1 <> json3) then raise (Exception("json1 != json3"))