/// <summary>
        /// for UnitTest. Use explicit GetT() or Deserialize(ref T)
        /// </summary>
        /// <returns></returns>
        // public static object GetValue(this JsonTreeNode self)
        // {
        //     return self.Value.GetValue<object>();
        // }
        #endregion

        public static IEnumerable <JsonTreeNode> Traverse(this JsonTreeNode self)
        {
            yield return(self);

            if (self.IsArray())
            {
                foreach (var x in self.ArrayItems())
                {
                    foreach (var y in x.Traverse())
                    {
                        yield return(y);
                    }
                }
            }
            else if (self.IsObject())
            {
                foreach (var kv in self.ObjectItems())
                {
                    foreach (var y in kv.Value.Traverse())
                    {
                        yield return(y);
                    }
                }
            }
        }
 public static bool IsFloat(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Number ||
            self.Value.ValueType == ValueNodeType.NaN ||
            self.Value.ValueType == ValueNodeType.Infinity ||
            self.Value.ValueType == ValueNodeType.MinusInfinity);
 }
Beispiel #3
0
 public static JsonPointer Create(JsonTreeNode node)
 {
     return(new JsonPointer
     {
         Path = new ArraySegment <Utf8String>(node.Path().Skip(1).Select(x => GetKeyFromParent(x)).ToArray())
     });
 }
Beispiel #4
0
 public static IEnumerable <JsonTreeNode> ArrayItems(this JsonTreeNode self)
 {
     if (!self.IsArray())
     {
         throw new DeserializationException("is not array");
     }
     return(self.Children);
 }
Beispiel #5
0
 public static int GetObjectCount(this JsonTreeNode self)
 {
     if (!self.IsObject())
     {
         throw new DeserializationException("is not object");
     }
     return(self.Children.Count() / 2);
 }
Beispiel #6
0
 public static int GetArrayCount(this JsonTreeNode self)
 {
     if (!self.IsArray())
     {
         throw new DeserializationException("is not array");
     }
     return(self.Children.Count());
 }
Beispiel #7
0
 public static JsonDiff Create(JsonTreeNode node, JsonDiffType diffType, string msg)
 {
     return(new JsonDiff
     {
         Path = JsonPointer.Create(node),
         DiffType = diffType,
         Msg = msg,
     });
 }
Beispiel #8
0
 public U Create(JsonTreeNode src)
 {
     if (s_creator == null)
     {
         var d = GetCreator();
         s_creator = new Creator(d);
     }
     return(s_creator(src));
 }
Beispiel #9
0
        static V[] ArrayCreator <V>(JsonTreeNode src)
        {
            if (!src.IsArray())
            {
                throw new ArgumentException("value is not array");
            }
            var count = src.GetArrayCount();

            return(new V[count]);
        }
Beispiel #10
0
 public static void SetValue(this JsonTreeNode self,
                             Utf8String jsonPointer, ArraySegment <Byte> bytes)
 {
     foreach (var node in self.GetNodes(jsonPointer))
     {
         node.SetValue(default(JsonValue).New(
                           bytes,
                           ValueNodeType.Boolean,
                           node.Value.ParentIndex));
     }
 }
Beispiel #11
0
 public static IEnumerable <JsonTreeNode> Path(this JsonTreeNode self)
 {
     if (self.HasParent)
     {
         foreach (var x in self.Parent.Path())
         {
             yield return(x);
         }
     }
     yield return(self);
 }
Beispiel #12
0
 public static void RemoveValue(this JsonTreeNode self, Utf8String jsonPointer)
 {
     foreach (var node in self.GetNodes(new JsonPointer(jsonPointer)))
     {
         if (node.Parent.IsObject())
         {
             node.Prev.SetValue(default(JsonValue)); // remove key
         }
         node.SetValue(default(JsonValue));          // remove
     }
 }
Beispiel #13
0
 public static Utf8String KeyOf(this JsonTreeNode self, JsonTreeNode node)
 {
     foreach (var kv in self.ObjectItems())
     {
         if (node.ValueIndex == kv.Value.ValueIndex)
         {
             return(kv.Key.GetUtf8String());
         }
     }
     throw new KeyNotFoundException();
 }
Beispiel #14
0
        public static JsonTreeNode GetObjectItem(this JsonTreeNode self, Utf8String key)

        {
            foreach (var kv in self.ObjectItems())
            {
                if (kv.Key.GetUtf8String() == key)
                {
                    return(kv.Value);
                }
            }
            throw new KeyNotFoundException();
        }
Beispiel #15
0
        public static JsonTreeNode GetArrrayItem(this JsonTreeNode self, int index)
        {
            int i = 0;

            foreach (var v in self.ArrayItems())
            {
                if (i++ == index)
                {
                    return(v);
                }
            }
            throw new KeyNotFoundException();
        }
Beispiel #16
0
        static JsonTreeNode ParseString(JsonTreeNode tree, Utf8String segment)
        {
            int pos;

            if (segment.TrySearchAscii((Byte)'"', 1, out pos))
            {
                return(tree.AddValue(segment.Subbytes(0, pos + 1).Bytes, ValueNodeType.String));
            }
            else
            {
                throw new ParserException("no close string: " + segment);
            }
        }
Beispiel #17
0
        public static int IndexOf(this JsonTreeNode self, JsonTreeNode child)
        {
            int i = 0;

            foreach (var v in self.ArrayItems())
            {
                if (v.ValueIndex == child.ValueIndex)
                {
                    return(i);
                }
                ++i;
            }
            throw new KeyNotFoundException();
        }
Beispiel #18
0
        /// <summary>
        /// Expected null, boolean, integer, number
        /// </summary>
        /// <param name="segment"></param>
        /// <param name="valueType"></param>
        /// <param name="parentIndex"></param>
        /// <returns></returns>
        static JsonTreeNode ParsePrimitive(JsonTreeNode tree, Utf8String segment, ValueNodeType valueType)
        {
            int i = 1;

            for (; i < segment.ByteLength; ++i)
            {
                if (Char.IsWhiteSpace((char)segment[i]) ||
                    segment[i] == '}' ||
                    segment[i] == ']' ||
                    segment[i] == ',' ||
                    segment[i] == ':'
                    )
                {
                    break;
                }
            }
            return(tree.AddValue(segment.Subbytes(0, i).Bytes, valueType));
        }
Beispiel #19
0
        static Utf8String GetKeyFromParent(JsonTreeNode json)
        {
            var parent = json.Parent;

            if (parent.IsArray())
            {
                var index = parent.IndexOf(json);
                return(Utf8String.From(index));
            }
            else if (parent.IsObject())
            {
                return(parent.KeyOf(json));
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Beispiel #20
0
        public static JsonTreeNode Parse(JsonTreeNode tree, Utf8String segment)
        {
            // skip white space
            int pos;

            if (!segment.TrySearchByte(x => !char.IsWhiteSpace((char)x), out pos))
            {
                throw new ParserException("only whitespace");
            }
            segment = segment.Subbytes(pos);

            var valueType = GetValueType(segment);

            switch (valueType)
            {
            case ValueNodeType.Boolean:
            case ValueNodeType.Integer:
            case ValueNodeType.Number:
            case ValueNodeType.Null:
            case ValueNodeType.NaN:
            case ValueNodeType.Infinity:
            case ValueNodeType.MinusInfinity:
                return(ParsePrimitive(tree, segment, valueType));

            case ValueNodeType.String:
                return(ParseString(tree, segment));

            case ValueNodeType.Array:     // fall through
                return(ParseArray(tree, segment));

            case ValueNodeType.Object:     // fall through
                return(ParseObject(tree, segment));

            default:
                throw new NotImplementedException();
            }
        }
 public static ulong GetUInt64(this JsonTreeNode self)
 {
     return(self.Value.GetUInt64());
 }
 public static uint GetUInt32(this JsonTreeNode self)
 {
     return(self.Value.GetUInt32());
 }
 public static ushort GetUInt16(this JsonTreeNode self)
 {
     return(self.Value.GetUInt16());
 }
 public static byte GetByte(this JsonTreeNode self)
 {
     return(self.Value.GetByte());
 }
 public static bool IsNull(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Null);
 }
 public static Utf8String GetUtf8String(this JsonTreeNode self)
 {
     return(self.Value.GetUtf8String());
 }
 public static bool GetBoolean(this JsonTreeNode self)
 {
     return(self.Value.GetBoolean());
 }
 public static bool IsObject(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Object);
 }
 public static bool IsArray(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Array);
 }
 public static bool IsInteger(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Integer);
 }