예제 #1
0
    /// <summary>
    /// Parses the i dictionary.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <param name="dictType">Type of the dictionary.</param>
    /// <returns>IDictionary.</returns>
    public static IDictionary ParseIDictionary(ReadOnlySpan <char> value, Type dictType)
    {
        if (value.IsEmpty)
        {
            return(null);
        }

        var index = VerifyAndGetStartIndex(value, dictType);

        var valueParseMethod = Serializer.GetParseStringSpanFn(typeof(object));

        if (valueParseMethod == null)
        {
            return(null);
        }

        var to = (IDictionary)dictType.CreateInstance();

        if (Json.JsonTypeSerializer.IsEmptyMap(value, index))
        {
            return(to);
        }

        var valueLength = value.Length;

        while (index < valueLength)
        {
            var keyValue = Serializer.EatMapKey(value, ref index);
            Serializer.EatMapKeySeperator(value, ref index);
            var elementStartIndex = index;
            var elementValue      = Serializer.EatTypeValue(value, ref index);
            if (keyValue.IsEmpty)
            {
                continue;
            }

            var mapKey = valueParseMethod(keyValue);

            if (elementStartIndex < valueLength)
            {
                Serializer.EatWhitespace(value, ref elementStartIndex);
                to[mapKey] = DeserializeType <TSerializer> .ParsePrimitive(elementValue.Value(), value[elementStartIndex]);
            }
            else
            {
                to[mapKey] = valueParseMethod(elementValue);
            }

            Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
        }

        return(to);
    }
        public static object ParseKeyValuePair <TKey, TValue>(
            ReadOnlySpan <char> value, Type createMapType,
            ParseStringSpanDelegate parseKeyFn, ParseStringSpanDelegate parseValueFn)
        {
            if (value.IsEmpty)
            {
                return(default(KeyValuePair <TKey, TValue>));
            }

            var index = VerifyAndGetStartIndex(value, createMapType);

            if (JsonTypeSerializer.IsEmptyMap(value, index))
            {
                return(new KeyValuePair <TKey, TValue>());
            }
            var keyValue   = default(TKey);
            var valueValue = default(TValue);

            var valueLength = value.Length;

            while (index < valueLength)
            {
                var key = Serializer.EatMapKey(value, ref index);
                Serializer.EatMapKeySeperator(value, ref index);
                var keyElementValue = Serializer.EatTypeValue(value, ref index);

                if (key.CompareIgnoreCase("key".AsSpan()))
                {
                    keyValue = (TKey)parseKeyFn(keyElementValue);
                }
                else if (key.CompareIgnoreCase("value".AsSpan()))
                {
                    valueValue = (TValue)parseValueFn(keyElementValue);
                }
                else
                {
                    throw new SerializationException("Incorrect KeyValuePair property: " + key.ToString());
                }

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(new KeyValuePair <TKey, TValue>(keyValue, valueValue));
        }
예제 #3
0
        public static object ParseKeyValuePair <TKey, TValue>(
            string value, Type createMapType,
            ParseStringDelegate parseKeyFn, ParseStringDelegate parseValueFn)
        {
            if (value == null)
            {
                return(default(KeyValuePair <TKey, TValue>));
            }

            var index = 1;

            if (JsonTypeSerializer.IsEmptyMap(value))
            {
                return(new KeyValuePair <TKey, TValue>());
            }
            var keyValue   = default(TKey);
            var valueValue = default(TValue);

            var valueLength = value.Length;

            while (index < valueLength)
            {
                var key = Serializer.EatMapKey(value, ref index);
                Serializer.EatMapKeySeperator(value, ref index);
                var keyElementValue = Serializer.EatTypeValue(value, ref index);

                if (string.Compare(key, "key", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    keyValue = (TKey)parseKeyFn(keyElementValue);
                }
                else if (string.Compare(key, "value", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    valueValue = (TValue)parseValueFn(keyElementValue);
                }
                else
                {
                    throw new SerializationException("Incorrect KeyValuePair property: " + key);
                }
                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(new KeyValuePair <TKey, TValue>(keyValue, valueValue));
        }
예제 #4
0
        public static IDictionary <TKey, TValue> ParseDictionary <TKey, TValue>(
            string value, Type createMapType,
            ParseStringDelegate parseKeyFn, ParseStringDelegate parseValueFn)
        {
            if (value == null)
            {
                return(null);
            }

            var tryToParseItemsAsDictionaries =
                JsConfig.ConvertObjectTypesIntoStringDictionary && typeof(TValue) == typeof(object);
            var tryToParseItemsAsPrimitiveTypes =
                JsConfig.TryToParsePrimitiveTypeValues && typeof(TValue) == typeof(object);

            var index = VerifyAndGetStartIndex(value, createMapType);

            var to = (createMapType == null)
                ? new Dictionary <TKey, TValue>()
                : (IDictionary <TKey, TValue>)createMapType.CreateInstance();

            if (JsonTypeSerializer.IsEmptyMap(value, index))
            {
                return(to);
            }

            var valueLength = value.Length;

            while (index < valueLength)
            {
                var keyValue = Serializer.EatMapKey(value, ref index);
                Serializer.EatMapKeySeperator(value, ref index);
                var elementStartIndex = index;
                var elementValue      = Serializer.EatTypeValue(value, ref index);
                if (keyValue == null)
                {
                    continue;
                }

                TKey mapKey = (TKey)parseKeyFn(keyValue);

                if (tryToParseItemsAsDictionaries)
                {
                    Serializer.EatWhitespace(value, ref elementStartIndex);
                    if (elementStartIndex < valueLength && value[elementStartIndex] == JsWriter.MapStartChar)
                    {
                        var tmpMap = ParseDictionary <TKey, TValue>(elementValue, createMapType, parseKeyFn, parseValueFn);
                        if (tmpMap != null && tmpMap.Count > 0)
                        {
                            to[mapKey] = (TValue)tmpMap;
                        }
                    }
                    else if (elementStartIndex < valueLength && value[elementStartIndex] == JsWriter.ListStartChar)
                    {
                        to[mapKey] = (TValue)DeserializeList <List <object>, TSerializer> .Parse(elementValue);
                    }
                    else
                    {
                        to[mapKey] = (TValue)(tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength
                                        ? DeserializeType <TSerializer> .ParsePrimitive(elementValue, value[elementStartIndex])
                                        : parseValueFn(elementValue));
                    }
                }
                else
                {
                    if (tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength)
                    {
                        Serializer.EatWhitespace(value, ref elementStartIndex);
                        to[mapKey] = (TValue)DeserializeType <TSerializer> .ParsePrimitive(elementValue, value[elementStartIndex]);
                    }
                    else
                    {
                        to[mapKey] = (TValue)parseValueFn(elementValue);
                    }
                }

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(to);
        }