public static object ObjectStringToType(string strType)
        {
            var type = ExtractType(strType);

            if (type != null)
            {
                var parseFn       = Serializer.GetParseFn(type);
                var propertyValue = parseFn(strType);
                return(propertyValue);
            }

            if (JsConfig.ConvertObjectTypesIntoStringDictionary && !string.IsNullOrEmpty(strType))
            {
                if (strType[0] == JsWriter.MapStartChar)
                {
                    var dynamicMatch = DeserializeDictionary <TSerializer> .ParseDictionary <string, object>(strType, null, Serializer.UnescapeString, Serializer.UnescapeString);

                    if (dynamicMatch != null && dynamicMatch.Count > 0)
                    {
                        return(dynamicMatch);
                    }
                }

                if (strType[0] == JsWriter.ListStartChar)
                {
                    return(DeserializeList <List <object>, TSerializer> .Parse(strType));
                }
            }

            return(Serializer.UnescapeString(strType));
        }
Example #2
0
        public static object ObjectStringToType(StringSegment strType)
        {
            var type = ExtractType(strType);

            if (type != null)
            {
                var parseFn       = Serializer.GetParseStringSegmentFn(type);
                var propertyValue = parseFn(strType);
                return(propertyValue);
            }

            if (JsConfig.ConvertObjectTypesIntoStringDictionary && !strType.IsNullOrEmpty())
            {
                if (strType.GetChar(0) == JsWriter.MapStartChar)
                {
                    var dynamicMatch = DeserializeDictionary <TSerializer> .ParseDictionary <string, object>(strType, null, v => Serializer.UnescapeString(v).Value, v => Serializer.UnescapeString(v).Value);

                    if (dynamicMatch != null && dynamicMatch.Count > 0)
                    {
                        return(dynamicMatch);
                    }
                }

                if (strType.GetChar(0) == JsWriter.ListStartChar)
                {
                    return(DeserializeList <List <object>, TSerializer> .ParseStringSegment(strType));
                }
            }

            return((JsConfig.TryToParsePrimitiveTypeValues
                ? ParsePrimitive(strType.Value)
                : null) ?? Serializer.UnescapeString(strType).Value);
        }
        public static object ObjectStringToType(ReadOnlySpan <char> strType)
        {
            var type = ExtractType(strType);

            if (type != null)
            {
                var parseFn       = Serializer.GetParseStringSpanFn(type);
                var propertyValue = parseFn(strType);
                return(propertyValue);
            }

            var config = JsConfig.GetConfig();

            if (config.ConvertObjectTypesIntoStringDictionary && !strType.IsNullOrEmpty())
            {
                var firstChar = strType[0];
                var endChar   = strType[strType.Length - 1];
                if (firstChar == JsWriter.MapStartChar && endChar == JsWriter.MapEndChar)
                {
                    var dynamicMatch = DeserializeDictionary <TSerializer> .ParseDictionary <string, object>(strType, null, v => Serializer.UnescapeString(v).ToString(), v => Serializer.UnescapeString(v).ToString());

                    if (dynamicMatch != null && dynamicMatch.Count > 0)
                    {
                        return(dynamicMatch);
                    }
                }

                if (firstChar == JsWriter.ListStartChar && endChar == JsWriter.ListEndChar)
                {
                    return(DeserializeList <List <object>, TSerializer> .ParseStringSpan(strType));
                }
            }

            var primitiveType = config.TryToParsePrimitiveTypeValues ? ParsePrimitive(strType) : null;

            if (primitiveType != null)
            {
                return(primitiveType);
            }

            if (Serializer.ObjectDeserializer != null && typeof(TSerializer) == typeof(Json.JsonTypeSerializer))
            {
                return(!strType.IsNullOrEmpty()
                    ? Serializer.ObjectDeserializer(strType)
                    : strType.Value());
            }

            return(Serializer.UnescapeString(strType).Value());
        }
        public static IDynamicMetaObjectProvider ParseDynamic(string value)
        {
            var index = VerifyAndGetStartIndex(value, typeof(ExpandoObject));

            var result = new ExpandoObject();

            if (JsonTypeSerializer.IsEmptyMap(value))
            {
                return(result);
            }

            var container = (IDictionary <String, Object>)result;

            var tryToParsePrimitiveTypes = JsConfig.TryToParsePrimitiveTypeValues;

            var valueLength = value.Length;

            while (index < valueLength)
            {
                var keyValue = Serializer.EatMapKey(value, ref index);
                Serializer.EatMapKeySeperator(value, ref index);
                var elementValue = Serializer.EatValue(value, ref index);

                var mapKey = Serializer.UnescapeString(keyValue);

                if (JsonUtils.IsJsObject(elementValue))
                {
                    container[mapKey] = ParseDynamic(elementValue);
                }
                else if (JsonUtils.IsJsArray(elementValue))
                {
                    container[mapKey] = DeserializeList <List <object>, TSerializer> .Parse(elementValue);
                }
                else if (tryToParsePrimitiveTypes)
                {
                    container[mapKey] = DeserializeType <TSerializer> .ParsePrimitive(elementValue) ?? Serializer.UnescapeString(elementValue);
                }
                else
                {
                    container[mapKey] = Serializer.UnescapeString(elementValue);
                }

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(result);
        }
        public static IDictionary <TKey, TValue> ParseDictionary <TKey, TValue>(
            StringSegment value, Type createMapType,
            ParseStringSegmentDelegate parseKeyFn, ParseStringSegmentDelegate parseValueFn)
        {
            if (!value.HasValue)
            {
                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.HasValue)
                {
                    continue;
                }

                var mapKey = (TKey)parseKeyFn(keyValue);

                if (tryToParseItemsAsDictionaries)
                {
                    Serializer.EatWhitespace(value, ref elementStartIndex);
                    if (elementStartIndex < valueLength && value.GetChar(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.GetChar(elementStartIndex) == JsWriter.ListStartChar)
                    {
                        to[mapKey] = (TValue)DeserializeList <List <object>, TSerializer> .ParseStringSegment(elementValue);
                    }
                    else
                    {
                        to[mapKey] = (TValue)(tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength
                                        ? DeserializeType <TSerializer> .ParsePrimitive(elementValue.Value, value.GetChar(elementStartIndex))
                                        : parseValueFn(elementValue));
                    }
                }
                else
                {
                    if (tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength)
                    {
                        Serializer.EatWhitespace(value, ref elementStartIndex);
                        to[mapKey] = (TValue)DeserializeType <TSerializer> .ParsePrimitive(elementValue.Value, value.GetChar(elementStartIndex));
                    }
                    else
                    {
                        to[mapKey] = (TValue)parseValueFn(elementValue);
                    }
                }

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(to);
        }
        public static Queue <int> ParseIntQueue(StringSegment value)
        {
            var parse = (IEnumerable <int>) DeserializeList <List <int>, TSerializer> .ParseStringSegment(value);

            return(new Queue <int>(parse));
        }
        public static Stack <string> ParseStringStack(StringSegment value)
        {
            var parse = (IEnumerable <string>) DeserializeList <List <string>, TSerializer> .ParseStringSegment(value);

            return(new Stack <string>(parse));
        }
Example #8
0
        public static IDictionary <TKey, TValue> ParseDictionary <TKey, TValue>(
            ReadOnlySpan <char> value, Type createMapType,
            ParseStringSpanDelegate parseKeyFn, ParseStringSpanDelegate parseValueFn)
        {
            if (value.IsEmpty)
            {
                return(null);
            }

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

            var objDeserializer = Json.JsonTypeSerializer.Instance.ObjectDeserializer;

            if (to is Dictionary <string, object> && objDeserializer != null && typeof(TSerializer) == typeof(Json.JsonTypeSerializer))
            {
                return((IDictionary <TKey, TValue>)objDeserializer(value));
            }

            var config = JsConfig.GetConfig();

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

            var index = VerifyAndGetStartIndex(value, createMapType);

            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.IsNullOrEmpty())
                {
                    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> .ParseStringSpan(elementValue);
                    }
                    else
                    {
                        to[mapKey] = (TValue)(tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength
                            ? DeserializeType <TSerializer> .ParsePrimitive(elementValue.Value(), value[elementStartIndex])
                            : parseValueFn(elementValue));
                    }
                }
                else
                {
                    if (tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength)
                    {
                        Serializer.EatWhitespace(value, ref elementStartIndex);
                        to[mapKey] = (TValue)DeserializeType <TSerializer> .ParsePrimitive(elementValue.Value(), value[elementStartIndex]);
                    }
                    else
                    {
                        to[mapKey] = (TValue)parseValueFn(elementValue);
                    }
                }

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(to);
        }
Example #9
0
        public static Queue <string> ParseStringQueue(string value)
        {
            var parse = (IEnumerable <string>) DeserializeList <List <string>, TSerializer> .Parse(value);

            return(new Queue <string>(parse));
        }
Example #10
0
        public static Stack <int> ParseIntStack(string value)
        {
            var parse = (IEnumerable <int>) DeserializeList <List <int>, TSerializer> .Parse(value);

            return(new Stack <int>(parse));
        }
        public static Queue <int> ParseIntQueue(ReadOnlySpan <char> value)
        {
            var parse = (IEnumerable <int>) DeserializeList <List <int>, TSerializer> .ParseStringSpan(value);

            return(new Queue <int>(parse));
        }
        public static Stack <string> ParseStringStack(ReadOnlySpan <char> value)
        {
            var parse = (IEnumerable <string>) DeserializeList <List <string>, TSerializer> .ParseStringSpan(value);

            return(new Stack <string>(parse));
        }