Пример #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);
        }