示例#1
0
        public static void JsonArray_05_ParseLong()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.CreateJsonArray();

            Assert.NotNull(jsonArray);
            Assert.Equal("[]", jsonArray.ToString());
            jsonArray.Insert(0, 100000000000L);
            var value = jsonArray.ParseLong(0);

            Assert.Equal(100000000000L, value);
            jsonArray.Insert(1, "200000000001");
            var value2 = jsonArray.ParseLong(1);

            Assert.Equal(200000000001L, value2);
            jsonArray.Insert(1, 300000000002L);
            var value3 = jsonArray.ParseLong(1);

            Assert.Equal(300000000002, value3);
            jsonArray.Insert(1, 1);
            var value4 = jsonArray.ParseLong(1);

            Assert.Equal(1L, value4);
        }
示例#2
0
        public static void Default_6_SerializeObject_WithList()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var classList = new List <TestClass1>();
            var class1    = new TestClass1
            {
                TestBool1   = true,
                TestInt1    = 3,
                TestString1 = "test"
            };

            classList.Add(class1);
            var class2 = new TestClass1
            {
                TestInt1 = 5
            };

            classList.Add(class2);
            var result = jsonFactory.SerializeObject(classList);

            Assert.NotNull(result);
            Assert.Equal("[{\"TestBool1\":true,\"TestInt1\":3,\"TestLong1\":0,\"TestString1\":\"test\"},{\"TestBool1\":false,\"TestInt1\":5,\"TestLong1\":0,\"TestString1\":null}]", result);
        }
        public static void Default_0_GetInstance()
        {
            JsonFactory.Register <JsonFactoryImpl>();
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
        }
示例#4
0
        public void Default_6_SerializeObject_WithListOfDictionary()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var dict = new Dictionary <string, string>
            {
                { "testKey0", "testValue0" },
                { "testKey1", "testValue1" },
                { "testKey2", "testValue2" }
            };
            var dict2 = new Dictionary <string, string>
            {
                { "testKey0", "testValue3" },
                { "testKey2", "testValue4" },
                { "testKey4", "testValue5" }
            };
            var dictList = new List <Dictionary <string, string> >
            {
                dict,
                dict2
            };
            var result = jsonFactory.SerializeObject(dictList);

            _output.WriteLine("Serialized string: " + result);
            Assert.Equal("[{\"testKey0\":\"testValue0\",\"testKey1\":\"testValue1\",\"testKey2\":\"testValue2\"},{\"testKey0\":\"testValue3\",\"testKey2\":\"testValue4\",\"testKey4\":\"testValue5\"}]", result);
        }
示例#5
0
        public static void Default_7_DeserializeObject()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var class1 = new TestClass1
            {
                TestBool1   = true,
                TestInt1    = 3,
                TestLong1   = 10,
                TestString1 = "test"
            };
            var result = jsonFactory.SerializeObject(class1);

            Assert.NotNull(result);
            Assert.Equal("{\"TestBool1\":true,\"TestInt1\":3,\"TestLong1\":10,\"TestString1\":\"test\"}", result);
            var deserializedClass1 = jsonFactory.DeserializeObject <TestClass1>(result);

            Assert.Equal(class1.TestBool1, deserializedClass1.TestBool1);
            Assert.Equal(class1.TestInt1, deserializedClass1.TestInt1);
            Assert.Equal(class1.TestLong1, deserializedClass1.TestLong1);
            Assert.Equal(class1.TestString1, deserializedClass1.TestString1);

            class1 = new TestClass1
            {
                TestLong1 = 1000000000000
            };
            result             = jsonFactory.SerializeObject(class1);
            deserializedClass1 = jsonFactory.DeserializeObject <TestClass1>(result);
            Assert.Equal(class1.TestLong1, deserializedClass1.TestLong1);
        }
示例#6
0
        public static void Default_1_CreateJsonArray()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.CreateJsonArray();

            Assert.NotNull(jsonArray);
            Assert.Equal("[]", jsonArray.ToString());
        }
示例#7
0
        public static void JsonArray_00_Size()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.GetJsonArray("[]");

            Assert.NotNull(jsonArray);
            Assert.Equal(0, jsonArray.Size());
        }
示例#8
0
        public static void Default_2_CreateJsonObject()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.CreateJsonObject();

            Assert.NotNull(jsonObject);
            Assert.Equal("{}", jsonObject.ToString());
        }
示例#9
0
        public void JsonObject_18_Unicode()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.CreateJsonObject();

            Assert.NotNull(jsonObject);
            jsonObject.Put("title", "Vive视频");
            _output.WriteLine("jsonObject: " + jsonObject);
        }
示例#10
0
        public static void JsonObject_16_InsertJsonObject()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            jsonObject = jsonObject.Put("key", new LitJsonJsonFactory.LitJsonJsonObject(JsonMapper.ToObject("{}")));
            Assert.NotNull(jsonObject);
        }
示例#11
0
        public static void JsonObject_14_PutString()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            jsonObject = jsonObject.Put("key", "test");
            Assert.NotNull(jsonObject);
        }
示例#12
0
        public static void JsonObject_12_PutInt()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            jsonObject = jsonObject.Put("key", 1);
            Assert.NotNull(jsonObject);
        }
示例#13
0
        public static void JsonObject_09_PutBool()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            jsonObject = jsonObject.Put("key", true);
            Assert.NotNull(jsonObject);
        }
        public static void Default_5_GetJsonObject()
        {
            JsonFactory.Register <JsonFactoryImpl>();
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            Assert.Equal("{}", jsonObject.ToString());
        }
示例#15
0
            internal bool SaveToFile(Dictionary <string, string> properties)
            {
                if (properties == null)
                {
                    return(false);
                }
                if (string.IsNullOrWhiteSpace(_path))
                {
                    return(false);
                }
                var jsonFactory = JsonFactory.GetInstance();
                var jsonObject  = jsonFactory.CreateJsonObject();

                foreach (var k in properties.Keys)
                {
                    jsonObject.Put(k, properties[k]);
                }

                try
                {
                    var file = new FileInfo(_path);
                    if (!file.Exists)
                    {
                        var directory = file.Directory;
                        if (directory == null)
                        {
                            return(false);
                        }

                        if (!directory.Exists)
                        {
                            directory.Create();
                        }
                    }

                    using (var fileStream = File.OpenWrite(file.FullName))
                    {
                        var bytes = Encoding.UTF8.GetBytes(jsonObject.ToPrettyString());
                        fileStream.SetLength(0);
                        fileStream.Write(bytes, 0, bytes.Length);
                    }
                    return(true);
                }
                catch (IOException e)
                {
                    Logger.GetInstance(typeof(Storage)).Error(e.Message);
                }
                catch (Exception e)
                {
                    Logger.GetInstance(typeof(Storage)).Error(e.ToString());
                }

                return(false);
            }
示例#16
0
        public static void JsonArray_24_InsertJsonObject()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.GetJsonArray("[]");

            Assert.NotNull(jsonArray);
            jsonArray = jsonArray.Insert(0, new LitJsonJsonFactory.LitJsonJsonArray(JsonMapper.ToObject("{}")));
            Assert.NotNull(jsonArray);
        }
        public static void Default_4_GetJsonArray()
        {
            JsonFactory.Register <JsonFactoryImpl>();
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.GetJsonArray("[]");

            Assert.NotNull(jsonArray);
            Assert.Equal("[]", jsonArray.ToString());
        }
示例#18
0
        public static void JsonArray_21_InsertLong()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.GetJsonArray("[]");

            Assert.NotNull(jsonArray);
            jsonArray = jsonArray.Insert(0, 100000000000L);
            Assert.NotNull(jsonArray);
        }
示例#19
0
        public static void JsonArray_22_InsertString()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.GetJsonArray("[]");

            Assert.NotNull(jsonArray);
            jsonArray = jsonArray.Insert(0, "test");
            Assert.NotNull(jsonArray);
        }
示例#20
0
        public static void JsonArray_18_InsertDouble()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.GetJsonArray("[]");

            Assert.NotNull(jsonArray);
            jsonArray = jsonArray.Insert(0, 3.3D);
            Assert.NotNull(jsonArray);
        }
示例#21
0
        public static void JsonArray_19_InsertFloat()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.GetJsonArray("[]");

            Assert.NotNull(jsonArray);
            jsonArray = jsonArray.Insert(0, 2.2F);
            Assert.NotNull(jsonArray);
        }
示例#22
0
        public static void JsonArray_17_InsertBool()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonArray = jsonFactory.GetJsonArray("[]");

            Assert.NotNull(jsonArray);
            jsonArray = jsonArray.Insert(0, true);
            Assert.NotNull(jsonArray);
        }
示例#23
0
        public static void JsonObject_02_ParseDouble_WithDefault()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            var value = jsonObject.ParseDouble("key", 3.3D);

            Assert.Equal(3.3D, value);
        }
示例#24
0
        public static void JsonObject_01_ParseBool_WithDefault()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            var value = jsonObject.ParseBool("key", true);

            Assert.True(value);
        }
示例#25
0
        public static void Default_3_DeserializeObject_WithNull()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var content    = "";
            var testClass1 = jsonFactory.DeserializeObject <TestClass1>(content);

            Assert.Null(testClass1);
            testClass1 = jsonFactory.DeserializeObject <TestClass1>(null);
            Assert.Null(testClass1);
        }
示例#26
0
        public static void JsonObject_03_ParseFloat_WithDefault()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            var value = jsonObject.ParseFloat("key", 2.2F);

            Assert.Equal(2.2F, value);
        }
示例#27
0
        public static void JsonObject_05_ParseLong_WithDefault()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            var value = jsonObject.ParseLong("key", 100000000000L);

            Assert.Equal(100000000000L, value);
        }
示例#28
0
        public static void JsonObject_06_ParseString_WithDefault()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            var value = jsonObject.ParseString("key", "test");

            Assert.Equal("test", value);
        }
示例#29
0
        public static void JsonObject_08_ParseJsonObject()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.GetJsonObject("{}");

            Assert.NotNull(jsonObject);
            var value = jsonObject.ParseJsonObject("key");

            Assert.Null(value);
        }
示例#30
0
        public static void JsonObject_00_HasKey()
        {
            var jsonFactory = JsonFactory.GetInstance();

            Assert.NotNull(jsonFactory);
            var jsonObject = jsonFactory.CreateJsonObject();

            Assert.NotNull(jsonObject);
            Assert.Equal("{}", jsonObject.ToString());
            jsonObject.Put("key", true);
            Assert.True(jsonObject.HasKey("key"));
            Assert.False(jsonObject.HasKey("key2"));
        }