static object DeserializeObject(ref Utf8JsonReader p_reader, string p_alias, Type p_tgtType) { Type type = SerializeTypeAlias.GetType(p_alias); if (p_tgtType != null && p_tgtType != type) { // 以子类型为准 if (p_tgtType.IsSubclassOf(type)) { type = p_tgtType; } else if (!type.IsSubclassOf(p_tgtType)) { throw new Exception($"{p_tgtType.Name} 与 {type.Name} 无继承关系!"); } } // 自定义序列化 if (type.GetInterface("IRpcJson") != null) { // 无参数构造方法可能为private,如实体类型 object tgt = Activator.CreateInstance(type, true); ((IRpcJson)tgt).ReadRpcJson(ref p_reader); return(tgt); } // 标准序列化 p_reader.Read(); object obj = JsonSerializer.Deserialize(ref p_reader, type); return(obj); }
static object DeserializeArray(ref Utf8JsonReader p_reader, string p_alias) { // 只支持List<T>的情况 Type type = SerializeTypeAlias.GetType(p_alias); Type itemType = type.GetGenericArguments()[0]; if (itemType == typeof(object)) { return(DeserializeObjsArray(ref p_reader)); } IList target = Activator.CreateInstance(type) as IList; while (p_reader.Read() && p_reader.TokenType != JsonTokenType.EndArray) { target.Add(Deserialize(ref p_reader, itemType)); } return(target); }