19 using System.Collections.Generic;
21 using Newtonsoft.Json.Linq;
22 using Newtonsoft.Json;
77 public abstract string Name {
get; }
88 if (
null == jtok)
throw new ArgumentNullException(
"j",
"j cannot be null.");
90 if (jtok.Type == JTokenType.String)
92 string value = (string)jtok;
95 if (
null != ps)
return ps;
98 if (names.
TryGetValue(value,
null, encspace, out schema))
return schema;
104 return UnionSchema.NewInstance(jtok as JArray,
null, names, encspace);
108 JObject jo = jtok as JObject;
110 JToken jtype = jo[
"type"];
112 throw new SchemaParseException(
"Property type is required");
114 var props =
Schema.GetProperties(jtok);
116 if (jtype.Type == JTokenType.String)
118 string type = (string)jtype;
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);
125 Schema schema = PrimitiveSchema.NewInstance((
string)type, props);
126 if (
null != schema)
return schema;
128 return NamedSchema.NewInstance(jo, props, names, encspace);
130 else if (jtype.Type == JTokenType.Array)
131 return UnionSchema.NewInstance(jtype as JArray, props, names, encspace);
133 throw new AvroTypeException(
"Invalid JSON for schema: " + jtok);
143 if (
string.IsNullOrEmpty(json))
throw new ArgumentNullException(
"json",
"json cannot be null.");
157 if (
null != sc)
return sc;
161 bool IsArray = json.StartsWith(
"[") && json.EndsWith(
"]");
162 JContainer j = IsArray ? (JContainer)JArray.
Parse(json) : (JContainer)JObject.
Parse(json);
164 return ParseJson(j, names, encspace);
166 catch (Newtonsoft.Json.JsonSerializationException ex)
168 throw new SchemaParseException(
"Could not parse. " + ex.Message + Environment.NewLine + json);
177 internal static PropertyMap GetProperties(JToken jtok)
179 var props =
new PropertyMap();
193 System.IO.StringWriter sw =
new System.IO.StringWriter();
194 Newtonsoft.Json.JsonTextWriter writer =
new Newtonsoft.Json.JsonTextWriter(sw);
198 writer.WriteStartObject();
199 writer.WritePropertyName(
"type");
205 writer.WriteEndObject();
207 return sw.ToString();
214 private void writeStartObject(JsonTextWriter writer)
216 writer.WriteStartObject();
217 writer.WritePropertyName(
"type");
228 if (type !=
Type.Enumeration)
return type.ToString().ToLower();
250 writeStartObject(writer);
252 if (
null != this.Props) Props.
WriteJson(writer);
253 writer.WriteEndObject();
263 if (
null == this.Props)
return null;
265 return (this.Props.TryGetValue(key, out v)) ? v :
null;
291 protected static bool areEqual(
object o1,
object o2)
293 return o1 ==
null ? o2 == null : o1.Equals(o2);
303 return obj ==
null ? 0 : obj.GetHashCode();
void WriteJson(JsonTextWriter writer)
Writes the schema's custom properties in JSON format
static int getHashCode(object obj)
Hash code helper function
override int GetHashCode()
Hash code function
bool TryGetValue(string name, string space, string encspace, out NamedSchema schema)
Tries to get the value for the given name fields
Base class for all schema types
static string GetTypeString(Type type)
Returns symbol name for the given schema type
virtual bool CanRead(Schema writerSchema)
Returns true if and only if data written using writerSchema can be read using the current schema acco...
Type
Enum for schema types
virtual internal void WriteJson(JsonTextWriter writer, SchemaNames names, string encspace)
Writes schema object in JSON format
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.
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.
virtual internal void WriteJsonFields(JsonTextWriter writer, SchemaNames names, string encspace)
Default implementation for writing schema properties in JSON format
Schema(Type type, PropertyMap props)
Constructor for schema class
static bool areEqual(object o1, object o2)
Compares two objects, null is equal to null
abstract string Name
The name of this schema.
string GetProperty(string key)
Returns the schema's custom property value given the property name
static Schema Parse(string json)
Parses a given JSON string to create a new schema object
Type Tag
Schema type property
Base class for all named schemas: fixed, enum, record