예제 #1
0
        public void AnObjectPropertyWithAnEmptyValueRoundTripsFromXmlCorrectly(bool shouldTreatEmptyElementAsString)
        {
            var instance = new ClassWithObjectProperty
            {
                IntProperty    = 123,
                ObjectProperty = ""
            };

            var serializer = new XmlSerializer <ClassWithObjectProperty>(options =>
            {
                if (shouldTreatEmptyElementAsString)
                {
                    options.Indent().ShouldTreatEmptyElementAsString();
                }
                else
                {
                    options.Indent();
                }
            });

            var xml = serializer.Serialize(instance);
            var roundTripInstance = serializer.Deserialize(xml);
            var roundTripXml      = serializer.Serialize(roundTripInstance);

            Assert.That(roundTripInstance, Has.PropertiesEqualTo(instance));
            Assert.That(roundTripXml, Is.EqualTo(xml));
        }
예제 #2
0
        public static void DoNotPreserveReferenceWhenRefPropertyIsAbsent()
        {
            string json = @"{""Child"":{""$id"":""1""},""Sibling"":{""foo"":""1""}}";
            ClassWithObjectProperty root = JsonSerializer.Deserialize <ClassWithObjectProperty>(json);

            Assert.IsType <JsonElement>(root.Sibling);

            // $ref with any escaped character shall not be treated as metadata, hence Sibling must be JsonElement.
            json = @"{""Child"":{""$id"":""1""},""Sibling"":{""\\u0024ref"":""1""}}";
            root = JsonSerializer.Deserialize <ClassWithObjectProperty>(json);
            Assert.IsType <JsonElement>(root.Sibling);
        }
예제 #3
0
        public async Task PreserveReferenceOfTypeObject()
        {
            var root = new ClassWithObjectProperty();

            root.Child   = new ClassWithObjectProperty();
            root.Sibling = root.Child;

            Assert.Same(root.Child, root.Sibling);

            string json = await JsonSerializerWrapperForString.SerializeWrapper(root, s_serializerOptionsPreserve);

            ClassWithObjectProperty rootCopy = await JsonSerializerWrapperForString.DeserializeWrapper <ClassWithObjectProperty>(json, s_serializerOptionsPreserve);

            Assert.Same(rootCopy.Child, rootCopy.Sibling);
        }
예제 #4
0
        public static void PreserveReferenceOfTypeObject()
        {
            var root = new ClassWithObjectProperty();

            root.Child   = new ClassWithObjectProperty();
            root.Sibling = root.Child;

            Assert.Same(root.Child, root.Sibling);

            string json = JsonSerializer.Serialize(root, s_serializerOptionsPreserve);

            ClassWithObjectProperty rootCopy = JsonSerializer.Deserialize <ClassWithObjectProperty>(json, s_serializerOptionsPreserve);

            Assert.Same(rootCopy.Child, rootCopy.Sibling);
        }
예제 #5
0
        public async Task PreserveReferenceOfTypeObjectAsync()
        {
            var root = new ClassWithObjectProperty();

            root.Child   = new ClassWithObjectProperty();
            root.Sibling = root.Child;

            Assert.Same(root.Child, root.Sibling);

            var stream = new MemoryStream();
            await JsonSerializerWrapperForStream.SerializeWrapper(stream, root, s_serializerOptionsPreserve);

            stream.Position = 0;

            ClassWithObjectProperty rootCopy = await JsonSerializer.DeserializeAsync <ClassWithObjectProperty>(stream, s_serializerOptionsPreserve);

            Assert.Same(rootCopy.Child, rootCopy.Sibling);
        }
예제 #6
0
        public async Task PreserveReferenceOfTypeObjectAsync()
        {
            if (StreamingSerializer is null)
            {
                return;
            }

            var root = new ClassWithObjectProperty();

            root.Child   = new ClassWithObjectProperty();
            root.Sibling = root.Child;

            Assert.Same(root.Child, root.Sibling);

            string json = await StreamingSerializer.SerializeWrapper(root, s_serializerOptionsPreserve);

            ClassWithObjectProperty rootCopy = await StreamingSerializer.DeserializeWrapper <ClassWithObjectProperty>(json, s_serializerOptionsPreserve);

            Assert.Same(rootCopy.Child, rootCopy.Sibling);
        }