private Thing CreateThing(string json) { ObjectConstructor ctor = new ObjectConstructor(typeof(Thing)); ImportContext context = JsonConvert.CreateImportContext(); ObjectConstructionResult result = ctor.CreateObject(context, JsonText.CreateReader(json)); return((Thing)result.Object); }
public void CreateObject() { ObjectSurrogateConstructor ctor = new ObjectSurrogateConstructor(typeof(Surrogate)); ImportContext context = new ImportContext(); ObjectConstructionResult result = ctor.CreateObject(context, JsonText.CreateReader("{y:2000,m:12,d:4}")); Assert.IsNotNull(result); Assert.AreEqual(new DateTime(2000, 12, 4), result.Object); }
public void Construction() { ObjectConstructor ctor = new ObjectConstructor(typeof(Point)); ImportContext context = JsonConvert.CreateImportContext(); const string json = "{ y: 456, x: 123 }"; ObjectConstructionResult result = ctor.CreateObject(context, JsonText.CreateReader(json)); Point point = (Point)result.Object; Assert.AreEqual(123, point.X); Assert.AreEqual(456, point.Y); Assert.AreEqual(0, JsonBuffer.From(result.TailReader).GetMemberCount()); }
public void ValueTypeWithDefaultConstructorConstruction() { ObjectConstructor constructor = new ObjectConstructor(typeof(ValueThing)); ObjectConstructionResult result = constructor.CreateObject(new ImportContext(), JsonText.CreateReader("{foo:bar}")); Assert.IsInstanceOfType(typeof(ValueThing), result.Object); JsonReader tail = result.TailReader; tail.ReadToken(JsonTokenClass.Object); Assert.AreEqual("foo", tail.ReadMember()); Assert.AreEqual("bar", tail.ReadString()); tail.ReadToken(JsonTokenClass.EndObject); }
public void CreateObject() { object obj = new object(); Surrogate surrogate = new Surrogate(obj); ImportContext context = new ImportContext(); ObjectConstructionResult result = surrogate.CreateObject(context); Assert.AreSame(obj, result.Object); JsonReader tail = result.TailReader; tail.ReadToken(JsonTokenClass.Object); tail.ReadToken(JsonTokenClass.EndObject); }
public void ConstructionWithTail() { ObjectConstructor ctor = new ObjectConstructor(typeof(Point)); ImportContext context = JsonConvert.CreateImportContext(); const string json = "{ y: 456, z: 789, x: 123 }"; ObjectConstructionResult result = ctor.CreateObject(context, JsonText.CreateReader(json)); Point point = (Point)result.Object; Assert.AreEqual(123, point.X); Assert.AreEqual(456, point.Y); NamedJsonBuffer[] tail = JsonBuffer.From(result.TailReader).GetMembersArray(); Assert.AreEqual(1, tail.Length); NamedJsonBuffer z = tail[0]; Assert.AreEqual("z", z.Name); Assert.AreEqual(789, z.Buffer.GetNumber().ToInt32()); }
protected override object ImportFromObject(ImportContext context, JsonReader reader) { Debug.Assert(context != null); Debug.Assert(reader != null); object obj; if (_constructor != null) { ObjectConstructionResult result = _constructor.CreateObject(context, reader); obj = result.Object; reader = result.TailReader; reader.MoveToContent(); reader.Read(); } else { reader.Read(); obj = Activator.CreateInstance(OutputType); } INonObjectMemberImporter otherImporter = obj as INonObjectMemberImporter; while (reader.TokenClass != JsonTokenClass.EndObject) { string memberName = reader.ReadMember(); PropertyDescriptor property = _properties.Find(memberName, true); // // Skip over the member value and continue with reading if // the property was not found or if the property found cannot // be set. // if (property == null || property.IsReadOnly) { if (otherImporter == null || !otherImporter.Import(context, memberName, reader)) { reader.Skip(); } continue; } // // Check if the property defines a custom import scheme. // If yes, ask it to import the value into the property // and then continue with the next. // if (_importers != null) { int index = _properties.IndexOf(property); IObjectMemberImporter importer = _importers[index]; if (importer != null) { importer.Import(context, reader, obj); continue; } } // // Import from reader based on the property type and // then set the value of the property. // property.SetValue(obj, context.Import(property.PropertyType, reader)); } return(ReadReturning(reader, obj)); }