public void JsonToDateTime_Basics() { // BSOA DateTime format is precise to the millisecond only DateTime now = ToMillisecond(DateTime.UtcNow); JsonRoundTrip.ValueOnly(now, JsonToDateTime.Write, JsonToDateTime.Read); JsonRoundTrip.NameAndValue(now, DateTime.MinValue, JsonToDateTime.Write, JsonToDateTime.Read); JsonRoundTrip.NameAndValue(ToMillisecond(DateTime.MinValue.ToUniversalTime()), now, JsonToDateTime.Write, JsonToDateTime.Read); JsonRoundTrip.NameAndValue(ToMillisecond(DateTime.MaxValue.ToUniversalTime()), now, JsonToDateTime.Write, JsonToDateTime.Read); DateTime nonStandardFormat = JsonRoundTrip.Parse <Database, DateTime>("\"01/01/2020\"", JsonToDateTime.Read); Assert.Equal(new DateTime(2020, 01, 01, 00, 00, 00, DateTimeKind.Utc), nonStandardFormat); }
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); })); } }
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); })); } }