public static JsonObject ParseJsonObject(string value)
        {
            var index = VerifyAndGetStartIndex(value, typeof(JsonObject));

            var result = new JsonObject();

            if (value == JsWriter.EmptyMap)
            {
                return(result);
            }

            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   = keyValue;
                var mapValue = elementValue;

                result[mapKey] = mapValue;

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(result);
        }
Example #2
0
        public static Dictionary <string, string> ParseStringDictionary(string value)
        {
            var index = VerifyAndGetStartIndex(value, typeof(Dictionary <string, string>));

            var result = new Dictionary <string, string>();

            if (value == JsWriter.EmptyMap)
            {
                return(result);
            }

            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.ParseString(keyValue);
                var mapValue = Serializer.ParseString(elementValue);

                result[mapKey] = mapValue;

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(result);
        }
        public static object Parse(Type tupleType, string value)
        {
            var index = 0;

            Serializer.EatMapStartChar(value, ref index);
            if (JsonTypeSerializer.IsEmptyMap(value, index))
            {
                return(Activator.CreateInstance(tupleType));
            }

            var genericArgs = tupleType.GetGenericArguments();
            var argValues   = new object[genericArgs.Length];
            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);
                if (keyValue == null)
                {
                    continue;
                }

                var keyIndex = int.Parse(keyValue.Substring(4)) - 1;
                argValues[keyIndex] = Serializer.GetParseFn(genericArgs[keyIndex]).Invoke(elementValue);

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(tupleType.GetConstructors().First(x => x.GetParameters().Length == genericArgs.Length).Invoke(argValues));
        }
Example #4
0
        private static object StringToType(Type type, string strType,
                                           EmptyCtorDelegate ctorFn,
                                           IDictionary <string, SetPropertyDelegate> setterMap,
                                           IDictionary <string, ParseStringDelegate> parseStringFnMap)
        {
            var index = 0;

            if (!Serializer.EatMapStartChar(strType, ref index))
            {
                throw new SerializationException(string.Format(
                                                     "Type definitions should start with a '{0}', expecting serialized type '{1}', got string starting with: {2}",
                                                     JsWriter.MapStartChar, type.Name, strType.Substring(0, strType.Length < 50 ? strType.Length : 50)));
            }


            var    instance = ctorFn();
            string propertyName;
            ParseStringDelegate parseStringFn;
            SetPropertyDelegate setterFn;

            if (strType == JsWriter.EmptyMap)
            {
                return(instance);
            }
            var strTypeLength = strType.Length;

            while (index < strTypeLength)
            {
                propertyName = Serializer.EatMapKey(strType, ref index);

                Serializer.EatMapKeySeperator(strType, ref index);

                var propertyValueString = Serializer.EatValue(strType, ref index);

                parseStringFnMap.TryGetValue(propertyName, out parseStringFn);

                if (parseStringFn != null)
                {
                    var propertyValue = parseStringFn(propertyValueString);

                    setterMap.TryGetValue(propertyName, out setterFn);

                    if (setterFn != null)
                    {
                        setterFn(instance, propertyValue);
                    }
                }

                Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
            }

            return(instance);
        }
Example #5
0
    /// <summary>
    /// Parses the json object.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <returns>JsonObject.</returns>
    public static JsonObject ParseJsonObject(ReadOnlySpan <char> value)
    {
        if (value.Length == 0)
        {
            return(null);
        }

        var index = VerifyAndGetStartIndex(value, typeof(JsonObject));

        var result = new JsonObject();

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

        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);
            if (keyValue.IsEmpty)
            {
                continue;
            }

            var mapKey   = keyValue.ToString();
            var mapValue = elementValue.Value();

            result[mapKey] = mapValue;

            Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
        }

        return(result);
    }
Example #6
0
        public static IDynamicMetaObjectProvider ParseDynamic(StringSegment 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).Value;

                if (JsonUtils.IsJsObject(elementValue))
                {
                    container[mapKey] = ParseDynamic(elementValue);
                }
                else if (JsonUtils.IsJsArray(elementValue))
                {
                    container[mapKey] = DeserializeList <List <object>, TSerializer> .ParseStringSegment(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 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));
        }
Example #8
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));
        }
Example #9
0
        public static Hashtable ParseHashtable(StringSegment value)
        {
            if (!value.HasValue)
            {
                return(null);
            }

            var index = VerifyAndGetStartIndex(value, typeof(Hashtable));

            var result = new Hashtable();

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

            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);
                if (!keyValue.HasValue)
                {
                    continue;
                }

                var mapKey   = keyValue.Value;
                var mapValue = elementValue.Value;

                result[mapKey] = mapValue;

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            return(result);
        }
Example #10
0
        public static object ParseTuple(Type tupleType, StringSegment value)
        {
            var index = 0;

            Serializer.EatMapStartChar(value, ref index);
            if (JsonTypeSerializer.IsEmptyMap(value, index))
            {
                //return tupleType.CreateInstance();
                return(ActivatorUtils.FastCreateInstance(tupleType));
            }

            var genericArgs = tupleType.GetGenericArguments();
            var argValues   = new object[genericArgs.Length];
            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);
                if (!keyValue.HasValue)
                {
                    continue;
                }

                var keyIndex = keyValue.Substring("Item".Length).ToInt() - 1;
                var parseFn  = Serializer.GetParseStringSegmentFn(genericArgs[keyIndex]);
                argValues[keyIndex] = parseFn(elementValue);

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            var ctor = tupleType.GetConstructors()
                       .First(x => x.GetParameters().Length == genericArgs.Length);

            return(ctor.Invoke(argValues));
        }
Example #11
0
    /// <summary>
    /// Parses the tuple.
    /// </summary>
    /// <param name="tupleType">Type of the tuple.</param>
    /// <param name="value">The value.</param>
    /// <returns>System.Object.</returns>
    public static object ParseTuple(Type tupleType, ReadOnlySpan <char> value)
    {
        var index = 0;

        Serializer.EatMapStartChar(value, ref index);
        if (JsonTypeSerializer.IsEmptyMap(value, index))
        {
            return(tupleType.CreateInstance());
        }

        var genericArgs = tupleType.GetGenericArguments();
        var argValues   = new object[genericArgs.Length];
        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);
            if (keyValue.IsEmpty)
            {
                continue;
            }

            var keyIndex = keyValue.Slice("Item".Length).ParseInt32() - 1;
            var parseFn  = Serializer.GetParseStringSpanFn(genericArgs[keyIndex]);
            argValues[keyIndex] = parseFn(elementValue);

            Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
        }

        var ctor = tupleType.GetConstructors()
                   .First(x => x.GetParameters().Length == genericArgs.Length);

        return(ctor.Invoke(argValues));
    }
Example #12
0
        private static object StringToType(Type type, string strType,
                                           EmptyCtorDelegate ctorFn,
                                           IDictionary <string, SetPropertyDelegate> setterMap,
                                           IDictionary <string, ParseStringDelegate> parseStringFnMap)
        {
            var index = 0;

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

            if (!Serializer.EatMapStartChar(strType, ref index))
            {
                throw new SerializationException(string.Format(
                                                     "Type definitions should start with a '{0}', expecting serialized type '{1}', got string starting with: {2}",
                                                     JsWriter.MapStartChar, type.Name, strType.Substring(0, strType.Length < 50 ? strType.Length : 50)));
            }

            if (strType == JsWriter.EmptyMap)
            {
                return(ctorFn());
            }

            object instance = null;
            string propertyName;
            ParseStringDelegate parseStringFn;
            SetPropertyDelegate setterFn;

            var strTypeLength = strType.Length;

            while (index < strTypeLength)
            {
                propertyName = Serializer.EatMapKey(strType, ref index);

                Serializer.EatMapKeySeperator(strType, ref index);

                var propertyValueString = Serializer.EatValue(strType, ref index);

                if (propertyName == JsWriter.TypeAttr)
                {
                    var typeName = Serializer.ParseString(propertyValueString);
                    instance = ReflectionExtensions.CreateInstance(typeName);
                    if (instance == null)
                    {
                        Tracer.Instance.WriteWarning("Could not find type: " + propertyValueString);
                    }
                    else
                    {
                        //If __type info doesn't match, ignore it.
                        if (!type.IsAssignableFrom(instance.GetType()))
                        {
                            instance = null;
                        }
                    }

                    Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    continue;
                }

                if (instance == null)
                {
                    instance = ctorFn();
                }

                var propType = ExtractType(propertyValueString);
                if (propType != null)
                {
                    try
                    {
                        var parseFn       = Serializer.GetParseFn(propType);
                        var propertyValue = parseFn(propertyValueString);

                        setterMap.TryGetValue(propertyName, out setterFn);

                        if (setterFn != null)
                        {
                            setterFn(instance, propertyValue);
                        }

                        Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                        continue;
                    }
                    catch (Exception)
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueString);
                    }
                }

                parseStringFnMap.TryGetValue(propertyName, out parseStringFn);

                if (parseStringFn != null)
                {
                    try
                    {
                        var propertyValue = parseStringFn(propertyValueString);

                        setterMap.TryGetValue(propertyName, out setterFn);

                        if (setterFn != null)
                        {
                            setterFn(instance, propertyValue);
                        }
                    }
                    catch (Exception)
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueString);
                    }
                }

                Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
            }

            return(instance);
        }