private DummyAssembly ReadObject(ref Utf8JsonReader reader, JsonSerializerOptions options) { byte[] ID = Encoding.UTF8.GetBytes("$id"); byte[] REF = Encoding.UTF8.GetBytes("$ref"); byte[] TYPE = Encoding.UTF8.GetBytes("$type"); string reference1 = string.Empty; string propertyName = string.Empty; PropertyInfo propertyInfo = null; DummyAssembly entity = null; Type entityType = null; while (reader.Read()) { if (reader.TokenType == JsonTokenType.StartObject) { if (propertyInfo.IsOptional()) { IOptional optional = (IOptional)propertyInfo.GetValue(entity); ReadOption(ref reader, optional, options); } else { DummyAssembly value = ReadObject(ref reader, options); propertyInfo.SetValue(entity, value); } } else if (reader.TokenType == JsonTokenType.EndObject) { return(entity); } else if (reader.TokenType == JsonTokenType.PropertyName) { if (reader.ValueTextEquals(REF)) { reader.Read(); string referenceN = reader.GetString(); entity = (DummyAssembly)_resolver.ResolveReference(referenceN); while (reader.TokenType != JsonTokenType.EndObject) { if (!reader.Read()) { break; } } return(entity); } else if (reader.ValueTextEquals(ID)) { reader.Read(); reference1 = reader.GetString(); } else if (reader.ValueTextEquals(TYPE)) { reader.Read(); string typeCode = reader.GetString(); entityType = _binder.GetType(typeCode); entity = (DummyAssembly)Activator.CreateInstance(entityType); _resolver.AddReference(reference1, entity); } else { propertyName = reader.GetString(); propertyInfo = entityType.GetProperty(propertyName); } } else if (reader.TokenType == JsonTokenType.Null) { propertyInfo.SetValue(entity, null); } else if (reader.TokenType == JsonTokenType.True) { propertyInfo.SetValue(entity, true); } else if (reader.TokenType == JsonTokenType.False) { propertyInfo.SetValue(entity, false); } else if (reader.TokenType == JsonTokenType.Number) { uint intValue = reader.GetUInt32(); if (propertyInfo.PropertyType.IsEnum) { propertyInfo.SetValue(entity, Enum.GetValues(propertyInfo.PropertyType).GetValue(intValue)); } else { propertyInfo.SetValue(entity, intValue); } } else if (reader.TokenType == JsonTokenType.String) { string stringValue = reader.GetString(); if (propertyInfo.PropertyType == typeof(Type)) { propertyInfo.SetValue(entity, Type.GetType(stringValue)); } else if (propertyInfo.PropertyType == typeof(Assembly)) { //AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(assembly => assembly.GetName().Name == name); propertyInfo.SetValue(entity, Assembly.Load(stringValue)); } else { propertyInfo.SetValue(entity, stringValue); } } else if (reader.TokenType == JsonTokenType.StartArray) { IList list = (IList)propertyInfo.GetValue(entity); while (reader.TokenType != JsonTokenType.EndArray) { if (!reader.Read() || reader.TokenType == JsonTokenType.EndArray) { break; } DummyAssembly item = ReadObject(ref reader, options); list.Add(item); } } } return(entity); // never gets here - JsonTokenType.EndObject returns ... }
private Entity ReadObject(ref Utf8JsonReader reader, JsonSerializerOptions options) { byte[] ID = Encoding.UTF8.GetBytes("$id"); byte[] REF = Encoding.UTF8.GetBytes("$ref"); byte[] TYPE = Encoding.UTF8.GetBytes("$type"); string reference1 = string.Empty; string propertyName = string.Empty; PropertyInfo propertyInfo = null; Entity entity = null; Type entityType = null; while (reader.Read()) { if (reader.TokenType == JsonTokenType.StartObject) { Entity value = ReadObject(ref reader, options); propertyInfo.SetValue(entity, value); } else if (reader.TokenType == JsonTokenType.EndObject) { return(entity); } else if (reader.TokenType == JsonTokenType.PropertyName) { if (reader.ValueTextEquals(REF)) { reader.Read(); string referenceN = reader.GetString(); entity = (Entity)_resolver.ResolveReference(referenceN); while (reader.TokenType != JsonTokenType.EndObject) { if (!reader.Read()) { break; } } return(entity); } else if (reader.ValueTextEquals(ID)) { reader.Read(); reference1 = reader.GetString(); } else if (reader.ValueTextEquals(TYPE)) { reader.Read(); int typeCode = reader.GetInt32(); entityType = _binder.GetType(typeCode); entity = (Entity)Activator.CreateInstance(entityType); _resolver.AddReference(reference1, entity); } else { propertyName = reader.GetString(); propertyInfo = entityType.GetProperty(propertyName); } } else if (reader.TokenType == JsonTokenType.Null) { propertyInfo.SetValue(entity, null); } else if (reader.TokenType == JsonTokenType.True) { propertyInfo.SetValue(entity, true); } else if (reader.TokenType == JsonTokenType.False) { propertyInfo.SetValue(entity, false); } else if (reader.TokenType == JsonTokenType.Number) { propertyInfo.SetValue(entity, reader.GetUInt32()); } else if (reader.TokenType == JsonTokenType.String) { propertyInfo.SetValue(entity, reader.GetString()); } else if (reader.TokenType == JsonTokenType.StartArray) { IList list = (IList)propertyInfo.GetValue(entity); while (reader.TokenType != JsonTokenType.EndArray) { if (!reader.Read() || reader.TokenType == JsonTokenType.EndArray) { break; } Entity item = ReadObject(ref reader, options); list.Add(item); } } } return(entity); }