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 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);
        }