Пример #1
0
 public object Write(object value = null, JsonConfig config = null)
 {
     if (config is null)
     {
         config = JsonConfig.Default;
     }
     WriteParameters[0] = JsonApi.ChangeType(value, AfterType, config);
     return(JsonApi.ChangeType(WriteMethod.Invoke(Instance, WriteParameters), WriteMethod.ReturnType, config));
 }
Пример #2
0
 public object Read(object value = null, JsonConfig config = null)
 {
     if (config is null)
     {
         config = JsonConfig.Default;
     }
     ReadParameters[0] = JsonApi.ChangeType(value, BeforeType, config);
     return(JsonApi.ChangeType(ReadMethod.Invoke(Instance, ReadParameters), ReadMethod.ReturnType, config));
 }
Пример #3
0
        public object BuildValue(Type type = null, object instance = null)
        {
            object returnValue = null;

            Reader.ReadValue(
                (value, _) => returnValue = JsonApi.ChangeType(value, type, Config),
                () => returnValue         = BuildArray(type, instance),
                () => returnValue         = BuildObject(type, instance)
                );
            return(returnValue);
        }
Пример #4
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));
 }
Пример #5
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);
            }
            }
        }