Inheritance: Jayrock.Json.Conversion.Converters.ImporterBase
Ejemplo n.º 1
0
        public void MemberImportCustomization()
        {
            var logicalType = new TestTypeDescriptor();
            var properties  = logicalType.GetProperties();

            properties.Add(new TestPropertyDescriptor("prop1", typeof(object), new Hashtable()));

            var calls2          = new ArrayList();
            var memberImporter2 = new TestObjectMemberImporter(calls2);
            var services2       = new Hashtable
            {
                [typeof(IObjectMemberImporter)] = memberImporter2
            };

            properties.Add(new TestPropertyDescriptor("prop2", typeof(object), services2));

            // Third property added to exercise issue #27:
            // http://code.google.com/p/jayrock/issues/detail?id=27

            var calls3          = new ArrayList();
            var memberImporter3 = new TestObjectMemberImporter(calls3);
            var services3       = new Hashtable
            {
                [typeof(IObjectMemberImporter)] = memberImporter3
            };

            properties.Add(new TestPropertyDescriptor("prop3", typeof(object), services3));

            var importer = new ComponentImporter(typeof(Thing), logicalType);
            var context  = new ImportContext();

            context.Register(importer);

            var writer = new JsonRecorder();

            writer.WriteStartObject();
            writer.WriteMember("prop1");
            writer.WriteString("value1");
            writer.WriteMember("prop2");
            writer.WriteString("value2");
            writer.WriteMember("prop3");
            writer.WriteString("value3");
            writer.WriteEndObject();

            var reader = writer.CreatePlayer();
            var thing  = (Thing)context.Import(typeof(Thing), reader);

            Assert.AreEqual(1, calls2.Count);

            Assert.AreSame(memberImporter2, calls2[0]);
            Assert.AreEqual(new object[] { context, reader, thing }, memberImporter2.ImportArgs);
            Assert.AreEqual("value2", memberImporter2.ImportedValue);

            Assert.AreEqual(1, calls3.Count);

            Assert.AreSame(memberImporter3, calls3[0]);
            Assert.AreEqual(new object[] { context, reader, thing }, memberImporter3.ImportArgs);
            Assert.AreEqual("value3", memberImporter3.ImportedValue);
        }
Ejemplo n.º 2
0
        public void NonMemberImport()
        {
            var          importer = new ComponentImporter(typeof(DynamicThing));
            var          context  = new ImportContext();
            const string json     = "{ str1: value1, str2: value2, num: 42 }";
            var          thing    = (DynamicThing)importer.Import(context, JsonText.CreateReader(json));

            Assert.AreEqual(2, thing.NonMembers.Count);
            Assert.AreEqual("value1", thing.NonMembers["str1"]);
            Assert.AreEqual("value2", thing.NonMembers["str2"]);
        }
Ejemplo n.º 3
0
        public void SkipsReadOnlyProperty()
        {
            var thingType  = typeof(Thing);
            var reader     = new JsonTextReader(new StringReader("{ Id : 42 }"));
            var descriptor = new TestTypeDescriptor();

            descriptor.GetProperties().Add(new ReadOnlyPropertyDescriptor("Id"));
            var importer = new ComponentImporter(thingType, descriptor);

            importer.Import(new ImportContext(), reader);
            Assert.IsFalse(((ReadOnlyPropertyDescriptor)descriptor.GetProperties().Find("Id", false)).SetValueCalled);
        }
Ejemplo n.º 4
0
        public void MemberImportCustomization()
        {
            ArrayList calls = new ArrayList();

            TestTypeDescriptor           logicalType = new TestTypeDescriptor();
            PropertyDescriptorCollection properties  = logicalType.GetProperties();

            properties.Add(new TestPropertyDescriptor("prop1", typeof(object), new Hashtable()));

            TestObjectMemberImporter memberImporter = new TestObjectMemberImporter(calls);
            Hashtable services = new Hashtable();

            services.Add(typeof(IObjectMemberImporter), memberImporter);
            properties.Add(new TestPropertyDescriptor("prop2", typeof(object), services));

            ComponentImporter importer = new ComponentImporter(typeof(Thing), logicalType);
            ImportContext     context  = new ImportContext();

            context.Register(importer);

            JsonRecorder writer = new JsonRecorder();

            writer.WriteStartObject();
            writer.WriteMember("prop1");
            writer.WriteString("value1");
            writer.WriteMember("prop2");
            writer.WriteString("value2");
            writer.WriteEndObject();

            JsonReader reader = writer.CreatePlayer();
            Thing      thing  = (Thing)context.Import(typeof(Thing), reader);

            Assert.AreEqual(1, calls.Count);

            Assert.AreSame(memberImporter, calls[0]);
            Assert.AreEqual(new object[] { context, reader, thing }, memberImporter.ImportArgs);
            Assert.AreEqual("value2", memberImporter.ImportedValue);
        }
Ejemplo n.º 5
0
 public void ImportNull()
 {
     ComponentImporter importer = new ComponentImporter(typeof(object));
     Assert.IsNull(importer.Import(new ImportContext(), CreateReader("null")));
 }
Ejemplo n.º 6
0
 public void NonMemberImport()
 {
     ComponentImporter importer = new ComponentImporter(typeof(DynamicThing));
     ImportContext context = new ImportContext();
     const string json = "{ str1: value1, str2: value2, num: 42 }";
     DynamicThing thing = (DynamicThing) importer.Import(context, JsonText.CreateReader(json));
     Assert.AreEqual(2, thing.NonMembers.Count);
     Assert.AreEqual("value1", thing.NonMembers["str1"]);
     Assert.AreEqual("value2", thing.NonMembers["str2"]);
 }
Ejemplo n.º 7
0
        public void MemberImportCustomization()
        {
            TestTypeDescriptor logicalType = new TestTypeDescriptor();
            PropertyDescriptorCollection properties = logicalType.GetProperties();

            properties.Add(new TestPropertyDescriptor("prop1", typeof(object), new Hashtable()));

            ArrayList calls2 = new ArrayList();
            TestObjectMemberImporter memberImporter2 = new TestObjectMemberImporter(calls2);
            Hashtable services2 = new Hashtable();
            services2.Add(typeof(IObjectMemberImporter), memberImporter2);
            properties.Add(new TestPropertyDescriptor("prop2", typeof(object), services2));

            // Third property added to exercise issue #27:
            // http://code.google.com/p/jayrock/issues/detail?id=27

            ArrayList calls3 = new ArrayList();
            TestObjectMemberImporter memberImporter3 = new TestObjectMemberImporter(calls3);
            Hashtable services3 = new Hashtable();
            services3.Add(typeof(IObjectMemberImporter), memberImporter3);
            properties.Add(new TestPropertyDescriptor("prop3", typeof(object), services3));

            ComponentImporter importer = new ComponentImporter(typeof(Thing), logicalType);
            ImportContext context = new ImportContext();
            context.Register(importer);
            
            JsonRecorder writer = new JsonRecorder();
            writer.WriteStartObject();
            writer.WriteMember("prop1");
            writer.WriteString("value1");
            writer.WriteMember("prop2");
            writer.WriteString("value2");
            writer.WriteMember("prop3");
            writer.WriteString("value3");
            writer.WriteEndObject();

            JsonReader reader = writer.CreatePlayer();
            Thing thing = (Thing) context.Import(typeof(Thing), reader);

            Assert.AreEqual(1, calls2.Count);

            Assert.AreSame(memberImporter2, calls2[0]);
            Assert.AreEqual(new object[] { context, reader, thing }, memberImporter2.ImportArgs);
            Assert.AreEqual("value2", memberImporter2.ImportedValue);

            Assert.AreEqual(1, calls3.Count);

            Assert.AreSame(memberImporter3, calls3[0]);
            Assert.AreEqual(new object[] { context, reader, thing }, memberImporter3.ImportArgs);
            Assert.AreEqual("value3", memberImporter3.ImportedValue);
        }
Ejemplo n.º 8
0
 public void SkipsReadOnlyProperty()
 {
     Type thingType = typeof(Thing);
     JsonTextReader reader = new JsonTextReader(new StringReader("{ Id : 42 }"));
     TestTypeDescriptor descriptor = new TestTypeDescriptor();
     descriptor.GetProperties().Add(new ReadOnlyPropertyDescriptor("Id"));
     ComponentImporter importer = new ComponentImporter(thingType, descriptor);
     importer.Import(new ImportContext(), reader);
     Assert.IsFalse(((ReadOnlyPropertyDescriptor) descriptor.GetProperties().Find("Id", false)).SetValueCalled);
 }
Ejemplo n.º 9
0
        public void MemberImportCustomization()
        {
            ArrayList calls = new ArrayList();

            TestTypeDescriptor logicalType = new TestTypeDescriptor();
            PropertyDescriptorCollection properties = logicalType.GetProperties();

            properties.Add(new TestPropertyDescriptor("prop1", typeof(object), new Hashtable()));

            TestObjectMemberImporter memberImporter = new TestObjectMemberImporter(calls);
            Hashtable services = new Hashtable();
            services.Add(typeof(IObjectMemberImporter), memberImporter);
            properties.Add(new TestPropertyDescriptor("prop2", typeof(object), services));

            ComponentImporter importer = new ComponentImporter(typeof(Thing), logicalType);
            ImportContext context = new ImportContext();
            context.Register(importer);

            JsonRecorder writer = new JsonRecorder();
            writer.WriteStartObject();
            writer.WriteMember("prop1");
            writer.WriteString("value1");
            writer.WriteMember("prop2");
            writer.WriteString("value2");
            writer.WriteEndObject();

            JsonReader reader = writer.CreatePlayer();
            Thing thing = (Thing) context.Import(typeof(Thing), reader);

            Assert.AreEqual(1, calls.Count);

            Assert.AreSame(memberImporter, calls[0]);
            Assert.AreEqual(new object[] { context, reader, thing }, memberImporter.ImportArgs);
            Assert.AreEqual("value2", memberImporter.ImportedValue);
        }
        public void MemberImportCustomization()
        {
            TestObjectMemberImporter memberImporter = new TestObjectMemberImporter();
            Hashtable services = new Hashtable();
            services.Add(typeof(IObjectMemberImporter), memberImporter);

            TestTypeDescriptor logicalType = new TestTypeDescriptor();
            PropertyDescriptorCollection properties = logicalType.GetProperties();
            TestPropertyDescriptor property = new TestPropertyDescriptor("prop", typeof(object), services);
            properties.Add(property);
            
            ComponentImporter importer = new ComponentImporter(typeof(Thing), logicalType);
            ImportContext context = new ImportContext();
            context.Register(importer);
            
            JsonRecorder writer = new JsonRecorder();
            writer.WriteStartObject();
            writer.WriteMember("prop");
            writer.WriteString("value");
            writer.WriteEndObject();

            JsonReader reader = writer.CreatePlayer();
            Thing thing = (Thing) context.Import(typeof(Thing), reader);
            
            Assert.AreSame(context, memberImporter.ImportContext);
            Assert.AreSame(reader, memberImporter.ImportReader);
            Assert.AreSame(thing, memberImporter.ImportTarget);
            Assert.AreEqual("value", memberImporter.ImportedValue);
        }
Ejemplo n.º 11
0
        public void ImportNull()
        {
            var importer = new ComponentImporter(typeof(object));

            Assert.IsNull(importer.Import(new ImportContext(), CreateReader("null")));
        }