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
member r.ReadAndAssert() =
if not (r.Read()) then raise (JsonReaderException("Unexpected end of JSON stream."))
member r.MoveToContentAndAssert() =
if r.TokenType = JsonToken.None then r.ReadAndAssert() |> ignore
while r.TokenType = JsonToken.Comment do r.ReadAndAssert() |> ignore
type SingleOrArrayConverter<'T>() =
override this.CanConvert(objectType) = objectType = typeof<ResizeArray<'T>>
override this.ReadJson(reader, objectType, existingValue, serializer) =
let a = if (existingValue :? ResizeArray<'T>) then existingValue :?> ResizeArray<'T> else ResizeArray<'T>()
match reader.MoveToContentAndAssert().TokenType with
| JsonToken.StartArray -> serializer.Populate(reader, a)
| _ -> a.Add(serializer.Deserialize<'T>(reader))
override this.CanWrite = false
override this.WriteJson(writer, value, serializer) = raise (new NotImplementedException())
[<JsonProperty("locality")>]
[<JsonConverter(typeof<SingleOrArrayConverter<Locality>>)>]
locality: ResizeArray<Locality>
type PostCodeLocality = {
/* Some COmment */ "localities": { /* Some COmment */
/* Some COmment */ /* Some COmment */ { /* Some COmment */
let p1 = JsonConvert.DeserializeObject<PostCodeLocality>(json0)
let p2 : PostCodeLocality = { localities = { locality = new ResizeArray<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"))