1 // Licensed to the Apache Software Foundation (ASF) under one or more
2 // contributor license agreements. See the NOTICE file distributed with
3 // this work for additional information regarding copyright ownership.
4 // The ASF licenses this file to You under the Apache License, Version 2.0
5 // (the "License"); you may not use this file except in compliance with
6 // the License. You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 using System.Collections.Generic;
18 using System.Text.Json;
19 using System.Text.Json.Serialization;
21 namespace Apache.Arrow.IntegrationTest
25 public JsonSchema Schema { get; set; }
26 public List<JsonRecordBatch> Batches { get; set; }
27 //public List<DictionaryBatch> Dictionaries {get;set;}
30 public class JsonSchema
32 public List<JsonField> Fields { get; set; }
33 public JsonMetadata Metadata { get; set; }
36 public class JsonField
38 public string Name { get; set; }
39 public bool Nullable { get; set; }
40 public JsonArrowType Type { get; set; }
41 public List<JsonField> Children { get; set; }
42 public JsonDictionaryIndex Dictionary { get; set; }
43 public JsonMetadata Metadata { get; set; }
46 public class JsonArrowType
48 public string Name { get; set; }
51 public int BitWidth { get; set; }
52 public bool IsSigned { get; set; }
54 // floating point fields
56 public string FloatingPointPrecision => ExtensionData["precision"].GetString();
60 public int DecimalPrecision => ExtensionData["precision"].GetInt32();
61 public int Scale { get; set; }
63 // date and time fields
64 public string Unit { get; set; }
66 public string Timezone { get; set; }
68 // FixedSizeBinary fields
69 public int ByteWidth { get; set; }
72 public Dictionary<string, JsonElement> ExtensionData { get; set; }
75 public class JsonDictionaryIndex
77 public int Id { get; set; }
78 public JsonArrowType Type { get; set; }
79 public bool IsOrdered { get; set; }
82 public class JsonMetadata : List<KeyValuePair<string, string>>
86 public class JsonRecordBatch
88 public int Count { get; set; }
89 public List<JsonFieldData> Columns { get; set; }
92 public class JsonFieldData
94 public string Name { get; set; }
95 public int Count { get; set; }
96 public bool[] Validity { get; set; }
97 public int[] Offset { get; set; }
98 public int[] TypeId { get; set; }
99 public JsonElement Data { get; set; }
100 public List<JsonFieldData> Children { get; set; }
103 internal sealed class ValidityConverter : JsonConverter<bool>
105 public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
107 if (reader.TokenType == JsonTokenType.True) return true;
108 if (reader.TokenType == JsonTokenType.False) return false;
110 if (typeToConvert != typeof(bool) || reader.TokenType != JsonTokenType.Number)
112 throw new InvalidOperationException($"Unexpected bool data: {reader.TokenType}");
115 int value = reader.GetInt32();
116 if (value == 0) return false;
117 if (value == 1) return true;
119 throw new InvalidOperationException($"Unexpected bool value: {value}");
122 public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options) => throw new NotImplementedException();
125 internal sealed class ByteArrayConverter : JsonConverter<byte[]>
127 public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
129 if (reader.TokenType != JsonTokenType.StartArray)
131 throw new InvalidOperationException($"Unexpected byte[] token: {reader.TokenType}");
134 List<byte> values = new List<byte>();
135 while (reader.Read())
137 if (reader.TokenType == JsonTokenType.EndArray)
139 return values.ToArray();
142 if (reader.TokenType != JsonTokenType.Number)
144 throw new InvalidOperationException($"Unexpected byte token: {reader.TokenType}");
147 values.Add(reader.GetByte());
150 throw new InvalidOperationException("Unexpectedly reached the end of the reader");
153 public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options) => throw new NotImplementedException();
156 internal sealed class JsonFileNamingPolicy : JsonNamingPolicy
158 public static JsonFileNamingPolicy Instance { get; } = new JsonFileNamingPolicy();
160 public override string ConvertName(string name)
162 if (name == "Validity")
166 else if (name == "Offset")
170 else if (name == "TypeId")
174 else if (name == "Data")
180 return CamelCase.ConvertName(name);