public object BuildObject(Type type) { if (type.GetInterface(nameof(IDictionary)) != null) { Type[] generics = type.GetGenericArguments(); if (generics[0] != typeof(string)) { throw new JsonSchemaException("Unable to create a dictionary with a key type that is not string"); } else { IDictionary dictionary = (IDictionary)ObjectCreator.NewInstance(type); foreach (KeyValuePair <string, IJsonValue> kvp in this) { dictionary.Add(kvp.Key, kvp.Value.BuildObject(generics[1])); } return(dictionary); } } else { object obj = ObjectCreator.NewInstance(type); JsonObjectSchema schema = JsonObjectSchemaCache.GetSchema(type); foreach (KeyValuePair <string, PropertyInfo> kvp in schema.JsonProperties) { if (this.ContainsKey(kvp.Key)) { kvp.Value.SetValue(obj, this[kvp.Key].BuildObject(kvp.Value.PropertyType)); } } return(obj); } }
public IJsonValue BuildToken() { switch (this._value) { case null: return(new JsonNull()); case string s: return(new JsonString(s)); case bool b: return(new JsonBoolean(b)); case sbyte sb: case byte b: case ushort us: case short s: case uint ui: case int i: case ulong ul: case long l: case float f: case double d: case decimal dc: return(new JsonNumber(this._value.ToString())); } // Array or object if (this._value.GetType().GetInterface(nameof(IList)) != null) { JsonWriterDirector writer; JsonArray arr = new JsonArray(); foreach (object obj in (IList)this._value) { writer = new JsonWriterDirector(obj); arr.Add(writer.BuildToken()); } return(arr); } else if (this._value.GetType().GetInterface(nameof(IDictionary)) != null) { JsonWriterDirector writer; JsonObject obj = new JsonObject(); Type[] generics = this._value.GetType().GetGenericArguments(); if (generics[0] != typeof(string)) { throw new JsonSchemaException("Can only serialize dictionaries with key type string"); } foreach (KeyValuePair <object, object> kvp in (IDictionary)this._value) { writer = new JsonWriterDirector(kvp.Value); obj.Add((string)kvp.Key, writer.BuildToken()); } return(obj); } else { JsonWriterDirector writer; JsonObject obj = new JsonObject(); JsonObjectSchema schema = JsonObjectSchemaCache.GetSchema(this._value.GetType()); foreach (KeyValuePair <string, PropertyInfo> kvp in schema.JsonProperties) { writer = new JsonWriterDirector(kvp.Value.GetValue(this._value)); obj.Add(kvp.Key, writer.BuildToken()); } return(obj); } }