Kinetica   C#   API  Version 7.2.3.0
Schema.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Text;
21 using Newtonsoft.Json.Linq;
22 using Newtonsoft.Json;
23 
24 namespace Avro
25 {
29  public abstract class Schema
30  {
34  public enum Type
35  {
36  Null,
37  Boolean,
38  Int,
39  Long,
40  Float,
41  Double,
42  Bytes,
43  String,
44  Record,
45  Enumeration,
46  Array,
47  Map,
48  Union,
49  Fixed,
50  Error
51  }
52 
56  public Type Tag { get; private set; }
57 
61  internal PropertyMap Props { get; private set; }
62 
67  protected Schema(Type type, PropertyMap props)
68  {
69  this.Tag = type;
70  this.Props = props;
71  }
72 
77  public abstract string Name { get; }
78 
86  internal static Schema ParseJson(JToken jtok, SchemaNames names, string encspace)
87  {
88  if (null == jtok) throw new ArgumentNullException("j", "j cannot be null.");
89 
90  if (jtok.Type == JTokenType.String) // primitive schema with no 'type' property or primitive or named type of a record field
91  {
92  string value = (string)jtok;
93 
95  if (null != ps) return ps;
96 
97  NamedSchema schema = null;
98  if (names.TryGetValue(value, null, encspace, out schema)) return schema;
99 
100  throw new SchemaParseException("Undefined name: " + value);
101  }
102 
103  if (jtok is JArray) // union schema with no 'type' property or union type for a record field
104  return UnionSchema.NewInstance(jtok as JArray, null, names, encspace);
105 
106  if (jtok is JObject) // JSON object with open/close parenthesis, it must have a 'type' property
107  {
108  JObject jo = jtok as JObject;
109 
110  JToken jtype = jo["type"];
111  if (null == jtype)
112  throw new SchemaParseException("Property type is required");
113 
114  var props = Schema.GetProperties(jtok);
115 
116  if (jtype.Type == JTokenType.String)
117  {
118  string type = (string)jtype;
119 
120  if (type.Equals("array"))
121  return ArraySchema.NewInstance(jtok, props, names, encspace);
122  if (type.Equals("map"))
123  return MapSchema.NewInstance(jtok, props, names, encspace);
124 
125  Schema schema = PrimitiveSchema.NewInstance((string)type, props);
126  if (null != schema) return schema;
127 
128  return NamedSchema.NewInstance(jo, props, names, encspace);
129  }
130  else if (jtype.Type == JTokenType.Array)
131  return UnionSchema.NewInstance(jtype as JArray, props, names, encspace);
132  }
133  throw new AvroTypeException("Invalid JSON for schema: " + jtok);
134  }
135 
141  public static Schema Parse(string json)
142  {
143  if (string.IsNullOrEmpty(json)) throw new ArgumentNullException("json", "json cannot be null.");
144  return Parse(json.Trim(), new SchemaNames(), null); // standalone schema, so no enclosing namespace
145  }
146 
154  internal static Schema Parse(string json, SchemaNames names, string encspace)
155  {
157  if (null != sc) return sc;
158 
159  try
160  {
161  bool IsArray = json.StartsWith("[") && json.EndsWith("]");
162  JContainer j = IsArray ? (JContainer)JArray.Parse(json) : (JContainer)JObject.Parse(json);
163 
164  return ParseJson(j, names, encspace);
165  }
166  catch (Newtonsoft.Json.JsonSerializationException ex)
167  {
168  throw new SchemaParseException("Could not parse. " + ex.Message + Environment.NewLine + json);
169  }
170  }
171 
177  internal static PropertyMap GetProperties(JToken jtok)
178  {
179  var props = new PropertyMap();
180  props.Parse(jtok);
181  if (props.Count > 0)
182  return props;
183  else
184  return null;
185  }
186 
191  public override string ToString()
192  {
193  System.IO.StringWriter sw = new System.IO.StringWriter();
194  Newtonsoft.Json.JsonTextWriter writer = new Newtonsoft.Json.JsonTextWriter(sw);
195 
196  if (this is PrimitiveSchema || this is UnionSchema)
197  {
198  writer.WriteStartObject();
199  writer.WritePropertyName("type");
200  }
201 
202  WriteJson(writer, new SchemaNames(), null); // stand alone schema, so no enclosing name space
203 
204  if (this is PrimitiveSchema || this is UnionSchema)
205  writer.WriteEndObject();
206 
207  return sw.ToString();
208  }
209 
214  private void writeStartObject(JsonTextWriter writer)
215  {
216  writer.WriteStartObject();
217  writer.WritePropertyName("type");
218  writer.WriteValue(GetTypeString(this.Tag));
219  }
220 
226  public static string GetTypeString(Type type)
227  {
228  if (type != Type.Enumeration) return type.ToString().ToLower();
229  return "enum";
230  }
231 
238  protected internal virtual void WriteJsonFields(JsonTextWriter writer, SchemaNames names, string encspace)
239  {
240  }
241 
248  protected internal virtual void WriteJson(JsonTextWriter writer, SchemaNames names, string encspace)
249  {
250  writeStartObject(writer);
251  WriteJsonFields(writer, names, encspace);
252  if (null != this.Props) Props.WriteJson(writer);
253  writer.WriteEndObject();
254  }
255 
261  public string GetProperty(string key)
262  {
263  if (null == this.Props) return null;
264  string v;
265  return (this.Props.TryGetValue(key, out v)) ? v : null;
266  }
267 
272  public override int GetHashCode()
273  {
274  return Tag.GetHashCode() + getHashCode(Props);
275  }
276 
283  public virtual bool CanRead(Schema writerSchema) { return Tag == writerSchema.Tag; }
284 
291  protected static bool areEqual(object o1, object o2)
292  {
293  return o1 == null ? o2 == null : o1.Equals(o2);
294  }
295 
301  protected static int getHashCode(object obj)
302  {
303  return obj == null ? 0 : obj.GetHashCode();
304  }
305  }
306 }
void WriteJson(JsonTextWriter writer)
Writes the schema's custom properties in JSON format
Definition: Property.cs:74
static int getHashCode(object obj)
Hash code helper function
Definition: Schema.cs:301
override int GetHashCode()
Hash code function
Definition: Schema.cs:272
bool TryGetValue(string name, string space, string encspace, out NamedSchema schema)
Tries to get the value for the given name fields
Definition: SchemaName.cs:207
Base class for all schema types
Definition: Schema.cs:29
static string GetTypeString(Type type)
Returns symbol name for the given schema type
Definition: Schema.cs:226
Class for union schemas
Definition: UnionSchema.cs:29
virtual bool CanRead(Schema writerSchema)
Returns true if and only if data written using writerSchema can be read using the current schema acco...
Definition: Schema.cs:283
Type
Enum for schema types
Definition: Schema.cs:34
virtual internal void WriteJson(JsonTextWriter writer, SchemaNames names, string encspace)
Writes schema object in JSON format
Definition: Schema.cs:248
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
override string ToString()
Returns the canonical JSON representation of this schema.
Definition: Schema.cs:191
Class for schemas of primitive types
static PrimitiveSchema NewInstance(string type, PropertyMap props=null)
Static function to return new instance of primitive schema
A class that contains a list of named schemas.
Definition: SchemaName.cs:146
virtual internal void WriteJsonFields(JsonTextWriter writer, SchemaNames names, string encspace)
Default implementation for writing schema properties in JSON format
Definition: Schema.cs:238
Schema(Type type, PropertyMap props)
Constructor for schema class
Definition: Schema.cs:67
static bool areEqual(object o1, object o2)
Compares two objects, null is equal to null
Definition: Schema.cs:291
abstract string Name
The name of this schema.
Definition: Schema.cs:77
string GetProperty(string key)
Returns the schema's custom property value given the property name
Definition: Schema.cs:261
static Schema Parse(string json)
Parses a given JSON string to create a new schema object
Definition: Schema.cs:141
Type Tag
Schema type property
Definition: Schema.cs:56
Base class for all named schemas: fixed, enum, record
Definition: NamedSchema.cs:29