]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/csharp/test/Apache.Arrow.IntegrationTest/JsonFile.cs
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / csharp / test / Apache.Arrow.IntegrationTest / JsonFile.cs
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15
16 using System;
17 using System.Collections.Generic;
18 using System.Text.Json;
19 using System.Text.Json.Serialization;
20
21 namespace Apache.Arrow.IntegrationTest
22 {
23 public class JsonFile
24 {
25 public JsonSchema Schema { get; set; }
26 public List<JsonRecordBatch> Batches { get; set; }
27 //public List<DictionaryBatch> Dictionaries {get;set;}
28 }
29
30 public class JsonSchema
31 {
32 public List<JsonField> Fields { get; set; }
33 public JsonMetadata Metadata { get; set; }
34 }
35
36 public class JsonField
37 {
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; }
44 }
45
46 public class JsonArrowType
47 {
48 public string Name { get; set; }
49
50 // int fields
51 public int BitWidth { get; set; }
52 public bool IsSigned { get; set; }
53
54 // floating point fields
55 [JsonIgnore]
56 public string FloatingPointPrecision => ExtensionData["precision"].GetString();
57
58 // decimal fields
59 [JsonIgnore]
60 public int DecimalPrecision => ExtensionData["precision"].GetInt32();
61 public int Scale { get; set; }
62
63 // date and time fields
64 public string Unit { get; set; }
65 // timestamp fields
66 public string Timezone { get; set; }
67
68 // FixedSizeBinary fields
69 public int ByteWidth { get; set; }
70
71 [JsonExtensionData]
72 public Dictionary<string, JsonElement> ExtensionData { get; set; }
73 }
74
75 public class JsonDictionaryIndex
76 {
77 public int Id { get; set; }
78 public JsonArrowType Type { get; set; }
79 public bool IsOrdered { get; set; }
80 }
81
82 public class JsonMetadata : List<KeyValuePair<string, string>>
83 {
84 }
85
86 public class JsonRecordBatch
87 {
88 public int Count { get; set; }
89 public List<JsonFieldData> Columns { get; set; }
90 }
91
92 public class JsonFieldData
93 {
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; }
101 }
102
103 internal sealed class ValidityConverter : JsonConverter<bool>
104 {
105 public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
106 {
107 if (reader.TokenType == JsonTokenType.True) return true;
108 if (reader.TokenType == JsonTokenType.False) return false;
109
110 if (typeToConvert != typeof(bool) || reader.TokenType != JsonTokenType.Number)
111 {
112 throw new InvalidOperationException($"Unexpected bool data: {reader.TokenType}");
113 }
114
115 int value = reader.GetInt32();
116 if (value == 0) return false;
117 if (value == 1) return true;
118
119 throw new InvalidOperationException($"Unexpected bool value: {value}");
120 }
121
122 public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options) => throw new NotImplementedException();
123 }
124
125 internal sealed class ByteArrayConverter : JsonConverter<byte[]>
126 {
127 public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
128 {
129 if (reader.TokenType != JsonTokenType.StartArray)
130 {
131 throw new InvalidOperationException($"Unexpected byte[] token: {reader.TokenType}");
132 }
133
134 List<byte> values = new List<byte>();
135 while (reader.Read())
136 {
137 if (reader.TokenType == JsonTokenType.EndArray)
138 {
139 return values.ToArray();
140 }
141
142 if (reader.TokenType != JsonTokenType.Number)
143 {
144 throw new InvalidOperationException($"Unexpected byte token: {reader.TokenType}");
145 }
146
147 values.Add(reader.GetByte());
148 }
149
150 throw new InvalidOperationException("Unexpectedly reached the end of the reader");
151 }
152
153 public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options) => throw new NotImplementedException();
154 }
155
156 internal sealed class JsonFileNamingPolicy : JsonNamingPolicy
157 {
158 public static JsonFileNamingPolicy Instance { get; } = new JsonFileNamingPolicy();
159
160 public override string ConvertName(string name)
161 {
162 if (name == "Validity")
163 {
164 return "VALIDITY";
165 }
166 else if (name == "Offset")
167 {
168 return "OFFSET";
169 }
170 else if (name == "TypeId")
171 {
172 return "TYPE_ID";
173 }
174 else if (name == "Data")
175 {
176 return "DATA";
177 }
178 else
179 {
180 return CamelCase.ConvertName(name);
181 }
182 }
183 }
184 }