Ejemplo n.º 1
0
 static StockJsonBuffers()
 {
     JsonBufferWriter writer = new JsonBufferWriter(2);
     writer.WriteStartObject();
     writer.WriteEndObject();
     EmptyObject = writer.GetBuffer();
 }
Ejemplo n.º 2
0
 private JsonBufferReader(JsonBuffer buffer, int index)
 {
     if (buffer.IsEmpty)
     {
         throw new ArgumentException("buffer");
     }
     this._buffer = buffer;
     this._index = index;
 }
Ejemplo n.º 3
0
 public void TestEndOfFile()
 {
     var json = "\t ";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.EndOfFile, token.Type);
     Assert.Equal("<eof>", token.Lexeme);
     Assert.Equal(-1, buffer.Read());
 }
Ejemplo n.º 4
0
 public void TestBeginObject()
 {
     var json = "\t {x";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.BeginObject, token.Type);
     Assert.Equal("{", token.Lexeme);
     Assert.Equal('x', buffer.Read());
 }
Ejemplo n.º 5
0
 public void TestEndArray()
 {
     var json = "\t ]x";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.EndArray, token.Type);
     Assert.Equal("]", token.Lexeme);
     Assert.Equal('x', buffer.Read());
 }
Ejemplo n.º 6
0
 public void TestNameSeparator()
 {
     var json = "\t :x";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.Colon, token.Type);
     Assert.Equal(":", token.Lexeme);
     Assert.Equal('x', buffer.Read());
 }
Ejemplo n.º 7
0
 public NamedJsonBuffer(string name, JsonBuffer buffer)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (buffer.IsEmpty)
     {
         throw new ArgumentException(null, "buffer");
     }
     this._name = Mask.NullString(name);
     this._buffer = buffer;
 }
Ejemplo n.º 8
0
 public void TestRegularExpressionPatternAndOptions()
 {
     var json = "\t /pattern/imxs,";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.RegularExpression, token.Type);
     Assert.Equal("/pattern/imxs", token.Lexeme);
     Assert.Equal(',', buffer.Read());
 }
Ejemplo n.º 9
0
 public void TestRegularExpressionEmpty()
 {
     var json = "\t //,";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.RegularExpression, token.Type);
     Assert.Equal("//", token.Lexeme);
     Assert.Equal(',', buffer.Read());
 }
Ejemplo n.º 10
0
 public void TestMinusZeroExponentMinusTwelve()
 {
     var json = "\t -1e-12,";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.Double, token.Type);
     Assert.Equal("-1e-12", token.Lexeme);
     Assert.Equal(',', buffer.Read());
 }
Ejemplo n.º 11
0
 public void TestMinusOnePointTwo()
 {
     var json = "\t -1.2,";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.Double, token.Type);
     Assert.Equal("-1.2", token.Lexeme);
     Assert.Equal(',', buffer.Read());
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Deserializes an object from a JsonBuffer.
 /// </summary>
 /// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
 /// <param name="buffer">The JsonBuffer.</param>
 /// <returns>A TNominalType.</returns>
 public static TNominalType Deserialize <TNominalType>(JsonBuffer buffer)
 {
     return((TNominalType)Deserialize(buffer, typeof(TNominalType)));
 }
Ejemplo n.º 13
0
        internal static IEnumerable <object> ReadDocumentsAs(Type documentType, string filePath, FileFormat format)
        {
            if (format == FileFormat.Auto)
            {
                format = filePath.EndsWith(".json", StringComparison.OrdinalIgnoreCase) ? FileFormat.Json : FileFormat.Bson;
            }

            var serializer = BsonSerializer.LookupSerializer(documentType);
            var options    = DocumentSerializationOptions.Defaults;

            if (format == FileFormat.Json)
            {
                var  jb    = new JsonBuffer(File.ReadAllText(filePath));
                bool array = false;
                for (; ;)
                {
                    // skip white
                    int c;
                    for (; ;)
                    {
                        // end or white?
                        if ((c = jb.Read()) <= 32)
                        {
                            // end?
                            if (c < 0)
                            {
                                goto end;
                            }

                            // white
                            continue;
                        }

                        // document
                        if (c == '{')
                        {
                            jb.UnRead(c);
                            break;
                        }

                        // array
                        if (c == ',')
                        {
                            if (array)
                            {
                                continue;
                            }
                        }
                        else if (c == ']')
                        {
                            if (array)
                            {
                                array = false;
                                continue;
                            }
                        }
                        else if (c == '[')
                        {
                            if (!array)
                            {
                                array = true;
                                continue;
                            }
                        }

                        throw new FormatException(string.Format(null, "Unexpected character '{0}' at position {1}.", (char)c, jb.Position - 1));
                    }

                    using (var bsonReader = BsonReader.Create(jb))
                        yield return(serializer.Deserialize(bsonReader, documentType, options));
                }
                end :;
            }
            else
            {
                using (var stream = File.OpenRead(filePath))
                {
                    long length = stream.Length;

                    while (stream.Position < length)
                    {
                        using (var bsonReader = BsonReader.Create(stream))
                            yield return(serializer.Deserialize(bsonReader, documentType, options));
                    }
                }
            }
        }
Ejemplo n.º 14
0
 public void TestMinusZero()
 {
     var json = "\t -0,";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.Int32, token.Type);
     Assert.Equal("-0", token.Lexeme);
     Assert.Equal(',', buffer.Read());
 }
Ejemplo n.º 15
0
 public void TestUnquotedString()
 {
     var json = "\t name123:1";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.UnquotedString, token.Type);
     Assert.Equal("name123", token.StringValue);
     Assert.Equal(':', buffer.Read());
 }
Ejemplo n.º 16
0
 public void TestUndefined()
 {
     var json = "\t undefined,";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.UnquotedString, token.Type);
     Assert.Equal("undefined", token.StringValue);
     Assert.Equal(',', buffer.Read());
 }
Ejemplo n.º 17
0
 public void TestEscapeSequences()
 {
     var json = "\t \"x\\\"\\\\\\/\\b\\f\\n\\r\\t\\u0030y\"x";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.String, token.Type);
     Assert.Equal("x\"\\/\b\f\n\r\t0y", token.StringValue);
     Assert.Equal('x', buffer.Read());
 }
Ejemplo n.º 18
0
 public void Test3CharacterString()
 {
     var json = "\t \"123\"x";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.String, token.Type);
     Assert.Equal("123", token.StringValue);
     Assert.Equal('x', buffer.Read());
 }
Ejemplo n.º 19
0
        private static ObjectConstructionResult TryCreateObject(ImportContext context, ConstructorInfo ctor, NamedJsonBuffer[] members)
        {
            Debug.Assert(context != null);
            Debug.Assert(ctor != null);
            Debug.Assert(members != null);

            ParameterInfo[] parameters = ctor.GetParameters();

            if (parameters.Length > members.Length)
            {
                return(null);
            }

            int[] bindings = Bind(context, parameters, members);

            int argc = 0;

            object[]         args  = null;
            JsonBufferWriter tailw = null;

            for (int i = 0; i < bindings.Length; i++)
            {
                int binding = bindings[i] - 1;

                if (binding >= 0)
                {
                    if (args == null)
                    {
                        args = new object[parameters.Length];
                    }

                    Type       type = parameters[binding].ParameterType;
                    JsonBuffer arg  = members[i].Buffer;
                    args[binding] = context.Import(type, arg.CreateReader());
                    argc++;
                }
                else
                {
                    if (tailw == null)
                    {
                        tailw = new JsonBufferWriter();
                        tailw.WriteStartObject();
                    }

                    NamedJsonBuffer member = members[i];
                    tailw.WriteMember(member.Name);
                    tailw.WriteFromReader(member.Buffer.CreateReader());
                }
            }

            if (tailw != null)
            {
                tailw.WriteEndObject();
            }

            if (argc != parameters.Length)
            {
                return(null);
            }

            object obj = ctor.Invoke(args);

            JsonBuffer tail = tailw != null
                            ? tailw.GetBuffer()
                            : StockJsonBuffers.EmptyObject;

            return(new ObjectConstructionResult(obj, tail.CreateReader()));
        }
Ejemplo n.º 20
0
 public void TestTwelve()
 {
     var json = "\t 12,";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.Int32, token.Type);
     Assert.Equal("12", token.Lexeme);
     Assert.Equal(',', buffer.Read());
 }
Ejemplo n.º 21
0
 public void TestZeroExponentOne()
 {
     var json = "\t 0e1,";
     var buffer = new JsonBuffer(json);
     var token = JsonScanner.GetNextToken(buffer);
     Assert.Equal(JsonTokenType.Double, token.Type);
     Assert.Equal("0e1", token.Lexeme);
     Assert.Equal(',', buffer.Read());
 }
Ejemplo n.º 22
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.");
            }
        }
Ejemplo n.º 23
0
 public JsonBufferReader(JsonBuffer buffer) : this(buffer, -1)
 {
 }