|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License") |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +using FlowtideDotNet.Core.ColumnStore.DataValues; |
| 14 | +using System.Text.Json; |
| 15 | + |
| 16 | +namespace FlowtideDotNet.Core.ColumnStore.Json |
| 17 | +{ |
| 18 | + internal static class DataValueJsonReader |
| 19 | + { |
| 20 | + public static IDataValue Read(ref Utf8JsonReader reader) |
| 21 | + { |
| 22 | + while (reader.TokenType == JsonTokenType.Comment || reader.TokenType == JsonTokenType.None) |
| 23 | + { |
| 24 | + if (!reader.Read()) |
| 25 | + { |
| 26 | + throw new JsonException("Unexpected json"); |
| 27 | + } |
| 28 | + continue; |
| 29 | + } |
| 30 | + switch (reader.TokenType) |
| 31 | + { |
| 32 | + case JsonTokenType.String: |
| 33 | + var strVal = reader.GetString(); |
| 34 | + if (strVal == null) |
| 35 | + { |
| 36 | + return NullValue.Instance; |
| 37 | + } |
| 38 | + return new StringValue(strVal); |
| 39 | + case JsonTokenType.False: |
| 40 | + return new BoolValue(false); |
| 41 | + case JsonTokenType.True: |
| 42 | + return new BoolValue(true); |
| 43 | + case JsonTokenType.Number: |
| 44 | + if (reader.TryGetInt64(out var intVal)) |
| 45 | + { |
| 46 | + return new Int64Value(intVal); |
| 47 | + } |
| 48 | + if (reader.TryGetDouble(out var doubleVal)) |
| 49 | + { |
| 50 | + return new DoubleValue(doubleVal); |
| 51 | + } |
| 52 | + throw new JsonException(); |
| 53 | + case JsonTokenType.Null: |
| 54 | + return NullValue.Instance; |
| 55 | + case JsonTokenType.StartObject: |
| 56 | + return ParseObject(ref reader); |
| 57 | + case JsonTokenType.StartArray: |
| 58 | + return ParseArray(ref reader); |
| 59 | + } |
| 60 | + throw new JsonException($"Unknown json token type {reader.TokenType}"); |
| 61 | + } |
| 62 | + |
| 63 | + private static IDataValue ParseArray(ref Utf8JsonReader reader) |
| 64 | + { |
| 65 | + if (!reader.Read()) |
| 66 | + { |
| 67 | + throw new JsonException("Unexpected json"); |
| 68 | + } |
| 69 | + |
| 70 | + List<IDataValue> values = new List<IDataValue>(); |
| 71 | + while (true) |
| 72 | + { |
| 73 | + if (reader.TokenType == JsonTokenType.EndArray) |
| 74 | + { |
| 75 | + break; |
| 76 | + } |
| 77 | + |
| 78 | + values.Add(Read(ref reader)); |
| 79 | + |
| 80 | + if (!reader.Read()) |
| 81 | + { |
| 82 | + throw new JsonException("Unexpected json"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return new ListValue(values); |
| 87 | + } |
| 88 | + |
| 89 | + private static IDataValue ParseObject(ref Utf8JsonReader reader) |
| 90 | + { |
| 91 | + if (!reader.Read()) |
| 92 | + { |
| 93 | + throw new JsonException("Unexpected json"); |
| 94 | + } |
| 95 | + |
| 96 | + List<KeyValuePair<IDataValue, IDataValue>> properties = new List<KeyValuePair<IDataValue, IDataValue>>(); |
| 97 | + while (true) |
| 98 | + { |
| 99 | + if (reader.TokenType == JsonTokenType.EndObject) |
| 100 | + { |
| 101 | + break; |
| 102 | + } |
| 103 | + if (reader.TokenType != JsonTokenType.PropertyName) |
| 104 | + { |
| 105 | + throw new JsonException(); |
| 106 | + } |
| 107 | + |
| 108 | + var propertyName = reader.GetString(); |
| 109 | + |
| 110 | + if (propertyName == null) |
| 111 | + { |
| 112 | + throw new JsonException("Unexpected json"); |
| 113 | + } |
| 114 | + |
| 115 | + if (!reader.Read()) |
| 116 | + { |
| 117 | + throw new JsonException("Unexpected json"); |
| 118 | + } |
| 119 | + |
| 120 | + var value = Read(ref reader); |
| 121 | + |
| 122 | + if (!reader.Read()) |
| 123 | + { |
| 124 | + throw new JsonException("Unexpected json"); |
| 125 | + } |
| 126 | + |
| 127 | + properties.Add(new KeyValuePair<IDataValue, IDataValue>(new StringValue(propertyName), value)); |
| 128 | + } |
| 129 | + |
| 130 | + return new MapValue(properties); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments