コード例 #1
0
ファイル: JsonParserTest.cs プロジェクト: vc-kusuha/UniJSON
        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
  hoge";
                var quoted = "\"fuga\\r\\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());
            }
        }
コード例 #2
0
 public void SetValue(string jsonPointer, string value)
 {
     SetValue(new JsonPointer(jsonPointer), parentIndex => new JsonValue
     {
         ParentIndex = parentIndex,
         Segment     = new StringSegment(JsonString.Quote(value)),
         ValueType   = JsonValueType.String
     });
 }
コード例 #3
0
ファイル: JsonFormatter.cs プロジェクト: gpsnmeajp/Oredayo4V
 void _Value(Utf8String key, bool isKey)
 {
     CommaCheck(isKey);
     if (isKey)
     {
         Indent();
     }
     JsonString.Quote(key, m_w);
 }
コード例 #4
0
        public void QuoteTest()
        {
            {
                var value  = Utf8String.From("ho5日本語ge");
                var quoted = Utf8String.From("\"ho5日本語ge\"");
                Assert.AreEqual(quoted, JsonString.Quote(value));
                Assert.AreEqual(value, JsonString.Unquote(quoted));
            }

            {
                var value  = Utf8String.From("fuga\n  ho5日本語ge");
                var quoted = Utf8String.From("\"fuga\\n  ho5日本語ge\"");
                Assert.AreEqual(quoted, JsonString.Quote(value));
                Assert.AreEqual(value, JsonString.Unquote(quoted));
            }
        }
コード例 #5
0
ファイル: JsonParserTest.cs プロジェクト: gpsnmeajp/Oredayo4V
        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());
            }
        }
コード例 #6
0
        public IEnumerable <JsonNode> GetNodes(JsonPointer jsonPointer)
        {
            if (jsonPointer.Path.Count == 0)
            {
                yield return(this);

                yield break;
            }

            if (Value.ValueType == JsonValueType.Array)
            {
                // array
                if (jsonPointer[0] == "*")
                {
                    // wildcard
                    foreach (var child in ArrayItems)
                    {
                        foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                        {
                            yield return(childChild);
                        }
                    }
                }
                else
                {
                    int index;
                    if (!int.TryParse(jsonPointer[0], out index))
                    {
                        throw new KeyNotFoundException();
                    }
                    var child = this[index];
                    foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                    {
                        yield return(childChild);
                    }
                }
            }
            else if (Value.ValueType == JsonValueType.Object)
            {
                // object
                if (jsonPointer[0] == "*")
                {
                    // wildcard
                    foreach (var kv in ObjectItems)
                    {
                        foreach (var childChild in kv.Value.GetNodes(jsonPointer.Unshift()))
                        {
                            yield return(childChild);
                        }
                    }
                }
                else
                {
                    JsonNode child;
                    try
                    {
                        child = this[jsonPointer[0]];
                    }
                    catch (KeyNotFoundException)
                    {
                        // key
                        Values.Add(new JsonValue(new StringSegment(JsonString.Quote(jsonPointer[0])), JsonValueType.String, m_index));
                        // value
                        Values.Add(new JsonValue(new StringSegment(), JsonValueType.Object, m_index));

                        child = this[jsonPointer[0]];
                    }
                    foreach (var childChild in child.GetNodes(jsonPointer.Unshift()))
                    {
                        yield return(childChild);
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
コード例 #7
0
 public JsonValue Key(Utf8String key, int parentIndex)
 {
     return(new JsonValue(JsonString.Quote(key), ValueNodeType.String, parentIndex));
 }