コード例 #1
0
        public static XNodeArray Create <T>(T[] source, Func <T, JsonNode> selector)
        {
            if (source == null || selector == null)
            {
                return(null);
            }
            var result = new XNodeArray();

            foreach (var item in source.Select(selector))
            {
                result.SafeAdd(item);
            }
            return(result);
        }
コード例 #2
0
        public KeyValuePair <string, JsonNode> ToJson()
        {
            var requests = new XNodeArray();

            foreach (var request in Requests)
            {
                if (request is PutRequest)
                {
                    requests.Add(new JsonObject {
                        { "PutRequest", request.ToJson() }
                    });
                }
                else if (request is DeleteRequest)
                {
                    requests.Add(new JsonObject {
                        { "DeleteRequest", request.ToJson() }
                    });
                }
            }

            return(new KeyValuePair <string, JsonNode>(TableName, requests));
        }
コード例 #3
0
        public JsonArray ReadArray()
        {
            reader.Ensure(TokenKind.LeftBracket, "array");

            var array = new XNodeArray();

            reader.Next(); // Read the '[' (Array start)

            // Read the array's items
            while (reader.Current.Kind != TokenKind.RightBracket)
            {
                if (reader.Current.Kind == TokenKind.Comma)
                {
                    reader.Next(); // Read the ',' (Seperator)
                }

                if (reader.Current.IsLiteral)
                {
                    array.Add(ReadLiteral()); // Boolean, Date, Number, Null, String, Uri
                }
                else if (reader.Current.Kind == TokenKind.LeftBracket)
                {
                    array.Add(ReadArray()); // Array
                }
                else if (reader.Current.Kind == TokenKind.LeftBrace)
                {
                    array.Add(ReadObject()); // Object
                }
                else
                {
                    throw new ParserException($"Expected comma, literal, or object. Was {reader.Current}.");
                }
            }

            reader.Next(); // Read the ']' (Array end)

            return(array);
        }
コード例 #4
0
        /// <summary>
        /// Serializes an enumerable and returns a JsonNode.
        /// </summary>
        /// <param name="enumerable">an IEnumerable collection of items</param>
        /// <returns>A JsonNode that contains the collection of items serialized.</returns>
        private static JsonNode ToJsonValue(System.Collections.IEnumerable enumerable)
        {
            if (enumerable != null)
            {
                // is it a byte array of some kind?
                if (enumerable is System.Collections.Generic.IEnumerable <byte> byteEnumerable)
                {
                    return(new XBinary(System.Linq.Enumerable.ToArray(byteEnumerable)));
                }

                var hasValues = false;
                // just create an array of value nodes.
                var result = new XNodeArray();
                foreach (var each in enumerable)
                {
                    // we had at least one value.
                    hasValues = true;

                    // try to serialize it.
                    var node = ToJsonValue(each);
                    if (null != node)
                    {
                        result.Add(node);
                    }
                }

                // if we were able to add values, (or it was just empty), return it.
                if (result.Count > 0 || !hasValues)
                {
                    return(result);
                }
            }

            // we couldn't serialize the values. Sorry.
            return(null);
        }
コード例 #5
0
ファイル: JsonParser.cs プロジェクト: carbon/Data
        public JsonArray ReadArray()
        {
            reader.Ensure(TokenKind.LeftBracket, "array");

            var array = new XNodeArray();

            reader.Next(); // Read the '[' (Array start)

            // Read the array's items
            while (reader.Current.Kind != TokenKind.RightBracket)
            {
                if (reader.Current.Kind == TokenKind.Comma)
                {
                    reader.Next(); // Read the ',' (Seperator)
                }

                if (reader.Current.IsLiteral)
                {
                    array.Add(ReadLiteral()); // Boolean, Date, Number, Null, String, Uri
                }
                else if (reader.Current.Kind == TokenKind.LeftBracket)
                {
                    array.Add(ReadArray()); // Array
                }
                else if (reader.Current.Kind == TokenKind.LeftBrace)
                {
                    array.Add(ReadObject()); // Object
                }
                else
                {
                    throw new ParserException($"Expected comma, literal, or object. Was {reader.Current}.");
                }
            }

            reader.Next(); // Read the ']' (Array end)

            return array;
        }
コード例 #6
0
        public JsonObject ToJson()
        {
            // {"N":"225"}
            // {"S":"Hello"}
            // {"SS": ["Keneau", "Alexis", "John"]}
            // {"NS": ["1", "2", "3"]}

            JsonNode node;

            if (kind == DbValueType.M)
            {
                node = ((AttributeCollection)value).ToJson();
            }
            else if (kind == DbValueType.B && value is byte[] data)
            {
                node = new JsonString(Convert.ToBase64String(data));
            }
            else if (kind == DbValueType.L)
            {
                var list = new XNodeArray();

                foreach (var item in (IEnumerable <DbValue>)value)
                {
                    list.Add(item.ToJson());
                }

                node = list;
            }
            else if (value.GetType().IsArray)
            {
                var elementType = value.GetType().GetElementType();

                if (elementType == typeof(string))
                {
                    node = new XImmutableArray <string>((string[])value);
                }
                else
                {
                    var list = new List <string>();

                    foreach (var item in (IEnumerable)value)
                    {
                        list.Add(item.ToString());
                    }

                    node = new XList <string>(list);
                }
            }
            else if (kind == DbValueType.BOOL)
            {
                var val = (bool)value;

                node = val ? JsonBoolean.True : JsonBoolean.False;
            }
            else
            {
                node = new JsonString(value.ToString());
            }

            return(new JsonObject {
                { kind.ToQuickString(), node }
            });
        }