Exemplo n.º 1
0
        /// <summary> 构造函数
        /// </summary>
        private JsonMember(MemberInfo member, bool ignoreSerialized)
        {
            if (member.MemberType == MemberTypes.Property)
            {
                Member = new ObjectProperty((PropertyInfo)member);
            }
            else
            {
                Member = new ObjectProperty((FieldInfo)member);
            }

            var name = Member.Attributes.First <JsonNameAttribute>();

            JsonName = name != null?JsonBuilder.EscapeString(name.Name) : member.Name;

            JsonFormatAttribute format = Member.Attributes.First <JsonFormatAttribute>();

            if (format != null)
            {
                if (!TypesHelper.IsChild(typeof(IFormattable), Member.MemberType))
                {
                    format = null;
                }
                MustFormat     = true;
                FormatString   = format.Format;
                FormatProvider = format.Provider;
            }
            NonSerialized = ignoreSerialized;
            Type          = Member.MemberType;
            CanRead       = Member.CanRead;
            CanWrite      = Member.CanWrite;
        }
Exemplo n.º 2
0
        /// <summary>  解释 json 字符串,并填充到obj对象中,如果obj为null则新建对象
        /// </summary>
        private void FillObject(ref object obj, Type type, UnsafeJsonReader reader)
        {
            reader.CheckEnd();
            var toObject = obj as ILoadJson;

            if (toObject != null)
            {
                obj  = null;
                type = null;
            }

            if (type == null)
            {
                switch (reader.Current)
                {
                case '{':
                    type = _keyValueType.Type;
                    break;

                case '[':     // 中括号的json仅支持反序列化成IList的对象
                    type = _arrayType.Type;
                    break;

                default:
                    ThrowException("Json字符串必须以 { 或 [ 开始");
                    break;
                }
            }
            else if (TypesHelper.IsChild(typeof(ILoadJson), type))
            {
                obj = Activator.CreateInstance(type);
                FillObject(ref obj, null, reader);
                return;
            }

            var jsonType = JsonType.Get(type);


            //如果obj == null创建新对象
            if (obj == null)
            {
                if (jsonType.TypeCodes == TypeCodes.AnonymousType)
                {
                    throw new NotSupportedException("不支持匿名类型的反序列化操作");
                }
                obj = jsonType.CreateInstance();
            }


            //判断起始字符
            switch (reader.Current)
            {
            case '{':
                reader.MoveNext();
                if (jsonType.IsDictionary)
                {
                    FillDictionary(obj, jsonType, reader);
                }
                else
                {
                    FillProperty(obj, jsonType, reader);
                }
                reader.SkipChar('}', true);
                break;

            case '[':     // 中括号的json仅支持反序列化成IList的对象
                reader.MoveNext();
                if (jsonType.Type.IsArray)
                {
                    if (obj is ArrayList)
                    {
                        FillList(obj, jsonType, reader);
                    }
                    else
                    {
                        var arr = obj as Array;
                        if (arr == null)
                        {
                            ThrowException("无法处理当前对象 : 类型( " + TypesHelper.DisplayName(obj.GetType()) + " )");
                        }
                        FillArray(arr, jsonType, reader);
                    }
                }
                else if (jsonType.IsList)
                {
                    FillList(obj, jsonType, reader);
                }
                else
                {
                    ThrowException("无法处理当前对象 : 类型( " + TypesHelper.DisplayName(obj.GetType()) + " )");
                }
                reader.SkipChar(']', true);
                break;

            default:
                ThrowException("Json字符串必须以 { 或 [ 开始");
                break;
            }
            if (toObject != null)
            {
                toObject.LoadJson(JsonObject.ToJsonObject(obj));
                obj = toObject;
            }
        }