将JsonValue反序列化为系统中的模型.Json



在任何人开始建议任何像Newtonsoft.JsonSystem.Text.Json这样的库或我喜欢使用的任何其他漂亮而简单的东西之前,要知道我不能使用System.Json以外的任何东西,因为我在应用程序的约束下工作,(我正在制作一个插件(,我对它没有影响,并且已经停止了主动开发,(这是一个ERP系统,每年只有一次安全补丁,功能请求会导致被动的攻击性响应;即使我主动提出自己免费进行更改(。

我有一些json和一些不错的域模型(对象、类、模型、实体,你喜欢称之为带有属性的公共类的任何东西(,我希望它们结婚。当有嵌套时,使用反射是一种痛苦。

有人能告诉我一些不需要任何裸体或dll的好方法吗?我在搜索时发现的所有内容都与System.Json以外的所有其他库有关。

以下是我在放弃之前一直在做的事情(我已经重构到了看起来像用例的东西,但那是因为合同原因(:

public void BuildSettings(string settingsPath = "appsettings.json", params Type[] types)
{
if (!types.Any())
throw new ArgumentException("The type parameters cannot be empty", nameof(types));
var file = new FileInfo(settingsPath);
if (!file.Exists)
throw new ArgumentException($"No settings file found in the path '{settingsPath}'", nameof(settingsPath));
using (var reader = file.OpenText())
{
var rootJson = JsonValue.Load(reader);
if (rootJson.JsonType != JsonType.Object)
throw new ArgumentException($"The settings file must be a Json Object, but a '{rootJson.JsonType}' was found", nameof(settingsPath));
var jsonObject = rootJson as JsonObject;
if (jsonObject == null)
throw new NullReferenceException("The json object is null");
foreach (var type in types)
{
if (jsonObject.ContainsKey(type.Name))
{
var jsonSetting = jsonObject[type.Name] as JsonObject;
var properties = type.GetProperties();
foreach (var property in properties)
{
var value = jsonSetting[property.Name];
var propertyType = property.PropertyType;
property.SetValue();
// TODO: Ask StackOwerflow
}
}
}
}
}

这有点愚蠢,但我不制定规则

我想你要待很长时间了。。。在我看来,你必须开发一个ORM,我曾经遇到过同样的问题,我必须为ATM机制作ORM。

这段代码不适用于您,因为它假设有一个IDataReader,但是您需要使用反射的属性映射,而这段代码就在那里。

让我知道这是否会让你继续前进,因为它在重用反射类型等方面进行了一些优化。

我认为您需要测试一个属性是否为类,并使用激活器创建它,并使用使用有效的强制转换

if (property.PropertyType.BaseType == typeof(Enum))
{
property.SetValue(obj, (int)value);
}
else if (property.PropertyType.BaseType == typeof(Guid))
{
property.SetValue(obj, Guid.Parse(value.ToString().ToUpper()));
}
else
{
property.SetValue(obj, Convert.ChangeType(value, property.PropertyType));
}

我不知道你需要如何支持我的调用,或者如果它是一个固定的数量,你可能只是在做了一个静态的T Parse(这个T目标,字符串json(方法之后其中,基于{和}括号对json字符串进行分段以获得属性,并使用[和]获得数组。

这是它的代码,我把它用于一个VCARD Json解析器,我前段时间不得不制作

/// <summary>
/// Splits the specified string in sections of open en closing characters.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="open">The opening char indicating where to start to read .</param>
/// <param name="close">The close char, indicating the part where should stop reading.</param>
/// <returns>IReadOnlyList&lt;System.String&gt;.</returns>
/// <exception cref="System.ArgumentNullException">text</exception>
/// <exception cref="ArgumentNullException">Will throw an exception if the string that needs to be split is null or empty</exception>
public static IReadOnlyList<string> Split(this string text, char open, char close)
{
if (text is null)
{
throw new ArgumentNullException(nameof(text));
}
var counted = 0;
var result = new List<string>();
var sb = new StringBuilder();
foreach (char c in text)
{
if (c == open)
{
if (counted != 0)
sb.Append(c);
counted++;
continue;
}
if (c == close)
{
counted--;
if (counted != 0)
sb.Append(c);
continue;
}
if (counted > 0)
{
sb.Append(c);
}
else if (counted == 0 && sb.Length > 0)
{
result.Add(sb.ToString());
sb.Clear();
}
}
return result;
}

这是我必须制作的完整地图,你可以看到上面提到的反射

class Mapper
{
ConcurrentDictionary<Type, PropertyInfo[]> _properties = new ConcurrentDictionary<Type, PropertyInfo[]>();
ConcurrentDictionary<string, List<string>> _fieldNames = new ConcurrentDictionary<string, List<string>>();
/// <summary>
/// Maps the specified reader to a given class. the reader must contain all properties of the type provided.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader">The reader.</param>
/// <returns></returns>
public IEnumerable<T> Map<T>(SqlDataReader reader)
{
var result = new List<T>();
if (!reader.HasRows)
return result;

var type = typeof(T);
if (!_properties.TryGetValue(type, out PropertyInfo[] prop))
{
prop = type.GetProperties();
_properties.TryAdd(type, prop);
}

if (!_fieldNames.TryGetValue(type.Name, out List<string> fieldNames))
{
var names = new List<string>(reader.FieldCount);
for (int i = 0; i < reader.FieldCount; i++)
{
names.Add(reader.GetName(i));
}
fieldNames = names;
_fieldNames.TryAdd(type.Name, fieldNames);
}
while (reader.Read())
{
var obj = Activator.CreateInstance<T>();
foreach (var property in prop)
{
if (fieldNames.Contains(property.Name))
{
var value = reader[property.Name];
if (value == DBNull.Value)
continue;
if (property.PropertyType.BaseType == typeof(Enum))
{
property.SetValue(obj, (int)value);
}
else if (property.PropertyType.BaseType == typeof(Guid))
{
property.SetValue(obj, Guid.Parse(value.ToString().ToUpper()));
}
else
{
property.SetValue(obj, Convert.ChangeType(value, property.PropertyType));
}
}
}
result.Add(obj);
}
return result;
}
public IEnumerable<T> Map<T,Y>(SqlDataReader reader,Y owner)
{
var result = new List<T>();
if (!reader.HasRows)
return result;

var type = typeof(T);
if (!_properties.TryGetValue(type, out PropertyInfo[] prop))
{
prop = type.GetProperties();
_properties.TryAdd(type, prop);
}
if (!_fieldNames.TryGetValue(type.Name, out List<string> fieldNames))
{
var names = new List<string>(reader.FieldCount);
for (int i = 0; i < reader.FieldCount; i++)
{
names.Add(reader.GetName(i));
}
fieldNames = names;
_fieldNames.TryAdd(type.Name, fieldNames);
}
while (reader.Read())
{
var obj = Activator.CreateInstance<T>();
foreach (var property in prop)
{
if (property.PropertyType == typeof(Y))
{
property.SetValue(obj, owner);
continue;
}
if (fieldNames.Contains(property.Name))
{
var value = reader[property.Name];
if (value == DBNull.Value)
continue;
if (property.PropertyType.BaseType == typeof(Enum))
{
property.SetValue(obj, (int)value);
}
else
{
property.SetValue(obj, Convert.ChangeType(value, property.PropertyType));
}
}
}
result.Add(obj);
}
return result;
}
/// <summary>
/// Maps the specified reader to a given class. the reader must contain all properties of the type provided.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader">The reader.</param>
/// <returns></returns>
public T MapOne<T>(SqlDataReader reader)
{
if (!reader.HasRows)
return default;

var type = typeof(T);
if (!_properties.TryGetValue(type, out PropertyInfo[] prop))
{
prop = type.GetProperties();
_properties.TryAdd(type, prop);
}

if (!_fieldNames.TryGetValue(type.Name, out  List<string> fieldNames))
{
var names = new List<string>(reader.FieldCount);
for (int i = 0; i < reader.FieldCount; i++)
{
names.Add(reader.GetName(i));
}
fieldNames = names;
_fieldNames.TryAdd(type.Name, fieldNames);
}
if (reader.Read())
{
var obj = Activator.CreateInstance<T>();
foreach (var property in prop)
{
if (fieldNames.Contains(property.Name))
property.SetValue(obj, reader[property.Name]);
}
return obj;
}
else
{
return default;
}
}

/// <summary>
/// Maps the specified reader to a given class. the reader must contain all properties of the type provided.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader">The reader.</param>
/// <returns></returns>
public IEnumerable<T> Map<T>(SqlDataReader reader, object[] args)
{
var result = new List<T>();
if (!reader.HasRows)
return result;

var type = typeof(T);
if (!_properties.TryGetValue(type, out PropertyInfo[] prop))
{
prop = type.GetProperties();
_properties.TryAdd(type, prop);
}

if (!_fieldNames.TryGetValue(type.Name, out List<string> fieldNames))
{
var names = new List<string>(reader.FieldCount);
for (int i = 0; i < reader.FieldCount; i++)
{
names.Add(reader.GetName(i));
}
fieldNames = names;
_fieldNames.TryAdd(type.Name, fieldNames);
}
while (reader.Read())
{
var obj = (T)Activator.CreateInstance(type, args);
foreach (var property in prop)
{
if (fieldNames.Contains(property.Name))
property.SetValue(obj, reader[property.Name]);
}
result.Add(obj);
}
return result;
}
}

最新更新