public void JsonToPerson_Basics() { Community readRoot = new Community(); Person p = new Person() { Name = "Scott", Birthdate = new DateTime(1981, 01, 01, 00, 00, 00, DateTimeKind.Utc) }; // Serialization via typed methods JsonRoundTrip.ValueOnly(p, JsonToPerson.Write, (r, db) => JsonToPerson.Read(r, readRoot)); JsonRoundTrip.ValueOnly(null, JsonToPerson.Write, (r, db) => JsonToPerson.Read(r, readRoot)); JsonRoundTrip.NameAndValue(p, null, (w, pn, v, dv, req) => JsonToPerson.Write(w, pn, v, req), (r, db) => JsonToPerson.Read(r, readRoot)); // JsonConverter.CanConvert (not called by default serialization) JsonToPerson converter = new JsonToPerson(); Assert.True(converter.CanConvert(typeof(Person))); Assert.False(converter.CanConvert(typeof(Community))); // Serialization via Newtonsoft default string personPath = "Person.NewtonsoftDefault.json"; AsJson.Save(personPath, p, true); Person roundTrip = AsJson.Load <Person>(personPath); Assert.Equal(p, roundTrip); // Serialize null via Newtonsoft AsJson.Save <Person>(personPath, null); roundTrip = AsJson.Load <Person>(personPath); Assert.Null(roundTrip); // Serialize empty root string communityPath = "Community.NewtonsoftDefault.json"; AsJson.Save(communityPath, readRoot); Community roundTripCommunity = AsJson.Load <Community>(communityPath); Assert.Null(roundTripCommunity.People); // Serialize root with Person readRoot.People = new List <Person>(); readRoot.People.Add(p); AsJson.Save(communityPath, readRoot); roundTripCommunity = AsJson.Load <Community>(communityPath); Assert.Single(roundTripCommunity.People); Assert.Equal(p, roundTripCommunity.People[0]); Assert.Equal("Scott", roundTripCommunity.People[0].Name); }
public void JsonReaderExtensions_Basics() { Person result; // Verify 'null' allowed (no setters set) result = JsonRoundTrip.Parse <Community, Person>("null", JsonToPerson.Read); Assert.Null(result); // Verify empty allowed (no setters set) result = JsonRoundTrip.Parse <Community, Person>("{ }", JsonToPerson.Read); Assert.Equal(new Person(), result); // Verify some properties may be ommitted result = JsonRoundTrip.Parse <Community, Person>("{ \"name\": \"Scott\" }", JsonToPerson.Read); Assert.Equal("Scott", result.Name); Assert.Equal(new Person().Birthdate, result.Birthdate); // Verify Read/Write happy path Person p = new Person() { Name = "Scott", Birthdate = new DateTime(1981, 01, 01).ToUniversalTime() }; JsonRoundTrip.ValueOnly(p, JsonToPerson.Write, (r, db) => JsonToPerson.Read(r)); if (!Debugger.IsAttached) { // Verify ReadObject validates that JSON is an object Assert.Throws <JsonReaderException>(() => JsonRoundTrip.Parse <Community, Person>("[ \"Scott\" ])", JsonToPerson.Read)); // Verify ReadObject validates that JSON object is closed properly Assert.Throws <JsonReaderException>(() => JsonRoundTrip.Parse <Community, Person>("{ \"name\": \"Scott\" ", JsonToPerson.Read)); // Verify ReadObject throws for unknown Property Names Assert.Throws <JsonReaderException>(() => JsonRoundTrip.Parse <Community, Person>("{ \"name\": \"Scott\", \"age\": 39 }", JsonToPerson.Read)); // Verify Expect/Throw handles non-JsonTextReader Assert.Throws <JsonReaderException>(() => JsonRoundTrip.Bson_ValueOnly(p, JsonToPerson.Write, (r, db) => { r.Expect(JsonToken.StartArray); return(null); })); } }