|
| 1 | +namespace FSharp.Data.JsonProvider |
| 2 | + |
| 3 | +open System.Runtime.CompilerServices |
| 4 | +open System |
| 5 | +open System.Text |
| 6 | +open System.Text.Json |
| 7 | +open FSharp.Data |
| 8 | +open System.IO |
| 9 | + |
| 10 | +module internal SerializationFunctions = |
| 11 | + |
| 12 | + let defaultoptions = |
| 13 | + JsonReaderOptions( |
| 14 | + AllowTrailingCommas = true, |
| 15 | + CommentHandling = JsonCommentHandling.Skip) |
| 16 | + |
| 17 | + let rec constructArray (reader: byref<Utf8JsonReader>) (built:JsonValue[]) = |
| 18 | + if not (reader.Read()) then built |
| 19 | + else |
| 20 | + match reader.TokenType with |
| 21 | + | JsonTokenType.EndArray -> |
| 22 | + built |
| 23 | + | _ -> |
| 24 | + match handleToken &reader with |
| 25 | + | ValueSome thisToken -> |
| 26 | + let nextGen = Array.append built [|thisToken|] |
| 27 | + constructArray &reader nextGen |
| 28 | + | ValueNone -> built |
| 29 | + |
| 30 | + and constructRecord (reader: byref<Utf8JsonReader>) (nextName:string) (built:(string*JsonValue)[]) = |
| 31 | + if not (reader.Read()) then built |
| 32 | + else |
| 33 | + match reader.TokenType with |
| 34 | + | JsonTokenType.EndObject -> |
| 35 | + built |
| 36 | + | JsonTokenType.PropertyName -> |
| 37 | + let name = reader.GetString() |
| 38 | + // todo, combine name and value |
| 39 | + constructRecord &reader name built |
| 40 | + | _ -> |
| 41 | + match handleToken &reader with |
| 42 | + | ValueSome thisToken -> |
| 43 | + |
| 44 | + let nextGen = Array.append built [|nextName,thisToken|] |
| 45 | + constructRecord &reader nextName nextGen |
| 46 | + | ValueNone -> built |
| 47 | + |
| 48 | + and handleToken (reader: byref<Utf8JsonReader>) = |
| 49 | + match reader.TokenType with |
| 50 | + | JsonTokenType.Number -> |
| 51 | + match reader.TryGetDecimal() with |
| 52 | + | true, deci -> ValueSome (JsonValue.Number (reader.GetDecimal())) |
| 53 | + | false, floa -> ValueSome (JsonValue.Float (reader.GetDouble())) |
| 54 | + | JsonTokenType.Null -> ValueSome JsonValue.Null |
| 55 | + | JsonTokenType.String -> |
| 56 | + ValueSome (JsonValue.String (reader.GetString())) |
| 57 | + | JsonTokenType.Comment |
| 58 | + | JsonTokenType.None -> ValueNone |
| 59 | + | JsonTokenType.True -> ValueSome (JsonValue.Boolean true) |
| 60 | + | JsonTokenType.False -> ValueSome (JsonValue.Boolean false) |
| 61 | + | JsonTokenType.StartArray -> |
| 62 | + let array = constructArray &reader [||] |
| 63 | + ValueSome (JsonValue.Array array) |
| 64 | + | JsonTokenType.StartObject -> |
| 65 | + let record = constructRecord &reader "" [||] |
| 66 | + ValueSome (JsonValue.Record record) |
| 67 | + |
| 68 | + |
| 69 | + | x -> failwithf $"Unexpected json: {x}" // Other token types elided for brevity |
| 70 | + |
| 71 | + |
| 72 | + let read (contentBytes:ReadOnlySpan<byte>) (options:JsonReaderOptions) = |
| 73 | + let mutable fullreader = Utf8JsonReader(contentBytes, options) |
| 74 | + let nextToken = fullreader.Read() |
| 75 | + if not nextToken then JsonValue.Null |
| 76 | + else |
| 77 | + |
| 78 | + match handleToken &fullreader with |
| 79 | + | ValueSome token -> token |
| 80 | + | ValueNone -> JsonValue.Null |
| 81 | + |
| 82 | + |
| 83 | + let write (item:JsonValue) (options:JsonWriterOptions voption) = |
| 84 | + use stream = new MemoryStream(); |
| 85 | + use writer = |
| 86 | + match options with |
| 87 | + | ValueSome opt -> new Utf8JsonWriter(stream, opt) |
| 88 | + | ValueNone -> new Utf8JsonWriter(stream) |
| 89 | + |
| 90 | + let rec write' content = |
| 91 | + match content with |
| 92 | + | JsonValue.Null -> writer.WriteNullValue() |
| 93 | + | JsonValue.Boolean b -> writer.WriteBooleanValue b |
| 94 | + | JsonValue.String s -> writer.WriteStringValue s |
| 95 | + | JsonValue.Float f -> writer.WriteNumberValue f |
| 96 | + | JsonValue.Number n -> writer.WriteNumberValue n |
| 97 | + | JsonValue.Array a -> |
| 98 | + writer.WriteStartArray() |
| 99 | + for itm in a do |
| 100 | + write' itm |
| 101 | + writer.WriteEndArray() |
| 102 | + | JsonValue.Record r -> |
| 103 | + writer.WriteStartObject() |
| 104 | + for (name,value) in r do |
| 105 | + writer.WritePropertyName name |
| 106 | + write' value |
| 107 | + writer.WriteEndObject() |
| 108 | + write' item |
| 109 | + writer.Flush(); |
| 110 | + stream.ToArray() |
| 111 | + |
| 112 | +module Serializer = |
| 113 | + |
| 114 | + /// Deserialize UTF8 stream to FSharp.Data.JsonValue using System.Text.Json |
| 115 | + [<Extension>] |
| 116 | + let DeserializeBytes (jsonUtf8Bytes:ReadOnlySpan<byte>) = |
| 117 | + SerializationFunctions.read jsonUtf8Bytes SerializationFunctions.defaultoptions |
| 118 | + |
| 119 | + /// Deserialize UTF8 stream to FSharp.Data.JsonValue using System.Text.Json |
| 120 | + [<Extension>] |
| 121 | + let DeserializeBytesWith (jsonUtf8Bytes:ReadOnlySpan<byte>, options:JsonReaderOptions) = |
| 122 | + SerializationFunctions.read jsonUtf8Bytes options |
| 123 | + |
| 124 | + /// Deserialize string to FSharp.Data.JsonValue using System.Text.Json |
| 125 | + [<Extension>] |
| 126 | + let Deserialize (item:string) = |
| 127 | + SerializationFunctions.read (System.ReadOnlySpan (Encoding.UTF8.GetBytes item)) SerializationFunctions.defaultoptions |
| 128 | + |
| 129 | + /// Deserialize string to FSharp.Data.JsonValue using System.Text.Json |
| 130 | + [<Extension>] |
| 131 | + let DeserializeWith (item:string, options:JsonReaderOptions) = |
| 132 | + SerializationFunctions.read (System.ReadOnlySpan (Encoding.UTF8.GetBytes item)) options |
| 133 | + |
| 134 | + |
| 135 | + /// Serialize FSharp.Data.JsonValue to byte array using System.Text.Json |
| 136 | + [<Extension>] |
| 137 | + let SerializeBytes (item:JsonValue) = |
| 138 | + SerializationFunctions.write item ValueNone |
| 139 | + |
| 140 | + /// Serialize FSharp.Data.JsonValue to byte array using System.Text.Json |
| 141 | + [<Extension>] |
| 142 | + let SerializeBytesWith (item:JsonValue, options:JsonWriterOptions) = |
| 143 | + SerializationFunctions.write item (ValueSome options) |
| 144 | + |
| 145 | + /// Serialize FSharp.Data.JsonValue to string using System.Text.Json |
| 146 | + [<Extension>] |
| 147 | + let Serialize (item:JsonValue) = |
| 148 | + Encoding.UTF8.GetString(SerializationFunctions.write item ValueNone) |
| 149 | + |
| 150 | + /// Serialize FSharp.Data.JsonValue to string using System.Text.Json |
| 151 | + [<Extension>] |
| 152 | + let SerializeWith (item:JsonValue, options:JsonWriterOptions) = |
| 153 | + Encoding.UTF8.GetString(SerializationFunctions.write item (ValueSome options)) |
| 154 | + |
0 commit comments