c# - 将类序列化为其属性之一
问题描述
当我JsonConvert
使用以下代码序列化对象时:
JsonConvert.SerializeObject(new Foo())
结果是:
{"count":{"Value":3},"name":{"Value":"Tom"}}
我希望结果看起来像这样。所以没有嵌入式{ "Value": * }
结构。
{"count":3,"name":Tom}
我需要使用 JObject.FromObject 和 JsonConvert.SerializeObject。
Foo 类的代码:
public class Foo
{
public DeltaProperty<int> count = new DeltaProperty<int>(3);
public DeltaProperty<string> name = new DeltaProperty<string>("Tom");
}
public class DeltaProperty<T>
{
public T Value
{
get
{
m_isDirty = false;
return m_value;
}
set
{
if (!m_value.Equals(value))
{
m_isDirty = true;
m_value = value;
}
}
}
private bool m_isDirty = default;
private T m_value = default;
public DeltaProperty(T val)
{
Value = val;
}
public bool ShouldSerializeValue()
{
return m_isDirty;
}
public override string ToString()
{
return m_value.ToString();
}
}
解决方案
推荐阅读
- javascript - 关闭另一个模式时打开模式的问题
- python - 重采样轨迹以在每个样本中具有相等的欧几里德距离
- kubernetes - 如何将 Kubernetes 仪表板限制为分配给特定角色的用户的特定仪表板
- android - “WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS”在 Android 11 上不起作用
- reactjs - React 窗口表在扩展行时重置
- python - 从德语句子中提取动词
- rpa - 将日期分配给变量并在 UiPath 中使用
- python - Beautifulsoup4 - 出现错误“NoneType”对象没有属性“字符串”
- memory - 当我仍然有足够的内存时,cuda push::sort 遇到了内存问题
- r - 将列表列表转换为 data.frame 或 tibble (R) 的单个嵌套行