Ejemplo n.º 1
0
        public void JsonRpcDispatcherTest()
        {
            var dispatcher = new RpcDispatcher <JsonValue>();
            var f          = new JsonFormatter();

            {
                f.Clear();
                dispatcher.Register("add", (int a, int b) => a + b);
                f.Request("add", 1, 2);

                var parsed = JsonParser.Parse(new Utf8String(f.GetStoreBytes()));

                f.Clear();
                dispatcher.Call(f, parsed["id"].GetInt32(), parsed["method"].GetString(), parsed["params"]);
                var response = JsonParser.Parse(new Utf8String(f.GetStoreBytes()));
                Assert.AreEqual(3, response["result"].GetInt32());
            }

            {
                string msg = null;
                dispatcher.Register("print", (string _msg) => { msg = _msg; });
                f.Clear();
                f.Request("print", "hoge");

                var parsed = JsonParser.Parse(new Utf8String(f.GetStoreBytes()));
                f.Clear();
                dispatcher.Call(f, parsed["id"].GetInt32(), parsed["method"].GetString(), parsed["params"]);

                Assert.AreEqual("hoge", msg);
            }
        }
Ejemplo n.º 2
0
        public void JsonRpcRequestTest()
        {
            var f = new JsonFormatter();

            {
                f.Clear();
                var l = JsonParser.Parse("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"num1\",\"params\":[1]}");
                f.Request("num1", 1);
                var u = new Utf8String(f.GetStoreBytes());
                var r = JsonParser.Parse(u);
                Assert.AreEqual(
                    l,
                    r);
            }
            {
                f.Clear();
                f.Request("num2", 2, true);
                Assert.AreEqual(
                    JsonParser.Parse("{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"num2\",\"params\":[2,true]}"),
                    JsonParser.Parse(new Utf8String(f.GetStoreBytes())));
            }
            {
                f.Clear();
                f.Request("num3",
                          3, true, "abc", false, (string)null, new[] { 1, 2 });
                Assert.AreEqual(
                    JsonParser.Parse("{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"num3\",\"params\":[3,true,\"abc\",false,null,[1,2]]}"),
                    JsonParser.Parse(new Utf8String(f.GetStoreBytes())));
            }
        }
Ejemplo n.º 3
0
        public void PathTest()
        {
            var json = @"
{
    ""a"": [
        {
            ""aa"": 1
        }       
    ]
}
";
            var root = JsonParser.Parse(json);

            {
                var it = root.Traverse().GetEnumerator();
                it.MoveNext(); Assert.AreEqual("/", it.Current.Pointer().ToString());
                it.MoveNext(); Assert.AreEqual("/a", it.Current.Pointer().ToString());
                it.MoveNext(); Assert.AreEqual("/a/0", it.Current.Pointer().ToString());
                it.MoveNext(); Assert.AreEqual("/a/0/aa", it.Current.Pointer().ToString());
                Assert.False(it.MoveNext());
            }

            {
                var it = root.Traverse().GetEnumerator();

                var f = new JsonFormatter();
                f.Serialize("JsonPath");

                root.SetValue(Utf8String.From("/a"), f.GetStoreBytes());
                it.MoveNext(); Assert.AreEqual("/", it.Current.Pointer().ToString());
                it.MoveNext(); Assert.AreEqual("/a", it.Current.Pointer().ToString());
                Assert.False(it.MoveNext());
            }
        }
Ejemplo n.º 4
0
        public void VciMeta()
        {
            var meta = new glTF_VCAST_vci_meta
            {
                author       = "AUTHOR",
                scriptFormat = ScriptFormat.luaBinary,
            };

            //var f = new JsonFormatter();
            //f.Serialize(src);
            //var parsed = JsonParser.Parse(new Utf8String(f.GetStore().Bytes));

            //Assert.AreEqual("AUTHOR", parsed["author"].GetString());
            //Assert.AreEqual("luaBinary", parsed["scriptFormat"].GetString());

            //var dst = new glTF_VCAST_vci_meta();
            //parsed.Deserialize(ref dst);
            //Assert.AreEqual(src.scriptFormat, dst.scriptFormat);

            var f = new UniJSON.JsonFormatter();

            glTF_VCAST_vci_meta_Serializer.Serialize(f, meta);
            var bytes = f.GetStoreBytes();

            UnityEngine.Debug.Log(System.Text.Encoding.UTF8.GetString(bytes.ToArray()));
            var parsed = bytes.ParseAsJson();

            Assert.AreEqual("AUTHOR", parsed["author"].GetString());
            Assert.AreEqual("luaBinary", parsed["scriptFormat"].GetString());
        }
Ejemplo n.º 5
0
        public void NullTest()
        {
            var bytes = Encoding.UTF8.GetBytes("null");
            var json  = new JsonFormatter();

            json.Null();
            Assert.True(json.GetStoreBytes().ToEnumerable().SequenceEqual(bytes));
        }
Ejemplo n.º 6
0
        public void TimeTest()
        {
            var f = new JsonFormatter();

            f.Value(new DateTimeOffset());

            Assert.AreEqual("\"0001-01-01T00:00:00Z\"", new Utf8String(f.GetStoreBytes()).ToString());
        }
Ejemplo n.º 7
0
 public void BooleanTest()
 {
     {
         var bytes = Encoding.UTF8.GetBytes("true");
         var json  = new JsonFormatter();
         json.Value(true);
         Assert.True(json.GetStoreBytes().ToEnumerable().SequenceEqual(bytes));
     }
     {
         var bytes = Encoding.UTF8.GetBytes("false");
         var json  = new JsonFormatter();
         json.Value(false);
         Assert.True(json.GetStoreBytes().ToEnumerable().SequenceEqual(bytes));
     }
 }
        public void KeyValue()
        {
            var p = new Point
            {
                X      = 1,
                Vector = new float[] { 1, 2, 3 }
            };

            var f = new JsonFormatter();

            f.BeginMap();
            f.KeyValue(() => p.Vector);
            f.EndMap();

            var json = JsonParser.Parse(new Utf8String(f.GetStoreBytes()));

            Assert.AreEqual(1, json.GetObjectCount());
            Assert.AreEqual(1, json["Vector"][0].GetInt32());
        }