예제 #1
0
        public void CannotReadPartialRecording()
        {
            var recorder = new JsonRecorder();

            recorder.WriteStartArray();
            recorder.CreatePlayer();
        }
 public void Blank()
 {
     JsonRecorder recorder = new JsonRecorder();
     JsonReader reader = recorder.CreatePlayer();
     Assert.AreEqual(JsonTokenClass.BOF, reader.TokenClass);
     Assert.IsFalse(reader.Read());
     Assert.IsTrue(reader.EOF);
 }
예제 #3
0
 public void Export()
 {
     JsonRecorder writer = new JsonRecorder();
     ((IJsonExportable) JsonNull.Value).Export(new ExportContext(), writer);
     JsonReader reader = writer.CreatePlayer();
     reader.ReadNull();
     Assert.IsTrue(reader.EOF);
 }
예제 #4
0
 public void Export()
 {
     JsonRecorder writer = new JsonRecorder();
     JsonNull.Value.Export(writer);
     JsonReader reader = writer.CreatePlayer();
     reader.ReadNull();
     Assert.IsTrue(reader.EOF);
 }
예제 #5
0
        public void Blank()
        {
            var recorder = new JsonRecorder();
            var reader   = recorder.CreatePlayer();

            Assert.AreEqual(JsonTokenClass.BOF, reader.TokenClass);
            Assert.IsFalse(reader.Read());
            Assert.IsTrue(reader.EOF);
        }
예제 #6
0
 public void Export()
 {
     JsonArray a = new JsonArray(new object[] { 123, "Hello World", true });
     JsonRecorder writer = new JsonRecorder();
     a.Export(writer);
     JsonReader reader = writer.CreatePlayer();
     reader.ReadToken(JsonTokenClass.Array);
     Assert.AreEqual(a[0], reader.ReadNumber().ToInt32());
     Assert.AreEqual(a[1], reader.ReadString());
     Assert.AreEqual(a[2], reader.ReadBoolean());
     reader.ReadToken(JsonTokenClass.EndArray);
 }
예제 #7
0
        public void Export()
        {
            JsonArray    a      = new JsonArray(new object[] { 123, "Hello World", true });
            JsonRecorder writer = new JsonRecorder();

            a.Export(writer);
            JsonReader reader = writer.CreatePlayer();

            reader.ReadToken(JsonTokenClass.Array);
            Assert.AreEqual(a[0], reader.ReadNumber().ToInt32());
            Assert.AreEqual(a[1], reader.ReadString());
            Assert.AreEqual(a[2], reader.ReadBoolean());
            reader.ReadToken(JsonTokenClass.EndArray);
        }
예제 #8
0
 public void Export()
 {
     JsonObject o = new JsonObject();
     o.Put("Number", 123);
     o.Put("String", "Hello World");
     o.Put("Boolean", true);
     JsonRecorder writer = new JsonRecorder();
     o.Export(writer);
     JsonReader reader = writer.CreatePlayer();
     reader.ReadToken(JsonTokenClass.Object);
     string[] members = (string[]) o.GetNamesArray().ToArray(typeof(string));
     Assert.AreEqual(members[0], reader.ReadMember());
     Assert.AreEqual(o[members[0]], reader.ReadNumber().ToInt32());
     Assert.AreEqual(members[1], reader.ReadMember());
     Assert.AreEqual(o[members[1]], reader.ReadString());
     Assert.AreEqual(members[2], reader.ReadMember());
     Assert.AreEqual(o[members[2]], reader.ReadBoolean());
     Assert.AreEqual(JsonTokenClass.EndObject, reader.TokenClass);
 }
예제 #9
0
        public void Export()
        {
            JsonObject o = new JsonObject();

            o.Put("Number", 123);
            o.Put("String", "Hello World");
            o.Put("Boolean", true);
            JsonRecorder writer = new JsonRecorder();

            o.Export(writer);
            JsonReader reader = writer.CreatePlayer();

            reader.ReadToken(JsonTokenClass.Object);
            string[] members = (string[])o.GetNamesArray().ToArray(typeof(string));
            Assert.AreEqual(members[0], reader.ReadMember());
            Assert.AreEqual(o[members[0]], reader.ReadNumber().ToInt32());
            Assert.AreEqual(members[1], reader.ReadMember());
            Assert.AreEqual(o[members[1]], reader.ReadString());
            Assert.AreEqual(members[2], reader.ReadMember());
            Assert.AreEqual(o[members[2]], reader.ReadBoolean());
            Assert.AreEqual(JsonTokenClass.EndObject, reader.TokenClass);
        }
예제 #10
0
        public void AutoCompletion()
        {
            JsonRecorder writer = new JsonRecorder();

            writer.WriteStartArray();
            writer.WriteStartObject();
            writer.WriteMember("outer");
            writer.WriteStartObject();
            writer.WriteMember("inner");
            writer.AutoComplete();

            JsonReader reader = writer.CreatePlayer();

            reader.ReadToken(JsonTokenClass.Array);
            reader.ReadToken(JsonTokenClass.Object);
            Assert.AreEqual("outer", reader.ReadMember());
            reader.ReadToken(JsonTokenClass.Object);
            Assert.AreEqual("inner", reader.ReadMember());
            reader.ReadNull();
            reader.ReadToken(JsonTokenClass.EndObject);
            reader.ReadToken(JsonTokenClass.EndObject);
            reader.ReadToken(JsonTokenClass.EndArray);
            Assert.IsTrue(reader.EOF);
        }
예제 #11
0
        public void AutoCompletion()
        {
            JsonRecorder writer = new JsonRecorder();

            writer.WriteStartArray();
            writer.WriteStartObject();
            writer.WriteMember("outer");
            writer.WriteStartObject();
            writer.WriteMember("inner");
            writer.AutoComplete();

            JsonReader reader = writer.CreatePlayer();

            reader.ReadToken(JsonTokenClass.Array);
            reader.ReadToken(JsonTokenClass.Object);
            Assert.AreEqual("outer", reader.ReadMember());
            reader.ReadToken(JsonTokenClass.Object);
            Assert.AreEqual("inner", reader.ReadMember());
            reader.ReadNull();
            reader.ReadToken(JsonTokenClass.EndObject);
            reader.ReadToken(JsonTokenClass.EndObject);
            reader.ReadToken(JsonTokenClass.EndArray);
            Assert.IsTrue(reader.EOF);
        }
        protected virtual IDictionary ParseRequest(JsonReader input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            JsonReader reader = input; // alias for clarity
            JsonImportHandler importer = JsonImporter;

            JsonObject request = new JsonObject();
            Method method = null;
            JsonReader paramsReader = null;
            object args = null;

            try
            {
                reader.ReadToken(JsonTokenClass.Object);

                while (reader.TokenClass != JsonTokenClass.EndObject)
                {
                    string memberName = reader.ReadMember();

                    switch (memberName)
                    {
                        case "id" :
                        {
                            request["id"] = importer(AnyType.Value, reader);
                            break;
                        }

                        case "method" :
                        {
                            string methodName = reader.ReadString();
                            request["method"] = methodName;
                            method = Service.GetClass().GetMethodByName(methodName);

                            if (paramsReader != null)
                            {
                                //
                                // If the parameters were already read in and
                                // buffer, then deserialize them now that we know
                                // the method we're dealing with.
                                //

                                args = ReadParameters(method, paramsReader, importer);
                                paramsReader = null;
                            }

                            break;
                        }

                        case "params" :
                        {
                            //
                            // Is the method already known? If so, then we can
                            // deserialize the parameters right away. Otherwise
                            // we record them until hopefully the method is
                            // encountered.
                            //

                            if (method != null)
                            {
                                args = ReadParameters(method, reader, importer);
                            }
                            else
                            {
                                JsonRecorder recorder = new JsonRecorder();
                                recorder.WriteFromReader(reader);
                                paramsReader = recorder.CreatePlayer();
                            }

                            break;
                        }

                        default:
                        {
                            reader.Skip();
                            break;
                        }
                    }
                }

                reader.Read();

                if (args != null)
                    request["params"] = args;

                return request;
            }
            catch (JsonException e)
            {
                throw new BadRequestException(e.Message, e, request);
            }
            catch (MethodNotFoundException e)
            {
                throw new BadRequestException(e.Message, e, request);
            }
        }
예제 #13
0
        public static object GetResult(IDictionary response, Type resultType)
        {
            if (response == null)
                throw new ArgumentNullException("response");

            object errorObject = response["error"];

            if (errorObject != null)
            {
                IDictionary error = errorObject as IDictionary;
                
                string message = null;

                if (!JsonNull.LogicallyEquals(error))
                {
                    object messageObject = error["message"];
                    
                    if (!JsonNull.LogicallyEquals(messageObject))
                        message = messageObject.ToString();
                }
                else
                    message = error.ToString();
   
                throw new JsonRpcException(message);
            }

            if (!response.Contains("result"))
                throw new ArgumentException("Response object is not valid because it does not contain the expected 'result' member.", "response");

            object result = response["result"];

            if (resultType == null)
                return result;
            
            JsonRecorder recorder = new JsonRecorder();
            // FIXME: Allow the context to be passed in.
            JsonConvert.Export(result, recorder);
            return JsonConvert.Import(resultType, recorder.CreatePlayer());
        }
        public void DoesNotExportPropertyValueWhenEqualsSpecfiedDefault()
        {
            IObjectMemberExporter exporter = CreatePropertyExporter("prop", 0, 0);

            ExportContext context = new ExportContext();
            JsonRecorder writer = new JsonRecorder();
            writer.WriteStartObject();
            exporter.Export(context, writer, new object());
            writer.WriteEndObject();

            JsonReader reader = writer.CreatePlayer();
            reader.ReadToken(JsonTokenClass.Object);
            reader.ReadToken(JsonTokenClass.EndObject);
        }
예제 #15
0
 public void CannotReadPartialRecording()
 {
     JsonRecorder recorder = new JsonRecorder();
     recorder.WriteStartArray();
     recorder.CreatePlayer();
 }
예제 #16
0
        protected virtual object ParseRequest(TextReader input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            JsonReader reader = (JsonReader) _serviceProvider.GetService(typeof(JsonReader));

            if (reader == null)
                reader = new JsonTextReader(input);

            ImportContext importContext = new ImportContext();

            JsonObject request = new JsonObject();
            Method method = null;
            JsonReader paramsReader = null;
            object args = null;

            reader.ReadToken(JsonTokenClass.Object);

            while (reader.TokenClass != JsonTokenClass.EndObject)
            {
                string memberName = reader.ReadMember();

                switch (memberName)
                {
                    case "id" :
                    {
                        request["id"] = importContext.Import(reader);
                        break;
                    }

                    case "method" :
                    {
                        string methodName = reader.ReadString();
                        request["method"] = methodName;
                        method = _service.GetClass().GetMethodByName(methodName);

                        if (paramsReader != null)
                        {
                            //
                            // If the parameters were already read in and
                            // buffer, then deserialize them now that we know
                            // the method we're dealing with.
                            //

                            args = ReadParameters(method, paramsReader, importContext);
                            paramsReader = null;
                        }

                        break;
                    }

                    case "params" :
                    {
                        //
                        // Is the method already known? If so, then we can
                        // deserialize the parameters right away. Otherwise
                        // we record them until hopefully the method is
                        // encountered.
                        //

                        if (method != null)
                        {
                            args = ReadParameters(method, reader, importContext);
                        }
                        else
                        {
                            JsonRecorder recorder = new JsonRecorder();
                            recorder.WriteFromReader(reader);
                            paramsReader = recorder.CreatePlayer();
                        }

                        break;
                    }

                    default:
                    {
                        reader.Skip();
                        break;
                    }
                }
            }

            reader.Read();

            if (args != null)
                request["params"] = args;

            return request;
        }
        public void ExportsPropertyValueWhenNotEqualsSpecfiedDefault()
        {
            const string propertyName = "prop";
            IObjectMemberExporter exporter = CreatePropertyExporter(propertyName, 42, 0);

            ExportContext context = new ExportContext();
            JsonRecorder writer = new JsonRecorder();
            writer.WriteStartObject();
            exporter.Export(context, writer, new object());
            
            JsonReader reader = writer.CreatePlayer();
            reader.ReadToken(JsonTokenClass.Object);
            Assert.AreEqual(propertyName, reader.ReadMember());
            Assert.AreEqual(42, reader.ReadNumber().ToInt32());
        }