コード例 #1
0
 object ConverterRead(Type type, object instance, JsonField field)
 {
     if (type is null)
     {
         throw new ArgumentNullException(nameof(type));
     }
     if (field?.HasConverter ?? false)
     {
         Type readType;
         if (field.ConverterReadType == type.BaseType || JsonApi.GetElementType(field.ConverterReadType) == JsonApi.GetElementType(type).BaseType)                    //todo 让多个转换器之间也支持
         {
             readType = type;
         }
         else
         {
             readType = field.ConverterReadType;
         }
         return(JsonApi.ChangeType(field.ConverterRead(BuildValue(readType, instance), Config), type, Config));
     }
     return(BuildValue(type, instance));
 }
コード例 #2
0
 public JsonSerializerStack(object instance, JsonField field)
 {
     Instance = instance;
     Field    = field;
 }
コード例 #3
0
        public static bool CanSerializeMember(MemberInfo memberInfo, out FieldInfo fieldInfo, out PropertyInfo propertyInfo, out JsonField field)
        {
            if (memberInfo is null)
            {
                throw new ArgumentNullException(nameof(memberInfo));
            }
            switch (memberInfo.MemberType)
            {
            case MemberTypes.Field: {
                fieldInfo    = (FieldInfo)memberInfo;
                propertyInfo = null;
                if (GetCustomAttribute <JsonIgnoreField> (memberInfo) != null)
                {
                    field = null;
                    return(false);
                }
                field = GetCustomAttribute <JsonField> (memberInfo);
                if (!fieldInfo.IsPublic && field is null)
                {
                    return(false);
                }
                return(true);
            }

            case MemberTypes.Property: {
                propertyInfo = (PropertyInfo)memberInfo;
                fieldInfo    = null;
                if (!propertyInfo.CanRead || !propertyInfo.CanWrite || GetCustomAttribute <JsonIgnoreField> (memberInfo) != null)
                {
                    field = null;
                    return(false);
                }
                field = GetCustomAttribute <JsonField> (memberInfo);
                return(true);
            }

            default:
                propertyInfo = null;
                fieldInfo    = null;
                field        = null;
                return(false);
            }
        }
コード例 #4
0
        public object BuildObject(Type type, object instance = null)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (!JsonApi.TryGetObjectType(type, out JsonObjectType objectType))
            {
                throw new JsonNotSupportException(type);
            }
            if (instance?.GetType() != type)
            {
                instance = JsonApi.CreateInstance(type);
            }
            switch (objectType)
            {
            default:
                throw new JsonNotSupportException(objectType);

            case JsonObjectType.Class: {
                MemberInfo   memberInfo   = null;
                FieldInfo    fieldInfo    = null;
                PropertyInfo propertyInfo = null;
                JsonField    field        = null;
                Reader.ReadObject(name => {
                        foreach (MemberInfo current in JsonApi.GetMembers(type))
                        {
                            if (JsonApi.CanSerializeMember(current, out fieldInfo, out propertyInfo, out field))
                            {
                                if (JsonApi.Equals(name, field?.Name ?? current.Name, Config))
                                {
                                    memberInfo = current;
                                    return(true);
                                }
                            }
                        }
                        return(false);
                    }, () => {
                        object value;
                        switch (memberInfo.MemberType)
                        {
                        default:
                            throw new JsonNotSupportException(memberInfo.MemberType);

                        case MemberTypes.Field:
                            value = ConverterRead(fieldInfo.FieldType, fieldInfo.GetValue(instance), field);
                            break;

                        case MemberTypes.Property:
                            value = ConverterRead(propertyInfo.PropertyType, propertyInfo.GetValue(instance, null), field);
                            break;
                        }
                        if (!JsonApi.CanSerializeValue(value, Config))
                        {
                            return;
                        }
                        switch (memberInfo.MemberType)
                        {
                        default:
                            throw new JsonNotSupportException(memberInfo.MemberType);

                        case MemberTypes.Field:
                            fieldInfo.SetValue(instance, value);
                            break;

                        case MemberTypes.Property:
                            propertyInfo.SetValue(instance, value, null);
                            break;
                        }
                    });
                return(instance);
            }

            case JsonObjectType.DataSet: {
                DataSet dataSet   = (DataSet)instance;
                string  tableName = null;
                Reader.ReadObject(name => {
                        tableName = name;
                        return(true);
                    }, () => {
                        DataTable dataTable = dataSet.Tables[tableName];
                        bool hasTable       = dataTable != null;
                        dataTable           = BuildArray <DataTable> (dataTable);
                        if (!hasTable)
                        {
                            dataSet.Tables.Add(dataTable);
                        }
                        dataTable.TableName = JsonApi.Naming(tableName, Config.NamingType);
                    });
                return(instance);
            }

            case JsonObjectType.GenericDictionary:
            case JsonObjectType.GenericSortedDictionary:
            case JsonObjectType.GenericSortedList: {
                Type        keyType    = type.GetGenericArguments()[0];
                Type        valueType  = type.GetGenericArguments()[1];
                IDictionary dictionary = (IDictionary)instance;
                dictionary.Clear();
                object key = null;
                Reader.ReadObject(name => {
                        key = JsonApi.ChangeType(JsonApi.Naming(name, Config.NamingType), keyType, Config);
                        return(true);
                    }, () => {
                        dictionary[key] = BuildValue(valueType);
                    });
                return(instance);
            }

            case JsonObjectType.GenericKeyValuePair: {
                Type keyType   = type.GetGenericArguments()[0];
                Type valueType = type.GetGenericArguments()[1];
                Reader.ReadObject(name => {
                        type.GetRuntimeField("key").SetValue(instance, JsonApi.ChangeType(JsonApi.Naming(name, Config.NamingType), keyType, Config));
                        return(true);
                    }, () => {
                        FieldInfo fieldInfo = type.GetRuntimeField("value");
                        fieldInfo.SetValue(instance, BuildValue(valueType, fieldInfo.GetValue(instance)));
                    });
                return(instance);
            }
            }
        }