Exemple #1
0
        public void JsonReaderExtensions_Basics()
        {
            V1.Person result;

            // Verify 'null' allowed (no setters set)
            result = JsonRoundTrip.Parse <V1.Community, V1.Person>("null", V1.JsonToPerson.Read);
            Assert.Null(result);

            // Verify empty allowed (no setters set)
            result = JsonRoundTrip.Parse <V1.Community, V1.Person>("{ }", V1.JsonToPerson.Read);
            Assert.Equal(new V1.Person(), result);

            // Verify some properties may be ommitted
            result = JsonRoundTrip.Parse <V1.Community, V1.Person>("{ \"name\": \"Scott\" }", V1.JsonToPerson.Read);
            Assert.Equal("Scott", result.Name);
            Assert.Equal(new V1.Person().Age, result.Age);

            // Verify Read/Write happy path
            V1.Person p = new V1.Person()
            {
                Name = "Scott", Age = 39
            };
            JsonRoundTrip.ValueOnly(p, V1.JsonToPerson.Write, (r, db) => V1.JsonToPerson.Read(r));

            // Verify ReadObject skips unknown Property Names when configured to (see V2 Model postReplacements)
            JsonRoundTrip.Parse <V2.Community, V2.Person>("{ \"name\": \"Scott\", \"age\": 39 }", V2.JsonToPerson.Read);

            if (!Debugger.IsAttached)
            {
                // Verify ReadObject validates that JSON is an object
                Assert.Throws <JsonReaderException>(() => JsonRoundTrip.Parse <V1.Community, V1.Person>("[ \"Scott\" ])", V1.JsonToPerson.Read));

                // Verify ReadObject validates that JSON object is closed properly
                Assert.Throws <JsonReaderException>(() => JsonRoundTrip.Parse <V1.Community, V1.Person>("{ \"name\": \"Scott\" ", V1.JsonToPerson.Read));

                // Verify ReadObject throws for unknown Property Names
                Assert.Throws <JsonReaderException>(() => JsonRoundTrip.Parse <V1.Community, V1.Person>("{ \"name\": \"Scott\", \"birthdate\": \"2000-01-01\" }", V1.JsonToPerson.Read));

                // Verify Expect/Throw handles non-JsonTextReader
                Assert.Throws <JsonReaderException>(() => JsonRoundTrip.Bson_ValueOnly(p, V1.JsonToPerson.Write, (r, db) =>
                {
                    r.Expect(JsonToken.StartArray);
                    return(null);
                }));
            }
        }
Exemple #2
0
        public void Table_Basics()
        {
            V1.Community other = new V1.Community();
            V1.Community v1    = new V1.Community();

            v1.People = new List <Person>();
            IList <Person> people = v1.People;

            // Add item already in correct instance
            people.Add(new V1.Person(v1)
            {
                Age = 39, Name = "Scott"
            });
            Assert.Equal("Scott", people[0].Name);

            // Add item copied from other instance
            people.Add(new V1.Person(other)
            {
                Age = 36, Name = "Adam"
            });
            Assert.Equal("Adam", people[1].Name);

            // Try setter from other DB
            V1.Person dave = new V1.Person(other)
            {
                Age = 45, Name = "Dave"
            };
            Assert.Equal(2, people.Count);
            people[1] = dave;
            Assert.Equal(2, people.Count);
            Assert.Equal("Dave", people[1].Name);

            // Set to already correct instance
            v1.People[1] = v1.People[1];

            // Set and verify null
            v1.People[1] = null;
            Assert.Null(v1.People[1]);
        }