Exemplo n.º 1
0
        public void TestMethodMissingProperties()
        {
            JsonObjectSerializer serializer = new JsonObjectSerializer();

            serializer.IsMissingFieldDataAllowed = true;

            FullFeaturedObject fullFeaturedObj = new FullFeaturedObject();

            fullFeaturedObj.Property1 = 3;
            fullFeaturedObj.Property2 = "2sdfjsalf32p3sdf";
            fullFeaturedObj.Property3 = 4.67;
            fullFeaturedObj.Property4 = "Test";
            fullFeaturedObj.Property5 = 7;

            string         json            = serializer.Serialize(fullFeaturedObj, null);
            FeaturedObject deserializedObj = (FeaturedObject)serializer.Deserialize(json, typeof(FeaturedObject), null);

            Assert.Equal <int>(fullFeaturedObj.Property1, deserializedObj.Property1);
            Assert.Equal(fullFeaturedObj.Property2, deserializedObj.Property2);
            Assert.Equal <double>(fullFeaturedObj.Property3, deserializedObj.Property3);

            // do the same with different property order
            json = "{\"Property1\":3,\"Property2\":\"2sdfjsalf32p3sdf\",\"Property3\":4.67,\"Property4\":\"Test\",\"Property5\":7}";

            deserializedObj = (FeaturedObject)serializer.Deserialize(json, typeof(FeaturedObject), null);

            Assert.Equal <int>(fullFeaturedObj.Property1, deserializedObj.Property1);
            Assert.Equal(fullFeaturedObj.Property2, deserializedObj.Property2);
            Assert.Equal <double>(fullFeaturedObj.Property3, deserializedObj.Property3);
        }
Exemplo n.º 2
0
        public void TestMethodMissingMultiSubObjectProperties()
        {
            JsonObjectSerializer serializer = new JsonObjectSerializer();

            serializer.IsMissingFieldDataAllowed = true;

            FullFeaturedMultiSubObject fullFeaturedObj = new FullFeaturedMultiSubObject();

            fullFeaturedObj.Property1 = 3;
            fullFeaturedObj.Property2 = "2sdfjsalf32p3sdf";
            fullFeaturedObj.Property3 = 4.67;
            fullFeaturedObj.Property4 = "Test";
            fullFeaturedObj.Property5 = 7;
            fullFeaturedObj.Holder1   = new SubObjectHolder()
            {
                SubSubObject = new SubObject()
                {
                    SubId = 3, SubDescr = "ldfdlsajsd"
                }
            };
            fullFeaturedObj.Holder2 = new SubObjectHolder()
            {
                SubSubObject = new SubObject()
                {
                    SubId = 6, SubDescr = "dsgfdgsfdgfs dfdsf"
                }
            };

            fullFeaturedObj.SubObjectProperty = new SubObject()
            {
                SubId = 1, SubDescr = "ldsfadsfs"
            };

            string         json            = serializer.Serialize(fullFeaturedObj, null);
            FeaturedObject deserializedObj = (FeaturedObject)serializer.Deserialize(json, typeof(FeaturedObject), null);

            Assert.Equal <int>(fullFeaturedObj.Property1, deserializedObj.Property1);
            Assert.Equal(fullFeaturedObj.Property2, deserializedObj.Property2);
            Assert.Equal <double>(fullFeaturedObj.Property3, deserializedObj.Property3);

            // do the same with different property orders
            json = "{\"Property1\":3,\"Property2\":\"2sdfjsalf32p3sdf\",\"Property3\":4.67,\"Holder1\":{\"SubSubObject\":{\"SubId\":3,\"SubDescr\":\"ldfdlsajsd\"}},\"Holder2\":{\"SubSubObject\":{\"SubId\":6,\"SubDescr\":\"dsgfdgsfdgfs dfdsf\"}},\"SubObjectProperty\":{\"SubId\":1,\"SubDescr\":\"ldsfadsfs\"},\"Property4\":\"Test\",\"Property5\":7}";

            deserializedObj = (FeaturedObject)serializer.Deserialize(json, typeof(FeaturedObject), null);

            Assert.Equal <int>(fullFeaturedObj.Property1, deserializedObj.Property1);
            Assert.Equal(fullFeaturedObj.Property2, deserializedObj.Property2);
            Assert.Equal <double>(fullFeaturedObj.Property3, deserializedObj.Property3);
        }
Exemplo n.º 3
0
        public void TestMethodMissingPropertiesOtherWay()
        {
            JsonObjectSerializer serializer = new JsonObjectSerializer();

            serializer.IsMissingFieldDataAllowed = true;

            FeaturedObject featuredObj = new FeaturedObject();

            featuredObj.Property1 = 3;
            featuredObj.Property2 = "2sdfjsalf32p3sdf";
            featuredObj.Property3 = 4.67;

            string             json            = serializer.Serialize(featuredObj, null);
            FullFeaturedObject deserializedObj = (FullFeaturedObject)serializer.Deserialize(json, typeof(FullFeaturedObject), null);

            Assert.Equal <int>(featuredObj.Property1, deserializedObj.Property1);
            Assert.Equal(featuredObj.Property2, deserializedObj.Property2);
            Assert.Equal <double>(featuredObj.Property3, deserializedObj.Property3);

            Assert.Null(deserializedObj.Property4);
            Assert.Equal <int>(0, deserializedObj.Property5);
        }