Beispiel #1
0
        public void registered_specialization_triggers_overridable_behavior()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(JsonStringParseSupport), typeof(ITestBaseClassOnly), typeof(IPocoAllOfThem));;

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var root = s.GetRequiredService <IPocoFactory <ITestBaseClassOnly> >().Create();

            root.Person = new Student("Sartre", 3712);

            // The Person is not IsFinal: the actual Student is serialized.
            // Here, the serialization relies on ToString() that is virtual (so everything works fine).
            root.ToString().Should().Be("{\"person\":[\"CS:CP\",\"Sartre|3712\"]}");

            // And the deserialization, based on the type name, calls Student.Parse.
            var root2 = JsonTestHelper.Roundtrip(directory, root);

            // Testing Intern.
            root2.Person = new Intern("Spi", "Newbie", null);
            var root3 = JsonTestHelper.Roundtrip(directory, root2);

            root3.Should().BeEquivalentTo(root2);
            root3.Should().NotBeEquivalentTo(root);
        }
        public void IPoco_types_properties_as_known_Collections_are_supported()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoWithObject), typeof(IPocoWithDictionary));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            // IPocoWithDictionary brings Dictionary<string, int> and Dictionary<int,string>.
            var f = s.GetRequiredService <IPocoFactory <IPocoWithObject> >();
            var a = f.Create(a =>
            {
                a.Value = new Dictionary <int, string>()
                {
                    { 1, "One" }, { 2, "Two" }, { 3, "Three" }
                };
            });
            var a2 = JsonTestHelper.Roundtrip(directory, a);

            a2.Should().BeEquivalentTo(a);

            a.Value = new Dictionary <string, int>()
            {
                { "One", 1 }, { "Two", 2 }, { "Three", 3 }
            };
            JsonTestHelper.Roundtrip(directory, a).Should().BeEquivalentTo(a);
        }
Beispiel #3
0
        public void simple_poco_serialization_using_the_JsonSerializer()
        {
            var oI = new IndependentObject()
            {
                Name = "Doe", Status = Status.Two
            };

            oI.Sub = new List <SubObject>()
            {
                new SubObject()
                {
                    Name = "S", X = 37, Y = 12
                }
            };

            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(ITest));;

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var f  = s.GetRequiredService <IPocoFactory <ITest> >();
            var o  = f.Create(o => { o.Obj = oI; });
            var o2 = JsonTestHelper.Roundtrip(directory, o);

            o2.Should().BeEquivalentTo(o);
        }
        public void Set_serialization(PocoJsonSerializerMode mode)
        {
            using var logGroup = TestHelper.Monitor.OpenInfo($"{nameof(Set_serialization)}-{mode}");

            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(ITestSetNumbers));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var f = s.GetRequiredService <IPocoFactory <ITestSetNumbers> >();
            var o = f.Create(o =>
            {
                o.Power = 3712;
                o.Hip  += "CodeGen!";
                o.Numbers.AddRangeArray(12, 87, 12, 54, 12);
            });
            var o2 = JsonTestHelper.Roundtrip(directory, o, new PocoJsonSerializerOptions {
                Mode = mode
            }, text: t => TestHelper.Monitor.Info(t));

            Debug.Assert(o2 != null);
            o2.Power.Should().Be(o.Power);
            o2.Hip.Should().Be(o.Hip);
            o2.Numbers.Should().BeEquivalentTo(o.Numbers);
        }
        public void recursive_poco_properties_throw_InvalidOperationException_by_JsonWriter()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoWithGeneric));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var fG = s.GetRequiredService <IPocoFactory <IPocoWithGeneric> >();

            var g1 = fG.Create();
            var g2 = fG.Create();
            var g3 = fG.Create();

            g1.OnePoco = g2;
            g2.OnePoco = g3;

            var gCool = JsonTestHelper.Roundtrip(directory, g1);

            g2.AnotherOnePoco = g3;
            var gDup = JsonTestHelper.Roundtrip(directory, g2);

            gDup.AnotherOnePoco.Should().NotBeNull().And.NotBeSameAs(gDup.OnePoco);
            gDup.OnePoco.Should().NotBeNull();

            g2.AnotherOnePoco = g1;
            g1.Invoking(fail => JsonTestHelper.Roundtrip(directory, fail)).Should().Throw <InvalidOperationException>();

            g2.AnotherOnePoco = g2;
            g1.Invoking(fail => JsonTestHelper.Roundtrip(directory, fail)).Should().Throw <InvalidOperationException>();
        }
        public void all_basic_types_roundtrip(PocoJsonSerializerMode mode)
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IAllBasicTypes));;

            using var services = TestHelper.CreateAutomaticServices(c).Services;
            var directory = services.GetRequiredService <PocoDirectory>();

            var nMax = services.GetRequiredService <IPocoFactory <IAllBasicTypes> >().Create();

            nMax.PByte           = Byte.MaxValue;
            nMax.PSByte          = SByte.MaxValue;
            nMax.PShort          = Int16.MaxValue;
            nMax.PUShort         = UInt16.MaxValue;
            nMax.PInteger        = Int32.MaxValue;
            nMax.PUInteger       = UInt32.MaxValue;
            nMax.PLong           = Int64.MaxValue;
            nMax.PULong          = UInt64.MaxValue;
            nMax.PFloat          = Single.MaxValue;
            nMax.PDouble         = Double.MaxValue;
            nMax.PDecimal        = Decimal.MaxValue;
            nMax.PBigInteger     = BigInteger.Parse("12345678901234567890123456789012345678901234567890123456789012345678901234567890", System.Globalization.NumberFormatInfo.InvariantInfo);
            nMax.PDateTime       = Util.UtcMaxValue;
            nMax.PDateTimeOffset = DateTimeOffset.MaxValue;
            nMax.PTimeSpan       = TimeSpan.MaxValue;
            nMax.PGuid           = Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff");

            var nMin = services.GetRequiredService <IPocoFactory <IAllBasicTypes> >().Create();

            nMin.PByte           = Byte.MinValue;
            nMin.PSByte          = SByte.MinValue;
            nMin.PShort          = Int16.MinValue;
            nMin.PUShort         = UInt16.MinValue;
            nMin.PInteger        = Int32.MinValue;
            nMin.PUInteger       = UInt32.MinValue;
            nMin.PLong           = Int64.MinValue;
            nMin.PULong          = UInt64.MinValue;
            nMin.PFloat          = Single.MinValue;
            nMin.PDouble         = Double.MinValue;
            nMin.PDecimal        = Decimal.MinValue;
            nMin.PBigInteger     = BigInteger.Parse("-12345678901234567890123456789012345678901234567890123456789012345678901234567890", System.Globalization.NumberFormatInfo.InvariantInfo);
            nMin.PDateTime       = Util.UtcMinValue;
            nMin.PDateTimeOffset = DateTimeOffset.MinValue;
            nMin.PTimeSpan       = TimeSpan.MinValue;
            nMin.PGuid           = Guid.Empty;

            var options = new PocoJsonSerializerOptions {
                Mode = mode
            };

            var nMax2 = JsonTestHelper.Roundtrip(directory, nMax, options, text: t => TestHelper.Monitor.Info($"IAllBasicTypes(max) serialization: " + t));

            nMax2.Should().BeEquivalentTo(nMax);

            var nMin2 = JsonTestHelper.Roundtrip(directory, nMin, options, text: t => TestHelper.Monitor.Info($"IAllBasicTypes(min) serialization: " + t));

            nMin2.Should().BeEquivalentTo(nMin);
        }
        public void simple_poco_serialization()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(ITest));;

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var f  = s.GetRequiredService <IPocoFactory <ITest> >();
            var o  = f.Create(o => { o.Power = 3712; o.Hip += "CodeGen!"; });
            var o2 = JsonTestHelper.Roundtrip(directory, o);

            o2.Power.Should().Be(o.Power);
            o2.Hip.Should().Be(o.Hip);
        }
        public void null_poco_is_handled()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(ITest));;

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            ITest?nullPoco = null;

            JsonTestHelper.Roundtrip(directory, nullPoco).Should().BeNull();

            IPoco?nullUnknwonPoco = null;

            JsonTestHelper.Roundtrip(directory, nullUnknwonPoco).Should().BeNull();
        }
        public void list_of_list_serialization()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IListOfList));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var f  = s.GetRequiredService <IPocoFactory <IListOfList> >();
            var oD = f.Create(o => { o.List.Add(new List <int> {
                    1, 2
                }); });
            var oD2 = JsonTestHelper.Roundtrip(directory, oD);

            oD2.List[0].Should().HaveCount(2);
        }
        public void basic_types_properties_as_Object_are_supported()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoWithObject));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var f = s.GetRequiredService <IPocoFactory <IPocoWithObject> >();
            var a = f.Create(a =>
            {
                a.Value = 1;
            });
            var a2 = JsonTestHelper.Roundtrip(directory, a);

            a2.Should().BeEquivalentTo(a);
        }
        public void dictionary_with_a_string_key_is_a_Json_object_otherwise_it_is_an_array_of_2_cells_array()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoWithDictionary));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var fA = s.GetRequiredService <IPocoFactory <IPocoWithDictionary> >();
            var a  = fA.Create(a =>
            {
                a.Map.Add(1, "Toto");
                a.ClassicalJson.Add("key", 3712);
            });
            var a2 = JsonTestHelper.Roundtrip(directory, a);

            a2.Should().BeEquivalentTo(a);
        }
        public void property_poco_serialization()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoA), typeof(IPocoB), typeof(IPocoC));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var fA = s.GetRequiredService <IPocoFactory <IPocoA> >();
            var a  = fA.Create(a => { a.B2.ValB = "B2"; a.B2.C.ValC = "B2.C"; });

            var a2 = JsonTestHelper.Roundtrip(directory, a);

            Debug.Assert(a2 != null);
            a2.B2.ValB.Should().Be("B2");
            a2.B2.C.ValC.Should().Be("B2.C");
            a2.Should().BeEquivalentTo(a);
        }
Beispiel #13
0
        public void when_a_specialization_is_not_registered_the_known_static_Type_drives_and_this_may_not_be_good()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(JsonStringParseSupport), typeof(ITestBaseClassOnly));;

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var root = s.GetRequiredService <IPocoFactory <ITestBaseClassOnly> >().Create();

            root.Person = new Student("Sartre", 3712);

            // Here, the serialization relies on ToString() that is virtual: the Student is serialized (it shouldn't!).
            root.ToString().Should().Be("{\"person\":\"Sartre|3712\"}");

            // But the deserialization calls Person.Parse.
            FluentActions.Invoking(() => JsonTestHelper.Roundtrip(directory, root)).Should().Throw <ArgumentException>().Where(ex => ex.Message.StartsWith("Invalid | in name.", StringComparison.OrdinalIgnoreCase));
        }
        public void IPoco_types_properties_must_be_known()
        {
            // Here we don't add IPocoWithDictionary: Dictionary<string, int> and Dictionary<int,string> are not registered.
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoWithObject));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();
            var f         = s.GetRequiredService <IPocoFactory <IPocoWithObject> >();
            var a         = f.Create(a =>
            {
                a.Value = new Dictionary <int, string>()
                {
                    { 1, "One" }, { 2, "Two" }, { 3, "Three" }
                };
            });

            a.Invoking(x => JsonTestHelper.Roundtrip(directory, x)).Should().Throw <JsonException>();
        }
        public void cross_poco_serialization_with_specialization()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoCrossASpec), typeof(IPocoCrossB));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var fA = s.GetRequiredService <IPocoFactory <IPocoCrossA> >();
            var a  = fA.Create(a =>
            {
                a.B.MsgB = "From A.B.";
            });

            var a2 = JsonTestHelper.Roundtrip(directory, a);

            Debug.Assert(a2 != null);
            a2.B.MsgB.Should().Be("From A.B.");

            a.B.A = a;
            a.Invoking(fail => JsonTestHelper.Roundtrip(directory, fail)).Should().Throw <InvalidOperationException>();
        }
Beispiel #16
0
        public void extending_json_serialization()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(JsonStringParseSupport), typeof(IPocoNoIntern));;

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var root = s.GetRequiredService <IPocoFactory <IPocoNoIntern> >().Create();

            root.Person  = new Person("Jean");
            root.Teacher = new Teacher("Paul", "Aggreg");
            root.Student = new Student("Sartre", 3712);

            var root2 = JsonTestHelper.Roundtrip(directory, root, text: t =>
            {
                TestHelper.Monitor.Info($"ITest serialization: " + t);
                t.Should().Be(@"[""NoIntern"",{""person"":[""CP"",""Jean""],""teacher"":""Paul|Aggreg"",""student"":""Sartre|3712""}]");
            });

            root2.Should().BeEquivalentTo(root);
        }
Beispiel #17
0
        public void enum_serialization()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(ITest));;

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var f  = s.GetRequiredService <IPocoFactory <ITest> >();
            var o  = f.Create(o => { o.Working = Code.Pending; o.NullableWorking = Code.Working; o.Result = Code.None; });
            var o2 = JsonTestHelper.Roundtrip(directory, o);

            Debug.Assert(o2 != null);
            o2.Working.Should().Be(Code.Pending);
            o2.NullableWorking.Should().Be(Code.Working);
            o2.Result.Should().Be(Code.None);

            o.NullableWorking = null;
            o2 = JsonTestHelper.Roundtrip(directory, o);
            o2.Working.Should().Be(Code.Pending);
            o2.NullableWorking.Should().BeNull();
            o2.Result.Should().Be(Code.None);
        }
        public void IPoco_types_properties_as_Object_are_supported()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoWithObject), typeof(IPocoWithDictionary));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var f  = s.GetRequiredService <IPocoFactory <IPocoWithObject> >();
            var fO = s.GetRequiredService <IPocoFactory <IPocoWithDictionary> >();
            var o  = fO.Create(o =>
            {
                o.ClassicalJson.Add("Name", 712);
                o.Map[3712] = "Hop";
                o.Map[0]    = "Zero";
            });
            var a = f.Create(a =>
            {
                a.Value = o;
            });
            var a2 = JsonTestHelper.Roundtrip(directory, a);

            a2.Should().BeEquivalentTo(a);
        }
        public void generic_property_poco_serialization()
        {
            var c = TestHelper.CreateStObjCollector(typeof(PocoJsonSerializer), typeof(IPocoWithGeneric), typeof(IPocoA), typeof(IPocoB), typeof(IPocoC));

            using var s = TestHelper.CreateAutomaticServices(c).Services;
            var directory = s.GetRequiredService <PocoDirectory>();

            var fG = s.GetRequiredService <IPocoFactory <IPocoWithGeneric> >();
            var fA = s.GetRequiredService <IPocoFactory <IPocoA> >();

            var g = fG.Create();

            var gWithNull = JsonTestHelper.Roundtrip(directory, g);

            gWithNull.OnePoco.Should().BeNull();
            gWithNull.AnotherOnePoco.Should().BeNull();

            g.OnePoco = fA.Create();
            var gWithA = JsonTestHelper.Roundtrip(directory, g);

            gWithA.OnePoco.Should().NotBeNull();
            gWithA.AnotherOnePoco.Should().BeNull();
        }