Example #1
0
        /// <summary>
        /// Gets the name specified for use in Json serialization.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetClassName(Type value)
        {
            if (value == null)
            {
                return(null);
            }
            var attribs = value.GetCustomAttributes(typeof(JsonClassAttribute), false);

            if (attribs.Length == 0)
            {
                return(null);
            }
            JsonClassAttribute attribute = attribs[0] as JsonClassAttribute;

            if (attribute == null)
            {
                return(null);
            }
            return(attribute.Name);
        }
Example #2
0
        public object ReadObject(Type objectType)
        {
            if (objectType != null && !string.IsNullOrEmpty(JsonClassAttribute.GetClassName(objectType)))
            {
                string typetoConvert = GetFieldValue(JsonClassAttribute.GetClassName(objectType));
                if (!string.IsNullOrEmpty(typetoConvert))
                {
                    Type nested;
                    if (this.typeCreator != null)
                    {
                        nested = this.typeCreator(typetoConvert);
                    }
                    else
                    {
                        nested = Type.GetType(typetoConvert);
                    }
                    if (objectType != null)
                    {
                        objectType = nested;
                    }
                }
            }
            if (this.Source[this.index] != JsonReader.OperatorObjectStart)
            {
                throw new JsonDeserializationException(JsonReader.ErrorExpectedObject, this.index);
            }

            Type genericDictionaryType = null;
            Dictionary <string, MemberInfo> memberMap = null;
            Object result;

            if (objectType != null)
            {
                result = this.Settings.Coercion.InstantiateObject(objectType, out memberMap);

                if (memberMap == null)
                {
                    // this allows specific IDictionary<string, T> to deserialize T
                    Type genericDictionary = objectType.GetInterface(JsonReader.TypeGenericIDictionary);
                    if (genericDictionary != null)
                    {
                        Type[] genericArgs = genericDictionary.GetGenericArguments();
                        if (genericArgs.Length == 2)
                        {
                            if (genericArgs[0] != typeof(String))
                            {
                                throw new JsonDeserializationException(
                                          String.Format(JsonReader.ErrorGenericIDictionaryKeys, objectType),
                                          this.index);
                            }

                            if (genericArgs[1] != typeof(Object))
                            {
                                genericDictionaryType = genericArgs[1];
                            }
                        }
                    }
                }
            }
            else
            {
                result = new Dictionary <String, Object>();
            }

            JsonToken token;

            do
            {
                Type       memberType;
                MemberInfo memberInfo;

                // consume opening brace or delim
                this.index++;
                if (this.index >= this.SourceLength)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index);
                }

                // get next token
                token = this.Tokenize(this.Settings.AllowUnquotedObjectKeys);
                if (token == JsonToken.ObjectEnd)
                {
                    break;
                }

                if (token != JsonToken.String && token != JsonToken.UnquotedName)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorExpectedPropertyName, this.index);
                }

                // parse object member value
                string memberName = (token == JsonToken.String) ?
                                    (String)this.ReadString(null) :
                                    this.ReadUnquotedKey();

                if (genericDictionaryType == null && memberMap != null)
                {
                    // determine the type of the property/field
                    memberType = TypeCoercionUtility.GetMemberInfo(memberMap, memberName, out memberInfo);
                }
                else
                {
                    memberType = genericDictionaryType;
                    memberInfo = null;
                }

                // get next token
                token = this.Tokenize();
                if (token != JsonToken.NameDelim)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorExpectedPropertyNameDelim, this.index);
                }

                // consume delim
                this.index++;
                if (this.index >= this.SourceLength)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index);
                }

                // parse object member value
                object value = this.Read(memberType, false);

                if (result is IDictionary)
                {
                    if (objectType == null && this.Settings.IsTypeHintName(memberName))
                    {
                        result = this.Settings.Coercion.ProcessTypeHint((IDictionary)result, value as string, out objectType, out memberMap);
                    }
                    else
                    {
                        ((IDictionary)result)[memberName] = value;
                    }
                }
                else if (objectType.GetInterface(JsonReader.TypeGenericIDictionary) != null)
                {
                    throw new JsonDeserializationException(
                              String.Format(JsonReader.ErrorGenericIDictionary, objectType),
                              this.index);
                }
                else
                {
                    this.Settings.Coercion.SetMemberValue(result, memberType, memberInfo, value);
                }

                // get next token
                token = this.Tokenize();
            } while (token == JsonToken.ValueDelim);

            if (token != JsonToken.ObjectEnd)
            {
                throw new JsonDeserializationException(JsonReader.ErrorUnterminatedObject, this.index);
            }

            // consume closing brace
            this.index++;

            return(result);
        }