Пример #1
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 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);
        }
        public static List <string> ParseStringList(StringSegment value)
        {
            if (!(value = StripList(value)).HasValue)
            {
                return(null);
            }
            if (value.Length == 0)
            {
                return(new List <string>());
            }

            var to          = new List <string>();
            var valueLength = value.Length;

            var i = 0;

            while (i < valueLength)
            {
                var elementValue = Serializer.EatValue(value, ref i);
                var listValue    = Serializer.UnescapeString(elementValue);
                to.Add(listValue.Value);
                if (Serializer.EatItemSeperatorOrMapEndChar(value, ref i) && i == valueLength)
                {
                    // If we ate a separator and we are at the end of the value,
                    // it means the last element is empty => add default
                    to.Add(null);
                }
            }

            return(to);
        }
Пример #4
0
        public static Type ExtractType(string strType)
        {
            var typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType != null &&
                strType.Length > typeAttrInObject.Length &&
                strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject)
            {
                var propIndex = typeAttrInObject.Length;
                var typeName  = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex));

                var type = JsConfig.TypeFinder.Invoke(typeName);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                    return(null);
                }

                if (type.IsInterface || type.IsAbstract)
                {
#if !SILVERLIGHT && !MONOTOUCH && !NETCF
                    return(DynamicProxy.GetInstanceFor(type).GetType());
#else
                    throw new NotImplementedException("Deserialization from Interface and Abstract type does not supported");
#endif
                }


                return(type);
            }
            return(null);
        }
Пример #5
0
        public static Type ExtractType(ITypeSerializer Serializer, string strType)
        {
            if (strType == null || strType.Length <= 1)
            {
                return((Type)null);
            }
            if (((IEnumerable <char>)JsonUtils.WhiteSpaceChars).Contains <char>(strType[1]))
            {
                int startIndex = strType.IndexOf('"');
                if (startIndex >= 0)
                {
                    strType = "{" + strType.Substring(startIndex);
                }
            }
            string typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType.Length <= typeAttrInObject.Length || !(strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject))
            {
                return((Type)null);
            }
            int    length = typeAttrInObject.Length;
            string str    = Serializer.EatValue(strType, ref length);
            Type   type   = JsConfig.TypeFinder(str);

            if (!(type == (Type)null))
            {
                return(type);
            }
            Tracer.Instance.WriteWarning("Could not find type: " + str);
            return(type);
        }
Пример #6
0
        public static Type ExtractType(string strType)
        {
            if (strType != null &&
                strType.Length > TypeAttrInObject.Length &&
                strType.Substring(0, TypeAttrInObject.Length) == TypeAttrInObject)
            {
                var propIndex = TypeAttrInObject.Length;
                var typeName  = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex));
                var type      = AssemblyUtils.FindType(typeName);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                    return(null);
                }

                if (type.IsInterface || type.IsAbstract)
                {
                    return(DynamicProxy.GetInstanceFor(type).GetType());
                }

                return(type);
            }
            return(null);
        }
        public static Type ExtractType(string strType)
        {
            var typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType != null &&
                strType.Length > typeAttrInObject.Length &&
                strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject)
            {
                var propIndex = typeAttrInObject.Length;
                var typeName  = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex));

                var type = JsConfig.TypeFinder.Invoke(typeName);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                    return(null);
                }

#if !SILVERLIGHT && !MONOTOUCH
                if (type.IsInterface || type.IsAbstract)
                {
                    return(DynamicProxy.GetInstanceFor(type).GetType());
                }
#endif

                return(type);
            }
            return(null);
        }
Пример #8
0
        public static List <string> ParseStringList(string value)
        {
            if ((value = StripList(value)) == null)
            {
                return(null);
            }
            if (value == string.Empty)
            {
                return(new List <string>());
            }

            var to          = new List <string>();
            var valueLength = value.Length;

            var i = 0;

            while (i < valueLength)
            {
                var elementValue = Serializer.EatValue(value, ref i);
                var listValue    = Serializer.ParseString(elementValue);
                to.Add(listValue);
                Serializer.EatItemSeperatorOrMapEndChar(value, ref i);
            }

            return(to);
        }
Пример #9
0
        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));
        }
Пример #10
0
        public static Type ExtractType(string strType)
        {
            if (strType != null &&
                strType.Length > TypeAttrInObject.Length &&
                strType.Substring(0, TypeAttrInObject.Length) == TypeAttrInObject)
            {
                var propIndex = TypeAttrInObject.Length;
                var typeName  = Serializer.EatValue(strType, ref propIndex);
                typeName = Serializer.ParseString(typeName);
                var type = AssemblyUtils.FindType(typeName);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                }

                return(type);
            }
            return(null);
        }
Пример #11
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);
        }
Пример #12
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);
    }
Пример #13
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);
        }
Пример #14
0
        public static Type ExtractType(string strType)
        {
            var typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType == null || strType.Length <= 1)
            {
                return(null);
            }

            var hasWhitespace = JsonUtils.WhiteSpaceChars.Contains(strType[1]);

            if (hasWhitespace)
            {
                var pos = strType.IndexOf('"');
                if (pos >= 0)
                {
                    strType = "{" + strType.Substring(pos);
                }
            }

            if (strType.Length > typeAttrInObject.Length &&
                strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject)
            {
                var propIndex = typeAttrInObject.Length;
                var typeName  = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex));

                var type = JsConfig.TypeFinder.Invoke(typeName);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                    return(null);
                }

#if !SILVERLIGHT && !MONOTOUCH
                if (type.IsInterface || type.IsAbstract)
                {
                    return(DynamicProxy.GetInstanceFor(type).GetType());
                }
#endif

                return(type);
            }
            return(null);
        }
Пример #15
0
        public static List <string> ParseFields(string line)
        {
            var to = new List <string>();

            if (string.IsNullOrEmpty(line))
            {
                return(to);
            }

            var i   = -1;
            var len = line.Length;

            while (++i <= len)
            {
                var value = Serializer.EatValue(line, ref i);
                to.Add(value.FromCsvField());
            }

            return(to);
        }
Пример #16
0
        // TODO: optimize ExtractType
        public static Type ExtractType(StringSegment strType)
        {
            if (!strType.HasValue || strType.Length <= 1)
            {
                return(null);
            }

            var hasWhitespace = Json.JsonUtils.WhiteSpaceChars.Contains(strType.GetChar(1));

            if (hasWhitespace)
            {
                var pos = strType.IndexOf('"');
                if (pos >= 0)
                {
                    strType = new StringSegment($"{{{strType.Substring(pos, strType.Length - pos)}");
                }
            }

            var typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType.Length > typeAttrInObject.Length &&
                strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject)
            {
                var propIndex = typeAttrInObject.Length;
                var typeName  = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex)).Value;

                var type = JsConfig.TypeFinder(typeName);

                JsWriter.AssertAllowedRuntimeType(type);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning($"Could not find type: {typeName}");
                    return(null);
                }

                return(PclExport.Instance.UseType(type));
            }

            return(null);
        }
        public static Type ExtractType(ITypeSerializer Serializer, string strType)
        {
            var typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType != null &&
                strType.Length > typeAttrInObject.Length &&
                strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject)
            {
                var propIndex = typeAttrInObject.Length;
                var typeName  = Serializer.EatValue(strType, ref propIndex);
                var type      = JsConfig.TypeFinder.Invoke(typeName);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                }

                return(type);
            }
            return(null);
        }
Пример #18
0
        //TODO: optimize ExtractType
        public static Type ExtractType(ReadOnlySpan <char> strType)
        {
            if (strType.IsEmpty || strType.Length <= 1)
            {
                return(null);
            }

            var hasWhitespace = Json.JsonUtils.WhiteSpaceChars.Contains(strType[1]);

            if (hasWhitespace)
            {
                var pos = strType.IndexOf('"');
                if (pos >= 0)
                {
                    strType = ("{" + strType.Substring(pos, strType.Length - pos)).AsSpan();
                }
            }

            var typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType.Length > typeAttrInObject.Length &&
                strType.Slice(0, typeAttrInObject.Length).EqualsOrdinal(typeAttrInObject))
            {
                var propIndex = typeAttrInObject.Length;
                var typeName  = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex)).ToString();

                var type = JsConfig.TypeFinder(typeName);

                JsWriter.AssertAllowedRuntimeType(type);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                    return(null);
                }

                return(ReflectionOptimizer.Instance.UseType(type));
            }
            return(null);
        }
Пример #19
0
        public static Type ExtractType(string strType)
        {
            if (strType == null || strType.Length <= 1)
            {
                return(null);
            }

            var hasWhitespace = JsonUtils.WhiteSpaceChars.Contains(strType[1]);

            if (hasWhitespace)
            {
                var pos = strType.IndexOf('"');
                if (pos >= 0)
                {
                    strType = "{" + strType.Substring(pos);
                }
            }

            var typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType.Length > typeAttrInObject.Length &&
                strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject)
            {
                var propIndex = typeAttrInObject.Length;
                var typeName  = Serializer.UnescapeSafeString(Serializer.EatValue(strType, ref propIndex));

                var type = JsConfig.TypeFinder(typeName);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                    return(null);
                }

                return(PclExport.Instance.UseType(type));
            }
            return(null);
        }
Пример #20
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);
        }
Пример #21
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));
        }
Пример #22
0
        public static Type ExtractType(ITypeSerializer Serializer, StringSegment strType)
        {
            if (!strType.HasValue || strType.Length <= 1)
            {
                return(null);
            }

            var hasWhitespace = Json.JsonUtils.WhiteSpaceChars.Contains(strType.GetChar(1));

            if (hasWhitespace)
            {
                var pos = strType.IndexOf('"');
                if (pos >= 0)
                {
                    strType = new StringSegment("{" + strType.Substring(pos));
                }
            }

            var typeAttrInObject = Serializer.TypeAttrInObject;

            if (strType.Length > typeAttrInObject.Length &&
                strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject)
            {
                var propIndex = typeAttrInObject.Length;
                var typeName  = Serializer.EatValue(strType, ref propIndex).Value;
                var type      = JsConfig.TypeFinder(typeName);

                if (type == null)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + typeName);
                }

                return(type);
            }

            return(null);
        }
Пример #23
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));
    }