示例#1
0
        public void DeserializeRequestDataT021()
        {
            var jsont = EmbeddedResourceManager.GetString("Assets.v2_spec_02.1_req.json");
            var jrcr  = new JsonRpcContractResolver();
            var jrs   = new JsonRpcSerializer(jrcr);

            var jrmp = new Dictionary <string, Type>
            {
                ["subtrahend"] = typeof(long),
                ["minuend"]    = typeof(long)
            };

            jrcr.AddRequestContract("subtract", new JsonRpcRequestContract(jrmp));

            var jrd = jrs.DeserializeRequestData(jsont);

            Assert.IsFalse(jrd.IsBatch);

            var jrmi = jrd.Item;

            Assert.IsTrue(jrmi.IsValid);

            var jrm = jrmi.Message;

            Assert.AreEqual(4L, jrm.Id);
            Assert.AreEqual("subtract", jrm.Method);
            Assert.AreEqual(JsonRpcParametersType.ByName, jrm.ParametersType);
            Assert.AreEqual(23L, jrm.ParametersByName["subtrahend"]);
            Assert.AreEqual(42L, jrm.ParametersByName["minuend"]);
        }
示例#2
0
        public void V2SpecT021DeserializeRequest()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_spec_02.1_req.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            var jsonRpcSubtractParametersScheme = new Dictionary <string, Type>
            {
                ["subtrahend"] = typeof(long),
                ["minuend"]    = typeof(long)
            };

            jsonRpcSerializer.RequestContracts["subtract"] = new JsonRpcRequestContract(jsonRpcSubtractParametersScheme);

            var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal(4L, jsonRpcMessage.Id);
            Assert.Equal("subtract", jsonRpcMessage.Method);
            Assert.Equal(JsonRpcParametersType.ByName, jsonRpcMessage.ParametersType);
            Assert.Equal(23L, jsonRpcMessage.ParametersByName["subtrahend"]);
            Assert.Equal(42L, jsonRpcMessage.ParametersByName["minuend"]);
        }
示例#3
0
        public void V1SpecT010DeserializeRequest()
        {
            var jsonSample = EmbeddedResourceManager.GetString("Assets.v1_spec_01.0_req.json");

            var jsonRpcSerializer = new JsonRpcSerializer
            {
                CompatibilityLevel = JsonRpcCompatibilityLevel.Level1
            };

            jsonRpcSerializer.RequestContracts["echo"] = new JsonRpcRequestContract(new[] { typeof(string) });

            var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal(1L, jsonRpcMessage.Id);
            Assert.Equal("echo", jsonRpcMessage.Method);
            Assert.Equal(JsonRpcParametersType.ByPosition, jsonRpcMessage.ParametersType);
            Assert.Equal(new object[] { "Hello JSON-RPC" }, jsonRpcMessage.ParametersByPosition);
        }
示例#4
0
        public void CoreDeserializeRequestDataWhenJsonStringIsNull()
        {
            var jsonRpcSerializer = new JsonRpcSerializer();

            Assert.Throws <ArgumentNullException>(() =>
                                                  jsonRpcSerializer.DeserializeRequestData(null));
        }
示例#5
0
        public void V1BitcoinT01DeserializeRequest()
        {
            var jsonSample = EmbeddedResourceManager.GetString("Assets.v1_btc_01_req.json");

            var jsonRpcSerializer = new JsonRpcSerializer
            {
                CompatibilityLevel = JsonRpcCompatibilityLevel.Level1
            };

            jsonRpcSerializer.RequestContracts["getblockhash"] = new JsonRpcRequestContract(new[] { typeof(long) });

            var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal("foo", jsonRpcMessage.Id);
            Assert.Equal("getblockhash", jsonRpcMessage.Method);
            Assert.Equal(JsonRpcParametersType.ByPosition, jsonRpcMessage.ParametersType);
            Assert.Equal(new object[] { 0L }, jsonRpcMessage.ParametersByPosition);
        }
示例#6
0
        public void CoreDeserializeRequestDataWhenJsonStringIsEmpty()
        {
            var jsonRpcSerializer = new JsonRpcSerializer();

            var exception = Assert.Throws <JsonRpcException>(() =>
                                                             jsonRpcSerializer.DeserializeRequestData(string.Empty));

            Assert.Equal(JsonRpcErrorCodes.InvalidJson, exception.ErrorCode);
        }
示例#7
0
        protected JsonRpcSerializerBenchmarks()
        {
            _serializer.RequestContracts["mn"] = new JsonRpcRequestContract(new Dictionary <string, Type> {
                ["p1"] = typeof(long), ["p2"] = typeof(long)
            });
            _serializer.RequestContracts["mp"] = new JsonRpcRequestContract(new[] { typeof(long), typeof(long) });

            foreach (var identifier in new JsonRpcId[] { "1", "2", 1L, 2L, 1D, 2D })
            {
                _serializer.DynamicResponseBindings[identifier] = new JsonRpcResponseContract(typeof(long), typeof(long));
            }

            foreach (var name in new[] { "req_nul_nam", "req_nul_pos", "req_str_nam", "req_str_pos", "req_int_nam", "req_int_pos", "req_flt_nam", "req_flt_pos" })
            {
                _resources[name] = EmbeddedResourceManager.GetString($"Assets.{name}.json");
                _requests[name]  = _serializer.DeserializeRequestData(_resources[name]).Items.Select(i => i.Message).ToArray();
            }

            foreach (var name in new[] { "res_nul_err", "res_str_err", "res_str_scs", "res_int_err", "res_int_scs", "res_flt_err", "res_flt_scs" })
            {
                _resources[name] = EmbeddedResourceManager.GetString($"Assets.{name}.json");
                _responses[name] = _serializer.DeserializeResponseData(_resources[name]).Items.Select(i => i.Message).ToArray();
            }
        }
示例#8
0
        public void V2SpecT030DeserializeRequest()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_spec_03.0_req.json");
            var jsonRpcSerializer = new JsonRpcSerializer();
            var jsonRpcSubtractParametersScheme = new[] { typeof(long), typeof(long), typeof(long), typeof(long), typeof(long) };

            jsonRpcSerializer.RequestContracts["update"] = new JsonRpcRequestContract(jsonRpcSubtractParametersScheme);

            var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal(default, jsonRpcMessage.Id);
示例#9
0
        public void V2CoreDeserializeRequestDataWhenIdTypeIsInvalid()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_core_id_type_invalid_req.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.RequestContracts["m"] = new JsonRpcRequestContract();

            var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.False(jsonRpcItem.IsValid);

            var jsonRpcException = jsonRpcItem.Exception;

            Assert.Equal(JsonRpcErrorCodes.InvalidMessage, jsonRpcException.ErrorCode);
        }
        public void DeserializeRequestDataT021()
        {
            var jsont = EmbeddedResourceManager.GetString("Assets.v1_spec_02.1_req.json");
            var jrcr  = new JsonRpcContractResolver();
            var jrs   = new JsonRpcSerializer(jrcr, compatibilityLevel: JsonRpcCompatibilityLevel.Level1);

            jrcr.AddRequestContract("handleMessage", new JsonRpcRequestContract(new[] { typeof(string), typeof(string) }));

            var jrd = jrs.DeserializeRequestData(jsont);

            Assert.IsFalse(jrd.IsBatch);

            var jrmi = jrd.Item;

            Assert.IsTrue(jrmi.IsValid);

            var jrm = jrmi.Message;

            Assert.AreEqual(default, jrm.Id);
示例#11
0
        public void DeserializeRequestDataT030()
        {
            var jsont = EmbeddedResourceManager.GetString("Assets.v2_spec_03.0_req.json");
            var jrcr  = new JsonRpcContractResolver();
            var jrs   = new JsonRpcSerializer(jrcr);
            var jrrcp = new[] { typeof(long), typeof(long), typeof(long), typeof(long), typeof(long) };

            jrcr.AddRequestContract("update", new JsonRpcRequestContract(jrrcp));

            var jrd = jrs.DeserializeRequestData(jsont);

            Assert.IsFalse(jrd.IsBatch);

            var jrmi = jrd.Item;

            Assert.IsTrue(jrmi.IsValid);

            var jrm = jrmi.Message;

            Assert.AreEqual(default, jrm.Id);
示例#12
0
        public void V2CoreDeserializeRequestDataWhenIdIsFloat()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_core_id_float_req.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.RequestContracts["m"] = new JsonRpcRequestContract();

            var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;
            var jsonRpcId      = jsonRpcMessage.Id;

            Assert.Equal(JsonRpcIdType.Float, jsonRpcId.Type);
            Assert.Equal(1D, (double)jsonRpcId);
        }
示例#13
0
        public void V1SpecT021DeserializeRequest()
        {
            var jsonSample = EmbeddedResourceManager.GetString("Assets.v1_spec_02.1_req.json");

            var jsonRpcSerializer = new JsonRpcSerializer
            {
                CompatibilityLevel = JsonRpcCompatibilityLevel.Level1
            };

            jsonRpcSerializer.RequestContracts["handleMessage"] = new JsonRpcRequestContract(new[] { typeof(string), typeof(string) });

            var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal(default, jsonRpcMessage.Id);
示例#14
0
        public void V2SpecT011DeserializeRequest()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_spec_01.1_req.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.RequestContracts["subtract"] = new JsonRpcRequestContract(new[] { typeof(long), typeof(long) });

            var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonSample);

            Assert.False(jsonRpcData.IsBatch);

            var jsonRpcItem = jsonRpcData.Item;

            Assert.True(jsonRpcItem.IsValid);

            var jsonRpcMessage = jsonRpcItem.Message;

            Assert.Equal(2L, jsonRpcMessage.Id);
            Assert.Equal("subtract", jsonRpcMessage.Method);
            Assert.Equal(JsonRpcParametersType.ByPosition, jsonRpcMessage.ParametersType);
            Assert.Equal(new object[] { 23L, 42L }, jsonRpcMessage.ParametersByPosition);
        }
示例#15
0
        public void DeserializeRequestDataT011()
        {
            var jsont = EmbeddedResourceManager.GetString("Assets.v2_spec_01.1_req.json");
            var jrcr  = new JsonRpcContractResolver();
            var jrs   = new JsonRpcSerializer(jrcr);

            jrcr.AddRequestContract("subtract", new JsonRpcRequestContract(new[] { typeof(long), typeof(long) }));

            var jrd = jrs.DeserializeRequestData(jsont);

            Assert.IsFalse(jrd.IsBatch);

            var jrmi = jrd.Item;

            Assert.IsTrue(jrmi.IsValid);

            var jrm = jrmi.Message;

            Assert.AreEqual(2L, jrm.Id);
            Assert.AreEqual("subtract", jrm.Method);
            Assert.AreEqual(JsonRpcParametersType.ByPosition, jrm.ParametersType);
            CollectionAssert.AreEqual(new object[] { 23L, 42L }, jrm.ParametersByPosition?.ToArray());
        }
        public void DeserializeRequestDataT020()
        {
            var jsont = EmbeddedResourceManager.GetString("Assets.v1_spec_02.0_req.json");
            var jrcr  = new JsonRpcContractResolver();
            var jrs   = new JsonRpcSerializer(jrcr, compatibilityLevel: JsonRpcCompatibilityLevel.Level1);

            jrcr.AddRequestContract("postMessage", new JsonRpcRequestContract(new[] { typeof(string) }));

            var jrd = jrs.DeserializeRequestData(jsont);

            Assert.IsFalse(jrd.IsBatch);

            var jrmi = jrd.Item;

            Assert.IsTrue(jrmi.IsValid);

            var jrm = jrmi.Message;

            Assert.AreEqual(99L, jrm.Id);
            Assert.AreEqual("postMessage", jrm.Method);
            Assert.AreEqual(JsonRpcParametersType.ByPosition, jrm.ParametersType);

            CollectionAssert.AreEqual(new object[] { "Hello all!" }, jrm.ParametersByPosition?.ToArray());
        }
示例#17
0
        public void CoreDeserializeRequestDatatFromStream()
        {
            var jsonSample        = EmbeddedResourceManager.GetString("Assets.v2_core_req.json");
            var jsonRpcSerializer = new JsonRpcSerializer();

            jsonRpcSerializer.RequestContracts["m"] = new JsonRpcRequestContract();

            using (var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonSample)))
            {
                var jsonRpcData = jsonRpcSerializer.DeserializeRequestData(jsonStream);

                Assert.False(jsonRpcData.IsBatch);

                var jsonRpcItem = jsonRpcData.Item;

                Assert.True(jsonRpcItem.IsValid);

                var jsonRpcMessage = jsonRpcItem.Message;

                Assert.Equal(0L, jsonRpcMessage.Id);
                Assert.Equal("m", jsonRpcMessage.Method);
                Assert.Equal(JsonRpcParametersType.None, jsonRpcMessage.ParametersType);
            }
        }