Exemplo n.º 1
0
        // ----------------------------------------------------------------------------------------
        #endregion

        #region AbstractObjectStructure properties
        // ----------------------------------------------------------------------------------------
        // AbstractObjectStructure properties
        // ----------------------------------------------------------------------------------------

        // ----------------------------------------------------------------------------------------
        #endregion

        #region AbstractObjectStructure methods
        // ----------------------------------------------------------------------------------------
        // AbstractObjectStructure methods
        // ----------------------------------------------------------------------------------------

        /// <summary>
        /// Deserializes an object with a special type meta tag.
        /// </summary>
        /// <param name="json">The json.</param>
        /// <param name="currentReadIndex">Index of the current read.</param>
        /// <param name="context">The context.</param>
        /// <param name="expectedSepIndex">Expected index of the sep.</param>
        /// <returns></returns>
        protected object DeserializeSpecialType(string json, ref int currentReadIndex, SerializationContext context, int expectedSepIndex)
        {
            // special type meta tag
            StructureString readTypeTagSerailaizer = new StructureString(Structure.TypeMetaTagKey, false);

            currentReadIndex = expectedSepIndex + 1;
            string typeName = (string)readTypeTagSerailaizer.Deserialize(json, ref currentReadIndex, context);

            if (typeNameSerializerCache == null)
            {
                typeNameSerializerCache = new ConcurrentDictionary <string, IJsonTypeStructure>();
            }

            IJsonTypeStructure currentObjectStructure;

            if (!typeNameSerializerCache.TryGetValue(typeName, out currentObjectStructure))
            {
                Type objectTargetType;
                if (!TypeService.TryGetTypeByName(typeName, out objectTargetType))
                {
                    throw new InvalidCastException(string.Format("Couldn't find Type: \"{0}\"", typeName));
                }
                currentObjectStructure = Structure.DetermineStructure(objectTargetType, this.key, context, true);

                typeNameSerializerCache.TryAdd(typeName, currentObjectStructure);
            }
            return(currentObjectStructure.Deserialize(json, ref currentReadIndex, context));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines the type structure.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="key">The key.</param>
        /// <param name="context">The context.</param>
        /// <param name="isArrayItem">if set to <c>true</c> [is array item].</param>
        /// <returns></returns>
        public static IJsonTypeStructure DetermineStructure(Type type, string key, SerializationContext context, bool isArrayItem)
        {
            IJsonTypeStructure result;

            if (type.Equals(typeof(string)))
            {
                result = new StructureString(key, isArrayItem);
            }
            else if (type.IsValueType)
            {
                if (type.Equals(typeof(int)))
                {
                    result = new StructureInt(key, isArrayItem);
                }
                else if (type.IsEnum)
                {
                    result = new StructureEnum(key, type, isArrayItem);
                }
                else if (type.Equals(typeof(bool)))
                {
                    result = new StructureBool(key, isArrayItem);
                }
                else if (type.Equals(typeof(TimeSpan)))
                {
                    result = new StructureTimeSpan(key, isArrayItem);
                }
                else if (type.Equals(typeof(DateTime)))
                {
                    result = new StructureDateTime(key, isArrayItem);
                }
                else if (type.Equals(typeof(char)))
                {
                    result = new StructureChar(key, isArrayItem);
                }
                else if (type.Equals(typeof(Guid)))
                {
                    result = new StructureGuid(key, isArrayItem);
                }
                else
                {
                    result = new StructureValueType(key, type, isArrayItem);
                }
            }
            else if (type.IsArray ||
                     type.GetInterface("IEnumerable") != null)
            {
                result = new StructureArray(key, type, context, isArrayItem);
            }
            else
            {
                result = new StructureComplexObject(type, key, context, isArrayItem);
            }

            return(result);
        }