예제 #1
0
        public void TestJsoninstrCourse()
        {
            Course expected = new Course
                              (
                "AVE",
                new Student[4] {
                new Student(27721, "Ze Manel"),
                new Student(15642, "Maria Papoila"),
                null,
                null
            }
                              );

            /*
             * O resultado de ToJson(expected) deve ser igual à string json abaixo
             */
            string json = Jsoninstr.ToJson(expected);

            /* string json = "{" +
             *   "\"name\":\"AVE\"," +
             *   "\"stds\":" +
             *       "[" +
             *           "{\"nr\":27721,\"name\":\"Ze Manel\"}," +
             *           "{\"nr\":15642,\"name\":\"Maria Papoila\"}," +
             *           "null," +
             *           "null" +
             *       "]" +
             *   "}";*/
            Course actual = JsonConvert.DeserializeObject <Course>(json);

            Assert.AreEqual(expected, actual);
        }
예제 #2
0
        public void TestChar()
        {
            char   c    = 'c';
            String json = Jsoninstr.ToJson(c);

            Assert.AreEqual("\"c\"", json);
        }
예제 #3
0
        public void TestEmmitVsReflectionWithFieldsForStudent()
        {
            double durationEmmit = double.MaxValue;

            for (int i = 0; i < 15; i++)
            {
                Stopwatch time = new Stopwatch();
                time.Start();
                Jsoninstr.SerializeType(new Student(3, "Ramiro Jorge"), new FilterByField());
                Jsoninstr.SerializeType(new Student(5, "Ramiro Jorge test 2"), new FilterByField());
                double test = time.Elapsed.TotalMilliseconds;
                durationEmmit = test < durationEmmit ? test : durationEmmit;
            }

            double durationReflection = double.MaxValue;

            for (int i = 0; i < 15; i++)
            {
                Stopwatch time = new Stopwatch();
                time.Start();
                Jsonfier.ToJson(new Student(27721, "Ze Manel"), new FilterByField());
                Jsonfier.ToJson(new Student(15642, "Maria Papoila"), new FilterByField());
                double test = time.Elapsed.TotalMilliseconds;
                durationReflection = test < durationReflection ? test : durationReflection;
            }
            if (durationEmmit < durationReflection)
            {
                Assert.AreNotEqual(durationEmmit, durationReflection);
            }
            else
            {
                Assert.AreEqual(durationEmmit, durationReflection);
            }
        }
예제 #4
0
        public void TestString()
        {
            String initial  = "Demo";
            string json     = Jsoninstr.ToJson(initial);
            String expected = "\"Demo\"";

            Assert.AreEqual(expected, json);
        }
예제 #5
0
        public void TestNull()
        {
            String expected = "null";

            System.Diagnostics.Debug.Write(typeof(int));
            string json = Jsoninstr.ToJson(null);

            Assert.AreEqual(expected, json);
        }
예제 #6
0
        public void TestDateTime()
        {
            DateTime expected = new DateTime();

            String json = Jsoninstr.ToJson(expected);

            DateTime actual = JsonConvert.DeserializeObject <DateTime>(json);

            Assert.AreEqual(expected, actual);
        }
예제 #7
0
        public void TestJsoninstrArrayDoublesPrimitives()
        {
            double[] expected = { 8.1, 4.9, 6.3, 7.7 };

            /*
             * O resultado de ToJson(expected) deve ser igual à string json abaixo
             */
            string json = Jsoninstr.ToJson(expected);

            //string json = "[4,5,6,7]";
            double[] actual = JsonConvert.DeserializeObject <double[]>(json);
            CollectionAssert.AreEqual(expected, actual);
        }
예제 #8
0
        public void TestJsoninstrArrayPrimitives()
        {
            int[] expected = { 4, 5, 6, 7 };

            /*
             * O resultado de ToJson(expected) deve ser igual à string json abaixo
             */
            string json = Jsoninstr.ToJson(expected);

            //string json = "[4,5,6,7]";
            int[] actual = JsonConvert.DeserializeObject <int[]>(json);
            CollectionAssert.AreEqual(expected, actual);
        }
예제 #9
0
        public void TestJsoninstrStudent()
        {
            Student expected = new Student(27721, "Ze Manel");

            /*
             * O resultado de ToJson(expected) deve ser igual à string json abaixo
             */
            string json = Jsoninstr.ToJson(expected);
            //string json = "{\"nr\":27721,\"name\":\"Ze Manel\"}";
            Student actual = JsonConvert.DeserializeObject <Student>(json);

            Assert.AreEqual(expected, actual);
        }
예제 #10
0
        public void TestFloatingPoints()
        {
            double d            = 2.1;
            String jsonDouble   = Jsoninstr.ToJson(d);
            double actualDouble = JsonConvert.DeserializeObject <double>(jsonDouble);

            Assert.AreEqual(d, actualDouble);

            float  single       = 5.1F;
            String jsonSingle   = Jsoninstr.ToJson(single);
            float  actualSingle = JsonConvert.DeserializeObject <float>(jsonSingle);

            Assert.AreEqual(single, actualSingle);
        }
예제 #11
0
        public static string ToJsonDictonary(object src)
        {
            if (src == null)
            {
                return("\"null\"");//return null;//Console.WriteLine("Object passed is null.");
            }
            Type t = src.GetType();

            if (t.IsPrimitive || t == typeof(string))
            {
                return(ProcessPrimitiveOrString(src));
            }

            return(t.IsArray ? Jsoninstr.GetElements(src, criteria) : Jsoninstr.SerializeType(src, criteria));
        }
예제 #12
0
        public void TestEmmitVsReflectionWithPropertyForCourse()
        {
            Course expected = new Course
                              (
                "AVE",
                new Student[4] {
                new Student(27721, "Ze Manel"),
                new Student(15642, "Maria Papoila"),
                null,
                null
            }
                              );
            double durationEmmit = double.MaxValue;

            for (int i = 0; i < 15; i++)
            {
                Stopwatch time = new Stopwatch();
                time.Start();
                Jsoninstr.SerializeType(expected, new FilterByProperty());
                double test = time.Elapsed.TotalMilliseconds;
                durationEmmit = test < durationEmmit ? test : durationEmmit;
            }

            double durationReflection = double.MaxValue;

            for (int i = 0; i < 15; i++)
            {
                Stopwatch time = new Stopwatch();
                time.Start();
                Jsonfier.ToJson(expected, new FilterByProperty());
                double test = time.Elapsed.TotalMilliseconds;
                durationReflection = test < durationReflection ? test : durationReflection;
            }
            if (durationEmmit < durationReflection)
            {
                Assert.AreNotEqual(durationEmmit, durationReflection);
            }
            else
            {
                Assert.AreEqual(durationEmmit, durationReflection);
            }
        }
예제 #13
0
        public void TestNumeric()
        {
            int    i         = int.MinValue;
            String jsonInt   = Jsoninstr.ToJson(i);
            int    actualInt = JsonConvert.DeserializeObject <int>(jsonInt);

            Assert.AreEqual(i, actualInt);

            long   l          = long.MaxValue;
            String jsonLong   = Jsoninstr.ToJson(l);
            long   actualLong = JsonConvert.DeserializeObject <long>(jsonLong);

            Assert.AreEqual(l, actualLong);

            UInt16 unsignedInt       = UInt16.MinValue;
            String jsonUnsignedInt   = Jsoninstr.ToJson(unsignedInt);
            UInt16 actualUnsignedInt = JsonConvert.DeserializeObject <UInt16>(jsonUnsignedInt);

            Assert.AreEqual(unsignedInt, actualUnsignedInt);
        }