Exemplo n.º 1
0
        /// <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);
                    }
                }
            }
        }
Exemplo n.º 2
0
 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);
 }
Exemplo n.º 3
0
 public static JsonPointer Create(JsonTreeNode node)
 {
     return(new JsonPointer
     {
         Path = new ArraySegment <Utf8String>(node.Path().Skip(1).Select(x => GetKeyFromParent(x)).ToArray())
     });
 }
Exemplo n.º 4
0
 public static IEnumerable <JsonTreeNode> ArrayItems(this JsonTreeNode self)
 {
     if (!self.IsArray())
     {
         throw new DeserializationException("is not array");
     }
     return(self.Children);
 }
Exemplo n.º 5
0
 public static int GetObjectCount(this JsonTreeNode self)
 {
     if (!self.IsObject())
     {
         throw new DeserializationException("is not object");
     }
     return(self.Children.Count() / 2);
 }
Exemplo n.º 6
0
 public static int GetArrayCount(this JsonTreeNode self)
 {
     if (!self.IsArray())
     {
         throw new DeserializationException("is not array");
     }
     return(self.Children.Count());
 }
Exemplo n.º 7
0
 public static JsonDiff Create(JsonTreeNode node, JsonDiffType diffType, string msg)
 {
     return(new JsonDiff
     {
         Path = JsonPointer.Create(node),
         DiffType = diffType,
         Msg = msg,
     });
 }
Exemplo n.º 8
0
 public U Create(JsonTreeNode src)
 {
     if (s_creator == null)
     {
         var d = GetCreator();
         s_creator = new Creator(d);
     }
     return(s_creator(src));
 }
Exemplo n.º 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]);
        }
Exemplo n.º 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));
     }
 }
Exemplo n.º 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);
 }
Exemplo n.º 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
     }
 }
Exemplo n.º 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();
 }
Exemplo n.º 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();
        }
Exemplo n.º 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();
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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();
        }
Exemplo n.º 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));
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 21
0
 public static ulong GetUInt64(this JsonTreeNode self)
 {
     return(self.Value.GetUInt64());
 }
Exemplo n.º 22
0
 public static uint GetUInt32(this JsonTreeNode self)
 {
     return(self.Value.GetUInt32());
 }
Exemplo n.º 23
0
 public static ushort GetUInt16(this JsonTreeNode self)
 {
     return(self.Value.GetUInt16());
 }
Exemplo n.º 24
0
 public static byte GetByte(this JsonTreeNode self)
 {
     return(self.Value.GetByte());
 }
Exemplo n.º 25
0
 public static bool IsNull(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Null);
 }
Exemplo n.º 26
0
 public static Utf8String GetUtf8String(this JsonTreeNode self)
 {
     return(self.Value.GetUtf8String());
 }
Exemplo n.º 27
0
 public static bool GetBoolean(this JsonTreeNode self)
 {
     return(self.Value.GetBoolean());
 }
Exemplo n.º 28
0
 public static bool IsObject(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Object);
 }
Exemplo n.º 29
0
 public static bool IsArray(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Array);
 }
Exemplo n.º 30
0
 public static bool IsInteger(this JsonTreeNode self)
 {
     return(self.Value.ValueType == ValueNodeType.Integer);
 }