Beispiel #1
0
        public void ReadObject(JsonFunc <string, bool> key, JsonAction readValue)
        {
            if (key is null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (readValue is null)
            {
                throw new ArgumentNullException(nameof(readValue));
            }
            Initialize(false);
            switch (Stacks.Peek().ObjectType)
            {
            default:
                throw new JsonNotSupportException(Stacks.Peek().ObjectType);

            case JsonObjectType.Class:
                JsonApi.ForEachSerializableMembers(Stacks.Peek().Type, (memberInfo, fieldInfo, propertyInfo, field) => {
                    bool isReaded   = false;
                    object instance = null;
                    Type fieldType  = null;
                    object Read()
                    {
                        if (isReaded)
                        {
                            return(instance);
                        }
                        isReaded = true;
                        switch (memberInfo.MemberType)
                        {
                        default:
                            throw new JsonNotSupportException(memberInfo.MemberType);

                        case MemberTypes.Field:
                            fieldType = fieldInfo.FieldType;
                            return(instance = fieldInfo.GetValue(Stacks.Peek().Instance));

                        case MemberTypes.Property:
                            fieldType = propertyInfo.PropertyType;
                            return(instance = propertyInfo.GetValue(Stacks.Peek().Instance, null));
                        }
                    }
                    object ConverterWrite(object value)
                    {
                        if (field?.HasConverter ?? false)
                        {
                            if (field.ConverterWriteType != fieldType.BaseType &&
                                JsonApi.GetElementType(field.ConverterWriteType) != JsonApi.GetElementType(fieldType).BaseType
                                )
                            {
                                fieldType = field.ConverterWriteType;
                            }
                            return(field.ConverterWrite(value, Config));
                        }
                        return(value);
                    }
                    if (!JsonApi.CanSerializeValue(Read(), Config))
                    {
                        return;
                    }
                    if (key(field?.Name ?? memberInfo.Name))
                    {
                        Stacks.Push(new JsonSerializerStack(ConverterWrite(Read()), field));
                        Stacks.Peek().Type = fieldType;
                        readValue();
                        Stacks.Pop();
                    }
                });
                break;

            case JsonObjectType.DataRow: {
                DataRow dataRow = (DataRow)Stacks.Peek().Instance;
                for (int i = 0; i < dataRow.Table.Columns.Count; i++)
                {
                    if (key(dataRow.Table.Columns[i].ColumnName))
                    {
                        Stacks.Push(new JsonSerializerStack(dataRow[i]));
                        readValue();
                        Stacks.Pop();
                    }
                }
                break;
            }

            case JsonObjectType.DataSet: {
                DataSet dataSet = (DataSet)Stacks.Peek().Instance;
                foreach (DataTable dataTable in dataSet.Tables)
                {
                    if (key(dataTable.TableName))
                    {
                        Stacks.Push(new JsonSerializerStack(dataTable));
                        readValue();
                        Stacks.Pop();
                    }
                }
                break;
            }

            case JsonObjectType.GenericDictionary:
            case JsonObjectType.GenericSortedDictionary:
            case JsonObjectType.GenericSortedList: {
                IDictionary dictionary = (IDictionary)Stacks.Peek().Instance;
                foreach (DictionaryEntry entry in dictionary)
                {
                    if (key(Convert.ToString(entry.Key)))
                    {
                        Stacks.Push(new JsonSerializerStack(entry.Value));
                        readValue();
                        Stacks.Pop();
                    }
                }
                break;
            }

            case JsonObjectType.GenericKeyValuePair: {
                if (key(Convert.ToString(Stacks.Peek().Type.GetProperty("Key").GetValue(Stacks.Peek().Instance, null))))
                {
                    Stacks.Push(new JsonSerializerStack(Stacks.Peek().Type.GetProperty("Value").GetValue(Stacks.Peek().Instance, null)));
                    readValue();
                    Stacks.Pop();
                }
                break;
            }
            }
        }
Beispiel #2
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);
            }
            }
        }