コード例 #1
0
        public void CannotReadPartialRecording()
        {
            var recorder = new JsonRecorder();

            recorder.WriteStartArray();
            recorder.CreatePlayer();
        }
コード例 #2
0
 public void String()
 {
     JsonRecorder recorder = new JsonRecorder();
     recorder.WriteString("Hello World");
     JsonTextWriter writer = new JsonTextWriter();
     recorder.Playback(writer);
     Assert.AreEqual("\"Hello World\"", writer.ToString());
 }
コード例 #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()
 {
     JsonRecorder recorder = new JsonRecorder();
     JsonReader reader = recorder.CreatePlayer();
     Assert.AreEqual(JsonTokenClass.BOF, reader.TokenClass);
     Assert.IsFalse(reader.Read());
     Assert.IsTrue(reader.EOF);
 }
コード例 #6
0
ファイル: JsonRecorder.cs プロジェクト: ashou1986/top4net
        public static JsonRecorder Record(JsonReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            JsonRecorder recorder = new JsonRecorder();
            recorder.WriteFromReader(reader);
            return recorder;
        }
コード例 #7
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);
        }
コード例 #8
0
        public void String()
        {
            var recorder = new JsonRecorder();

            recorder.WriteString("Hello World");
            var writer = new JsonTextWriter();

            recorder.Playback(writer);
            Assert.AreEqual("[\"Hello World\"]", writer.ToString());
        }
コード例 #9
0
        public static JsonRecorder Record(JsonReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            JsonRecorder jsonRecorder = new JsonRecorder();

            jsonRecorder.WriteFromReader(reader);
            return(jsonRecorder);
        }
コード例 #10
0
ファイル: TestJsonArray.cs プロジェクト: bubbafat/Hebo
 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);
 }
コード例 #11
0
        public static JsonRecorder Record(JsonReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            var recorder = new JsonRecorder();

            recorder.WriteFromReader(reader);
            return(recorder);
        }
コード例 #12
0
ファイル: TestJsonArray.cs プロジェクト: krbvroc1/KeeFox
        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);
        }
コード例 #13
0
ファイル: TestJsonObject.cs プロジェクト: bubbafat/Hebo
 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);
 }
コード例 #14
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);
        }
コード例 #15
0
ファイル: TestJsonWriter.cs プロジェクト: pjcrosbie/jayrock
        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);
        }
コード例 #16
0
        public void Complex()
        {
            const string input = @"{'menu': {
                'header': 'SVG Viewer',
                'items': [
                    {'id': 'Open'},
                    {'id': 'OpenNew', 'label': 'Open New'},
                    null,
                    {'id': 'ZoomIn', 'label': 'Zoom In'},
                    {'id': 'ZoomOut', 'label': 'Zoom Out'},
                    {'id': 'OriginalView', 'label': 'Original View'},
                    null,
                    {'id': 'Quality'},
                    {'id': 'Pause'},
                    {'id': 'Mute'},
                    null,
                    {'id': 'Find', 'label': 'Find...'},
                    {'id': 'FindAgain', 'label': 'Find Again'},
                    {'id': 'Copy'},
                    {'id': 'CopyAgain', 'label': 'Copy Again'},
                    {'id': 'CopySVG', 'label': 'Copy SVG'},
                    {'id': 'ViewSVG', 'label': 'View SVG'},
                    {'id': 'ViewSource', 'label': 'View Source'},
                    {'id': 'SaveAs', 'label': 'Save As'},
                    null,
                    {'id': 'Help'},
                    {'id': 'About', 'label': 'About Adobe CVG Viewer...'}
                ]
            }}";

            var reader = new JsonTextReader(new StringReader(input));
            var writer = new JsonTextWriter();

            JsonRecorder.Record(reader).Playback(writer);
            Assert.AreEqual("{\"menu\":{\"header\":\"SVG Viewer\",\"items\":[{\"id\":\"Open\"},{\"id\":\"OpenNew\",\"label\":\"Open New\"},null,{\"id\":\"ZoomIn\",\"label\":\"Zoom In\"},{\"id\":\"ZoomOut\",\"label\":\"Zoom Out\"},{\"id\":\"OriginalView\",\"label\":\"Original View\"},null,{\"id\":\"Quality\"},{\"id\":\"Pause\"},{\"id\":\"Mute\"},null,{\"id\":\"Find\",\"label\":\"Find...\"},{\"id\":\"FindAgain\",\"label\":\"Find Again\"},{\"id\":\"Copy\"},{\"id\":\"CopyAgain\",\"label\":\"Copy Again\"},{\"id\":\"CopySVG\",\"label\":\"Copy SVG\"},{\"id\":\"ViewSVG\",\"label\":\"View SVG\"},{\"id\":\"ViewSource\",\"label\":\"View Source\"},{\"id\":\"SaveAs\",\"label\":\"Save As\"},null,{\"id\":\"Help\"},{\"id\":\"About\",\"label\":\"About Adobe CVG Viewer...\"}]}}", writer.ToString());
        }
コード例 #17
0
ファイル: TestJsonWriter.cs プロジェクト: bubbafat/Hebo
        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);
        }
コード例 #18
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;
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
            }
        }
コード例 #21
0
ファイル: JsonRpcServices.cs プロジェクト: RyanWangTHU/ccv2
        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());
        }
コード例 #22
0
        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());
        }
コード例 #23
0
ファイル: TestJsonRecorder.cs プロジェクト: krbvroc1/KeeFox
 public void CannotReadPartialRecording()
 {
     JsonRecorder recorder = new JsonRecorder();
     recorder.WriteStartArray();
     recorder.CreatePlayer();
 }