Parse() static private method

static private Parse ( UniJSON.Utf8String segment, List values, int parentIndex ) : UniJSON.JsonValue
segment UniJSON.Utf8String
values List
parentIndex int
return UniJSON.JsonValue
Ejemplo n.º 1
0
        public void ObjectTest()
        {
            {
                var json = "{}";
                var node = JsonParser.Parse(json);
                Assert.AreEqual(0, node.Value.Segment.Offset);

                Assert.AreEqual(2, node.Value.Segment.Count);

                Assert.AreEqual(JsonValueType.Object, node.Value.ValueType);
                Assert.AreEqual(0, node.ObjectItems.Count());
            }

            {
                var json = "{\"key\":\"value\"}";
                var node = JsonParser.Parse(json);
                Assert.AreEqual(0, node.Value.Segment.Offset);
                Assert.AreEqual(json.Length, node.Value.Segment.Count);
                Assert.AreEqual(JsonValueType.Object, node.Value.ValueType);

                var it = node.ObjectItems.GetEnumerator();

                Assert.IsTrue(it.MoveNext());
                Assert.AreEqual("key", it.Current.Key);
                Assert.AreEqual("value", it.Current.Value.GetString());

                Assert.IsFalse(it.MoveNext());
            }

            {
                var json = "{\"key\":\"value\"}";
                var node = JsonParser.Parse(json);
                Assert.AreEqual(0, node.Value.Segment.Offset);
                Assert.AreEqual(json.Length, node.Value.Segment.Count);
                Assert.AreEqual(JsonValueType.Object, node.Value.ValueType);

                var it = node.ObjectItems.GetEnumerator();

                Assert.IsTrue(it.MoveNext());
                Assert.AreEqual("key", it.Current.Key);
                Assert.AreEqual("value", it.Current.Value.GetString());

                Assert.IsFalse(it.MoveNext());
            }
        }
Ejemplo n.º 2
0
 public void BooleanTest()
 {
     {
         var node = JsonParser.Parse("true");
         Assert.AreEqual(0, node.Value.Segment.Offset);
         Assert.AreEqual(4, node.Value.Segment.Count);
         Assert.AreEqual(JsonValueType.Boolean, node.Value.ValueType);
         Assert.AreEqual(true, node.GetBoolean());
         Assert.Catch(typeof(FormatException), () => node.GetDouble());
     }
     {
         var node = JsonParser.Parse(" false ");
         Assert.AreEqual(1, node.Value.Segment.Offset);
         Assert.AreEqual(5, node.Value.Segment.Count);
         Assert.AreEqual(JsonValueType.Boolean, node.Value.ValueType);
         Assert.AreEqual(false, node.GetBoolean());
     }
 }
        public void KeyValue()
        {
            var p = new Point
            {
                X      = 1,
                Vector = new float[] { 1, 2, 3 }
            };

            var f = new JsonFormatter();

            f.BeginMap();
            f.KeyValue(() => p.Vector);
            f.EndMap();

            var json = JsonParser.Parse(new Utf8String(f.GetStoreBytes()));

            Assert.AreEqual(1, json.GetObjectCount());
            Assert.AreEqual(1, json["Vector"][0].GetInt32());
        }
Ejemplo n.º 4
0
        public void CreateFromClass()
        {
            var s = JsonSchema.FromType <Person>();

            Assert.AreEqual("Person", s.Title);

            var v = s.Validator as JsonObjectValidator;

            Assert.AreEqual("Age in years", v.Properties["age"].Description);
            Assert.AreEqual(new[] { "firstName", "lastName" }, v.Required);

            var f = new JsonFormatter(2);

            s.ToJson(f);
            var json = f.ToString();

            var parsed = JsonParser.Parse(json);

            Assert.AreEqual(0, parsed["properties"]["age"]["minimum"].GetInt32());
        }
Ejemplo n.º 5
0
 public void NumberTest()
 {
     {
         var node = JsonParser.Parse("1");
         Assert.AreEqual(0, node.Value.Segment.Offset);
         Assert.AreEqual(1, node.Value.Segment.Count);
         Assert.AreEqual(JsonValueType.Integer, node.Value.ValueType);
         Assert.AreEqual(1, (int)node.GetDouble());
         Assert.Catch(typeof(JsonValueException), () => node.GetBoolean());
     }
     {
         var node = JsonParser.Parse(" 22 ");
         Assert.AreEqual(1, node.Value.Segment.Offset);
         Assert.AreEqual(2, node.Value.Segment.Count);
         Assert.AreEqual(JsonValueType.Integer, node.Value.ValueType);
         Assert.AreEqual(22, (int)node.GetDouble());
     }
     {
         var node = JsonParser.Parse(" 3.3 ");
         Assert.AreEqual(1, node.Value.Segment.Offset);
         Assert.AreEqual(3, node.Value.Segment.Count);
         Assert.AreEqual(JsonValueType.Number, node.Value.ValueType);
         Assert.AreEqual(3, (int)node.GetDouble());
         Assert.AreEqual(3.3f, (float)node.GetDouble());
     }
     {
         var node = JsonParser.Parse(" -4.44444444444444444444 ");
         Assert.AreEqual(JsonValueType.Number, node.Value.ValueType);
         Assert.AreEqual(-4, (int)node.GetDouble());
         Assert.AreEqual(-4.44444444444444444444, node.GetDouble());
     }
     {
         var node = JsonParser.Parse(" -5e-4 ");
         Assert.AreEqual(JsonValueType.Number, node.Value.ValueType);
         Assert.AreEqual(0, (int)node.GetDouble());
         Assert.AreEqual(-5e-4, node.GetDouble());
     }
 }
Ejemplo n.º 6
0
        public void NestedObjectTest()
        {
            {
                var json = "{\"key\":{ \"nestedKey\": \"nestedValue\" }, \"key2\": { \"nestedKey2\": \"nestedValue2\" } }";
                var node = JsonParser.Parse(json);
                Assert.AreEqual(JsonValueType.Object, node.Value.ValueType);

                {
                    var it = node.ObjectItems.GetEnumerator();

                    Assert.IsTrue(it.MoveNext());
                    Assert.AreEqual("key", it.Current.Key);
                    Assert.AreEqual(JsonValueType.Object, it.Current.Value.Value.ValueType);

                    Assert.IsTrue(it.MoveNext());
                    Assert.AreEqual("key2", it.Current.Key);
                    Assert.AreEqual(JsonValueType.Object, it.Current.Value.Value.ValueType);

                    Assert.IsFalse(it.MoveNext());
                }

                var nested = node["key2"];

                {
                    var it = nested.ObjectItems.GetEnumerator();

                    Assert.IsTrue(it.MoveNext());
                    Assert.AreEqual("nestedKey2", it.Current.Key);
                    Assert.AreEqual("nestedValue2", it.Current.Value.GetString());

                    Assert.IsFalse(it.MoveNext());
                }

                Assert.AreEqual("nestedValue2", node["key2"]["nestedKey2"].GetString());
            }
        }
Ejemplo n.º 7
0
        public void StringTest()
        {
            {
                var value  = "hoge";
                var quoted = "\"hoge\"";
                Assert.AreEqual(quoted, JsonString.Quote(value));
                var node = JsonParser.Parse(quoted);
                Assert.AreEqual(0, node.Value.Segment.Offset);
                Assert.AreEqual(quoted.Length, node.Value.Segment.Count);
                Assert.AreEqual(JsonValueType.String, node.Value.ValueType);
                Assert.AreEqual("hoge", node.GetString());
            }

            {
                var value  = "fuga\n  hoge";
                var quoted = "\"fuga\\n  hoge\"";
                Assert.AreEqual(quoted, JsonString.Quote(value));
                var node = JsonParser.Parse(quoted);
                Assert.AreEqual(0, node.Value.Segment.Offset);
                Assert.AreEqual(quoted.Length, node.Value.Segment.Count);
                Assert.AreEqual(JsonValueType.String, node.Value.ValueType);
                Assert.AreEqual(value, node.GetString());
            }
        }
Ejemplo n.º 8
0
        public void StringTest()
        {
            {
                var value  = "hoge";
                var quoted = "\"hoge\"";
                Assert.AreEqual(quoted, JsonString.Quote(value));
                var node = JsonParser.Parse(quoted);
                Assert.AreEqual(0, node.Value.Bytes.Offset);
                Assert.AreEqual(quoted.Length, node.Value.Bytes.Count);
                Assert.True(node.IsString());
                Assert.AreEqual("hoge", node.GetString());
            }

            {
                var value  = "fuga\n  hoge";
                var quoted = "\"fuga\\n  hoge\"";
                Assert.AreEqual(quoted, JsonString.Quote(value));
                var node = JsonParser.Parse(quoted);
                Assert.AreEqual(0, node.Value.Bytes.Offset);
                Assert.AreEqual(quoted.Length, node.Value.Bytes.Count);
                Assert.True(node.IsString());
                Assert.AreEqual(value, node.GetString());
            }
        }
Ejemplo n.º 9
0
        public void ParseTest()
        {
            var json = "{";

            Assert.Catch(typeof(JsonParseException), () => JsonParser.Parse(json));
        }
Ejemplo n.º 10
0
 public static ListTreeNode <JsonValue> ParseAsJson(this ArraySegment <byte> bytes)
 {
     return(JsonParser.Parse(new Utf8String(bytes)));
 }
Ejemplo n.º 11
0
 public static JsonNode ParseAsJson(this string json)
 {
     return(JsonParser.Parse(json));
 }
Ejemplo n.º 12
0
 public static JsonNode ParseAsJson(this ArraySegment <byte> bytes)
 {
     return(JsonParser.Parse(new Utf8String(bytes)));
 }
Ejemplo n.º 13
0
 public static JsonNode ParseAsJson(this byte[] bytes)
 {
     return(JsonParser.Parse(new Utf8String(bytes)));
 }
Ejemplo n.º 14
0
 public static ListTreeNode <JsonValue> ParseAsJson(this string json)
 {
     return(JsonParser.Parse(json));
 }
Ejemplo n.º 15
0
 public void Utf8Test()
 {
     JsonParser.Parse("\"5\"");
 }
Ejemplo n.º 16
0
        public void Parse(IFileSystemAccessor fs, JsonNode root, string Key)
        {
            m_context.Push(Key);

            var compositionType = default(CompositionType);
            var composition     = new List <JsonSchema>();

            foreach (var kv in root.ObjectItems)
            {
                //Console.WriteLine(kv.Key);
                switch (kv.Key)
                {
                case "$schema":
                    Schema = kv.Value.GetString();
                    break;

                case "$ref":
                {
                    var refFs = fs.Get(kv.Value.GetString());

                    // parse JSON
                    var json    = refFs.ReadAllText();
                    var refRoot = JsonParser.Parse(json);

                    Parse(refFs, refRoot, "$ref");
                }
                break;

                    #region Annotation
                case "title":
                    Title = kv.Value.GetString();
                    break;

                case "description":
                    Description = kv.Value.GetString();
                    break;

                case "default":
                    Default = kv.Value.Value.Segment;
                    break;
                    #endregion

                    #region Validation
                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1
                case "type":
                    Validator = JsonSchemaValidatorFactory.Create(kv.Value.GetString());
                    break;

                case "enum":
                    Validator = JsonEnumValidator.Create(kv.Value);
                    break;

                case "const":
                    break;
                    #endregion

                    #region Composite
                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.7
                case "oneOf":
                    break;

                case "not":
                    break;

                case "anyOf":     // composition
                case "allOf":     // composition
                {
                    compositionType = (CompositionType)Enum.Parse(typeof(CompositionType), kv.Key, true);
                    foreach (var item in kv.Value.ArrayItems)
                    {
                        if (item.ContainsKey("$ref"))
                        {
                            var sub = JsonSchema.ParseFromPath(fs.Get(item["$ref"].GetString()));
                            composition.Add(sub);
                        }
                        else
                        {
                            var sub = new JsonSchema();
                            sub.Parse(fs, item, compositionType.ToString());
                            composition.Add(sub);
                        }
                    }
                    Composite(compositionType, composition);
                }
                break;
                    #endregion

                // http://json-schema.org/latest/json-schema-validation.html#rfc.section.7
                case "format":
                    break;

                    #region Gltf
                case "gltf_detailedDescription":
                    break;

                case "gltf_webgl":
                    break;

                case "gltf_uriType":
                    break;
                    #endregion

                default:
                {
                    if (Validator != null)
                    {
                        if (Validator.Parse(fs, kv.Key, kv.Value))
                        {
                            continue;
                        }
                    }
                    throw new NotImplementedException(string.Format("unknown key: {0}", kv.Key));
                }
                }
            }
            m_context.Pop();

            if (Validator == null)
            {
                SkipComparison = true;
            }
        }
Ejemplo n.º 17
0
 public static ListTreeNode <JsonValue> ParseAsJson(this byte[] bytes)
 {
     return(JsonParser.Parse(new Utf8String(bytes)));
 }