public JsonObject BuildObject(JsonObject jsonObject = null) { JsonObject returnValue = jsonObject ?? new JsonObject(); string key = null; Reader.ReadObject(name => { key = name; return(true); }, () => returnValue.Add(JsonApi.Naming(key, Config.NamingType), BuildValue(returnValue.Get(key)))); //todo 待测试复用是否正常 return(returnValue); }
public void BuildObject() { TextWriter.BeginObject(); Reader.ReadObject( name => { TextWriter.Write(JsonApi.Naming(name, Config.NamingType), JsonValueType.String); return(true); }, () => BuildValue() ); TextWriter.EndObject(); }
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); } } }