Exemple #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));
 }
Exemple #2
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;
            }
            }
        }