Пример #1
0
        public void JsonToBool_Basics()
        {
            JsonRoundTrip.ValueOnly(true, JsonToBool.Write, JsonToBool.Read);

            JsonRoundTrip.NameAndValue(true, false, JsonToBool.Write, JsonToBool.Read <Database>);
            JsonRoundTrip.NameAndValue(false, true, JsonToBool.Write, JsonToBool.Read <Database>);
        }
Пример #2
0
        public void JsonToNumbers_Basics()
        {
            JsonRoundTrip.ValueOnly((byte)4, JsonToByte.Write, JsonToByte.Read);
            JsonRoundTrip.NameAndValue(byte.MaxValue, byte.MinValue, JsonToByte.Write, JsonToByte.Read);

            JsonRoundTrip.ValueOnly((sbyte)4, JsonToSbyte.Write, JsonToSbyte.Read);
            JsonRoundTrip.NameAndValue(sbyte.MaxValue, sbyte.MinValue, JsonToSbyte.Write, JsonToSbyte.Read);

            JsonRoundTrip.ValueOnly((short)-4, JsonToShort.Write, JsonToShort.Read);
            JsonRoundTrip.NameAndValue(short.MaxValue, short.MinValue, JsonToShort.Write, JsonToShort.Read);

            JsonRoundTrip.ValueOnly((ushort)4, JsonToUshort.Write, JsonToUshort.Read);
            JsonRoundTrip.NameAndValue(ushort.MaxValue, ushort.MinValue, JsonToUshort.Write, JsonToUshort.Read);

            JsonRoundTrip.ValueOnly(int.MaxValue / 2, JsonToInt.Write, JsonToInt.Read);
            JsonRoundTrip.NameAndValue(int.MaxValue, int.MinValue, JsonToInt.Write, JsonToInt.Read);

            JsonRoundTrip.ValueOnly((uint)4, JsonToUint.Write, JsonToUint.Read);
            JsonRoundTrip.NameAndValue(uint.MaxValue, uint.MinValue, JsonToUint.Write, JsonToUint.Read);

            JsonRoundTrip.ValueOnly((long.MinValue / 2), JsonToLong.Write, JsonToLong.Read);
            JsonRoundTrip.NameAndValue(long.MaxValue, long.MinValue, JsonToLong.Write, JsonToLong.Read);

            JsonRoundTrip.ValueOnly((ulong)4, JsonToUlong.Write, JsonToUlong.Read);
            JsonRoundTrip.NameAndValue(ulong.MaxValue, ulong.MinValue, JsonToUlong.Write, JsonToUlong.Read);

            JsonRoundTrip.ValueOnly(3.14f, JsonToFloat.Write, JsonToFloat.Read);
            JsonRoundTrip.NameAndValue(float.MaxValue, float.MinValue, JsonToFloat.Write, JsonToFloat.Read);

            JsonRoundTrip.ValueOnly(2.16789480d, JsonToDouble.Write, JsonToDouble.Read);
            JsonRoundTrip.NameAndValue(double.MaxValue, double.MinValue, JsonToDouble.Write, JsonToDouble.Read);
        }
Пример #3
0
        public void JsonToString_Basics()
        {
            JsonRoundTrip.ValueOnly(null, JsonToString.Write, JsonToString.Read);
            JsonRoundTrip.ValueOnly("", JsonToString.Write, JsonToString.Read);
            JsonRoundTrip.ValueOnly("simple", JsonToString.Write, JsonToString.Read);
            JsonRoundTrip.ValueOnly("'\"\\\"'", JsonToString.Write, JsonToString.Read);

            JsonRoundTrip.NameAndValue("Simple", null, JsonToString.Write, JsonToString.Read);
            JsonRoundTrip.NameAndValue(null, "NonNullDefault", JsonToString.Write, JsonToString.Read);
        }
Пример #4
0
        public void JsonToEnum_Basics()
        {
            JsonRoundTrip.ValueOnly(DayOfWeek.Thursday, JsonToEnum <DayOfWeek> .Write, JsonToEnum <DayOfWeek> .Read);

            JsonRoundTrip.NameAndValue(DayOfWeek.Thursday, DayOfWeek.Sunday, JsonToEnum <DayOfWeek> .Write, JsonToEnum <DayOfWeek> .Read);
            JsonRoundTrip.NameAndValue(DayOfWeek.Wednesday, DayOfWeek.Thursday, JsonToEnum <DayOfWeek> .Write, JsonToEnum <DayOfWeek> .Read);

            // Flags enum
            JsonRoundTrip.NameAndValue(FileAttributes.ReadOnly | FileAttributes.Directory, default(FileAttributes), JsonToEnum <FileAttributes> .Write, JsonToEnum <FileAttributes> .Read);
        }
Пример #5
0
        public void JsonToIDictionary_Basics()
        {
            Action <JsonWriter, IDictionary <string, string> > writeValueOnly = (w, v) => JsonToIDictionary <string, string> .Write(w, v, JsonToString.Write);

            Action <JsonWriter, string, IDictionary <string, string>, IDictionary <string, string>, bool> writeNameAndValue = (w, pn, v, dv, r) => JsonToIDictionary <string, string> .Write(w, pn, v, JsonToString.Write, r);

            // Dictionaries can be read by taking the return value or passing a Dictionary to fill as the argument
            Func <JsonReader, Database, IDictionary <string, string> > readViaReturnValue = (r, db) => JsonToIDictionary <string, string> .Read(r, db, null, JsonToString.Read);

            Func <JsonReader, Database, IDictionary <string, string> > readViaArgument = (r, db) =>
            {
                if (r.TokenType == JsonToken.Null)
                {
                    return(null);
                }
                IDictionary <string, string> result = new Dictionary <string, string>();
                JsonToIDictionary <string, string> .Read(r, db, result, JsonToString.Read);

                return(result);
            };

            Dictionary <string, string> empty  = new Dictionary <string, string>();
            Dictionary <string, string> single = new Dictionary <string, string>()
            {
                ["Name"] = "single"
            };

            // Test null(only readable via return value, as argument must be initialized to be passed)
            JsonRoundTrip.ValueOnly(null, writeValueOnly, readViaReturnValue);
            JsonRoundTrip.NameAndValue(single, null, writeNameAndValue, readViaReturnValue);

            // Test empty (must be padded to RoundTrip as default, so underlying code knows to expect no output written)
            JsonRoundTrip.ValueOnly(empty, writeValueOnly, readViaReturnValue);
            JsonRoundTrip.ValueOnly(empty, writeValueOnly, readViaArgument);
            JsonRoundTrip.NameAndValue(single, empty, writeNameAndValue, readViaReturnValue);
            JsonRoundTrip.NameAndValue(single, empty, writeNameAndValue, readViaArgument);

            // Single element array
            JsonRoundTrip.ValueOnly(single, writeValueOnly, readViaReturnValue);
            JsonRoundTrip.ValueOnly(single, writeValueOnly, readViaArgument);

            // Multiple element array
            Dictionary <string, string> several = new Dictionary <string, string>()
            {
                ["Null"]   = null,
                ["Empty"]  = "",
                ["Simple"] = "Value"
            };

            JsonRoundTrip.ValueOnly(several, writeValueOnly, readViaReturnValue);
            JsonRoundTrip.ValueOnly(several, writeValueOnly, readViaArgument);
            JsonRoundTrip.NameAndValue(several, null, writeNameAndValue, readViaReturnValue);
            JsonRoundTrip.NameAndValue(several, null, writeNameAndValue, readViaArgument);
        }
Пример #6
0
        public void JsonToIList_Basics()
        {
            Action <JsonWriter, IList <int> > writeValueOnly = (w, v) => JsonToIList <int> .Write(w, v, JsonToInt.Write);

            Action <JsonWriter, string, IList <int>, IList <int>, bool> writeNameAndValue = (w, pn, v, dv, r) => JsonToIList <int> .Write(w, pn, v, JsonToInt.Write, r);

            // Lists can be read either by taking the return value, or by passing a list to fill as the argument
            Func <JsonReader, Database, IList <int> > readViaReturnValue = (r, db) => JsonToIList <int> .Read(r, db, null, JsonToInt.Read);

            Func <JsonReader, Database, IList <int> > readViaArgument = (r, db) =>
            {
                if (r.TokenType == JsonToken.Null)
                {
                    return(null);
                }
                IList <int> result = new List <int>();
                JsonToIList <int> .Read(r, db, result, JsonToInt.Read);

                return(result);
            };

            List <int> empty  = new List <int>();
            List <int> single = new List <int>()
            {
                -55
            };

            // Test null (only readable via return value, as argument must be initialized to be passed)
            JsonRoundTrip.ValueOnly(null, writeValueOnly, readViaReturnValue);
            JsonRoundTrip.NameAndValue(single, null, writeNameAndValue, readViaReturnValue);

            // Test empty (must be padded to RoundTrip as default, so underlying code knows to expect no output written)
            JsonRoundTrip.ValueOnly(empty, writeValueOnly, readViaReturnValue);
            JsonRoundTrip.ValueOnly(empty, writeValueOnly, readViaArgument);
            JsonRoundTrip.NameAndValue(single, empty, writeNameAndValue, readViaReturnValue);
            JsonRoundTrip.NameAndValue(single, empty, writeNameAndValue, readViaArgument);

            // Single element array
            JsonRoundTrip.ValueOnly(single, writeValueOnly, readViaReturnValue);
            JsonRoundTrip.ValueOnly(single, writeValueOnly, readViaArgument);

            // Multiple element array
            List <int> several = new List <int>()
            {
                0, 44, 0, -55
            };

            JsonRoundTrip.ValueOnly(several, writeValueOnly, readViaReturnValue);
            JsonRoundTrip.ValueOnly(several, writeValueOnly, readViaArgument);
            JsonRoundTrip.NameAndValue(several, null, writeNameAndValue, readViaReturnValue);
            JsonRoundTrip.NameAndValue(several, null, writeNameAndValue, readViaArgument);
        }
Пример #7
0
        public void JsonToUri_Basics()
        {
            JsonRoundTrip.ValueOnly(null, JsonToUri.Write, JsonToUri.Read);
            JsonRoundTrip.ValueOnly(new Uri("https://www.github.com/Microsoft/bion"), JsonToUri.Write, JsonToUri.Read);

            // Null default
            JsonRoundTrip.NameAndValue(new Uri("https://www.github.com/Microsoft/bion"), null, JsonToUri.Write, JsonToUri.Read);

            // Non-null default
            JsonRoundTrip.NameAndValue(new Uri("https://www.github.com/Microsoft/bion"), new Uri("src/Program.cs", UriKind.Relative), JsonToUri.Write, JsonToUri.Read);

            // Null when default is not null
            JsonRoundTrip.NameAndValue(null, new Uri("src/Program.cs", UriKind.Relative), JsonToUri.Write, JsonToUri.Read);
        }
Пример #8
0
        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);
        }
Пример #9
0
        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);
        }
Пример #10
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);
                }));
            }
        }
Пример #11
0
        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);
                }));
            }
        }