예제 #1
0
        private object OnResponseArray(JsonReader reader, Type returnType)
        {
            Logger.Instance().Log("RPC", "Response");
            var members = JsonBuffer.From(reader).GetArray();//.GetMembers();// .GetMembersArray();

            foreach (var member in members)
            {
                var members2 = member.GetMembersArray();//JsonBuffer.From(member.).GetMembersArray();

                foreach (var member2 in members2)
                {
                    if (string.CompareOrdinal(member2.Name, "error") == 0)
                    {
                        var errorObject = JsonConvert.Import(member2.Buffer.CreateReader());
                        if (errorObject != null)
                        {
                            OnError(errorObject);
                        }
                    }
                    else if (string.CompareOrdinal(member2.Name, "result") == 0)
                    {
                        Logger.Instance().Log("RPC", JsonConvert.Import(returnType, member2.Buffer.CreateReader()).ToString());
                        return(returnType != typeof(JsonBuffer)
                                   ? JsonConvert.Import(returnType, member2.Buffer.CreateReader())
                                   : member2.Buffer);
                    }
                }
            }
            throw new JsonException("Invalid JSON-RPC response. It contains neither a result nor error.");
        }
예제 #2
0
        private object OnResponse(JsonReader reader, Type returnType)
        {
            Debug.Assert(reader != null);
            Debug.Assert(returnType != null);

            NamedJsonBuffer[] members = JsonBuffer.From(reader).GetMembersArray();
            foreach (NamedJsonBuffer member in members)
            {
                if (string.CompareOrdinal(member.Name, "error") == 0)
                {
                    object errorObject = JsonConvert.Import(member.Buffer.CreateReader());
                    if (errorObject != null)
                    {
                        OnError(errorObject);
                    }
                }
                else if (string.CompareOrdinal(member.Name, "result") == 0)
                {
                    return(returnType != typeof(JsonBuffer)
                         ? JsonConvert.Import(returnType, member.Buffer.CreateReader())
                         : member.Buffer);
                }
            }

            throw new Exception("Invalid JSON-RPC response. It contains neither a result nor error.");
        }
예제 #3
0
        private static Dictionary <string, string> ParseJsonHeaders(ArraySegment <byte> data)
        {
            var headers = new Dictionary <string, string>();

            foreach (var h in JsonBuffer.From(data.ToAsciiString()).GetMembers())
            {
                var key = h.Name;
                if (h.Buffer.IsScalar)
                {
                    headers.Add(key, h.Buffer.GetString());
                }
                else if (h.Buffer.IsArray)
                {
                    for (int i = 1; i < h.Buffer.GetArrayLength(); i++)
                    {
                        var v = h.Buffer[i];
                        if (v.Class != JsonTokenClass.String)
                        {
                            throw new Exception("non-string value header");
                        }
                        if (headers.ContainsKey(key))
                        {
                            break; //TODO: support many header values
                        }
                        headers.Add(key, v.Text);
                    }
                }
                else
                {
                    throw new Exception("Unexpected JSON type.");
                }
            }
            return(headers);
        }
        public void Export()
        {
            var sb       = new StringBuilder();
            var writer   = JsonText.CreateWriter(sb);
            var buffer   = JsonBuffer.From("[42,{x:123,y:456},foo,true,null,false]");
            var exporter = new JsonBufferExporter();

            exporter.Export(JsonConvert.CreateExportContext(), buffer, writer);
            Assert.AreEqual("[42,{\"x\":123,\"y\":456},\"foo\",true,null,false]", sb.ToString());
        }
예제 #5
0
        public void Construction()
        {
            ObjectConstructor        ctor    = new ObjectConstructor(typeof(Point));
            ImportContext            context = JsonConvert.CreateImportContext();
            const string             json    = "{ y: 456, x: 123 }";
            ObjectConstructionResult result  = ctor.CreateObject(context, JsonText.CreateReader(json));
            Point point = (Point)result.Object;

            Assert.AreEqual(123, point.X);
            Assert.AreEqual(456, point.Y);
            Assert.AreEqual(0, JsonBuffer.From(result.TailReader).GetMemberCount());
        }
예제 #6
0
        public virtual object Import(ImportContext context, JsonReader reader)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return(JsonBuffer.From(reader));
        }
예제 #7
0
        public ObjectConstructionResult CreateObject(ImportContext context, JsonReader reader)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return(CreateObject(context, JsonBuffer.From(reader).GetMembersArray()));
        }
예제 #8
0
        public void ConstructionWithTail()
        {
            ObjectConstructor        ctor    = new ObjectConstructor(typeof(Point));
            ImportContext            context = JsonConvert.CreateImportContext();
            const string             json    = "{ y: 456, z: 789, x: 123 }";
            ObjectConstructionResult result  = ctor.CreateObject(context, JsonText.CreateReader(json));
            Point point = (Point)result.Object;

            Assert.AreEqual(123, point.X);
            Assert.AreEqual(456, point.Y);
            NamedJsonBuffer[] tail = JsonBuffer.From(result.TailReader).GetMembersArray();
            Assert.AreEqual(1, tail.Length);
            NamedJsonBuffer z = tail[0];

            Assert.AreEqual("z", z.Name);
            Assert.AreEqual(789, z.Buffer.GetNumber().ToInt32());
        }
예제 #9
0
        internal Request(string sender, string conn_id, string path, Dictionary <string, string> headers, byte[] body)
        {
            this.Sender  = sender;
            this.ConnId  = conn_id;
            this.Path    = path;
            this.Headers = headers;
            this.Body    = body;

            if (this.Headers["METHOD"] == "JSON")
            {
                this.Data = JsonBuffer.From(body.ToAsciiString());
            }
            else
            {
                this.Data = new JsonBuffer();
            }
        }
예제 #10
0
        public object OnResponse(JsonReader reader, Type returnType)
        {
            Debug.Assert(reader != null);
            Debug.Assert(returnType != null);

            bool   resultSpecified = false;
            object result          = null;

            //
            // JSON-RPC 2.0 specification/protocol, states that either error
            // or result must be present but not both. JSON-RPC 1.0 is less
            // strict and states that one or the other must be null. There
            // is an ambiguity however with 1.0 when both result and error
            // are null. Here, it is treated like a successful null result.
            //

            NamedJsonBuffer[] members = JsonBuffer.From(reader).GetMembersArray();
            foreach (NamedJsonBuffer member in members)
            {
                if (string.CompareOrdinal(member.Name, "error") == 0)
                {
                    object errorObject = JsonConvert.Import(member.Buffer.CreateReader());
                    if (errorObject != null)
                    {
                        OnError(errorObject);
                    }
                }
                else if (string.CompareOrdinal(member.Name, "result") == 0)
                {
                    resultSpecified = true;
                    result          = returnType != typeof(JsonBuffer)
                           ? JsonConvert.Import(returnType, member.Buffer.CreateReader())
                           : member.Buffer;
                }
            }

            if (!resultSpecified) // never gets here on error
            {
                throw new Exception("Invalid JSON-RPC response. It contains neither a result nor an error.");
            }

            return(result);
        }
예제 #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <exception cref="ArgumentNullException">строка запроса не может быть равна null</exception>
        /// <exception cref="ArgumentException">json request некорректен по формату</exception>
        public JsonRequest(string request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (request.Length == 0)
            {
                throw new ArgumentException(null, "request");
            }

            mRequest = request;
            Args     = null;

            // parse request here
            var reader  = JsonText.CreateReader(mRequest);
            var members = JsonBuffer.From(reader).GetMembersArray();

            foreach (var member in members)
            {
                switch (member.Name)
                {
                case "id":
                    Id = (long)JsonConvert.Import(typeof(long), member.Buffer.CreateReader());
                    break;

                case "method":
                    Method = (string)JsonConvert.Import(typeof(string), member.Buffer.CreateReader());
                    break;

                case "params":
                    Args = JsonConvert.Import(typeof(object), member.Buffer.CreateReader());
                    break;
                }
            }

            if (Method.Length == 0)
            {
                throw new ArgumentException("method");
            }
        }
예제 #12
0
        public ResponseParser(string response, Type returnType)
        {
            if (returnType == null)
            {
                throw new ArgumentNullException("returnType");
            }
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            if (response.Length == 0)
            {
                throw new ArgumentException(null, "response");
            }


            var reader          = JsonText.CreateReader(response);
            var resultSpecified = false;

            mResult       = null;
            mErrorMessage = null;

            //
            // JSON-RPC 2.0 specification/protocol, states that either error
            // or result must be present but not both. JSON-RPC 1.0 is less
            // strict and states that one or the other must be null. There
            // is an ambiguity however with 1.0 when both result and error
            // are null. Here, it is treated like a successful null result.
            //

            var members = JsonBuffer.From(reader).GetMembersArray();

            foreach (var member in members)
            {
                switch (member.Name)
                {
                case "error":
                {
                    var errorObject = JsonConvert.Import(member.Buffer.CreateReader());
                    if (errorObject != null)
                    {
                        OnError(errorObject);
                    }
                }
                break;

                case "id":
                    Id = (string)JsonConvert.Import(typeof(string), member.Buffer.CreateReader());
                    break;

                case "result":
                {
                    resultSpecified = true;

                    mResult = (returnType != typeof(JsonBuffer))
                                          ? JsonConvert.Import(returnType, member.Buffer.CreateReader())
                                          : response;
                }
                break;
                }
            }

            if (!resultSpecified) // never gets here on error
            {
                throw new Exception("Invalid JSON-RPC response. It contains neither a result nor an error.");
            }
        }