static private Parse ( UniJSON.Utf8String segment, List values, int parentIndex ) : UniJSON.JsonValue | ||
segment | UniJSON.Utf8String | |
values | List | |
parentIndex | int | |
Résultat | UniJSON.JsonValue |
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()); } }
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()); }
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()); }
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()); } }
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()); } }
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()); } }
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()); } }
public void ParseTest() { var json = "{"; Assert.Catch(typeof(JsonParseException), () => JsonParser.Parse(json)); }
public static ListTreeNode <JsonValue> ParseAsJson(this ArraySegment <byte> bytes) { return(JsonParser.Parse(new Utf8String(bytes))); }
public static JsonNode ParseAsJson(this string json) { return(JsonParser.Parse(json)); }
public static JsonNode ParseAsJson(this ArraySegment <byte> bytes) { return(JsonParser.Parse(new Utf8String(bytes))); }
public static JsonNode ParseAsJson(this byte[] bytes) { return(JsonParser.Parse(new Utf8String(bytes))); }
public static ListTreeNode <JsonValue> ParseAsJson(this string json) { return(JsonParser.Parse(json)); }
public void Utf8Test() { JsonParser.Parse("\"5\""); }
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; } }
public static ListTreeNode <JsonValue> ParseAsJson(this byte[] bytes) { return(JsonParser.Parse(new Utf8String(bytes))); }