使用Newtonsoft.JSON将现有条目保留在json配置文件中进行序列化

huangapple go评论63阅读模式
英文:

Keeping existing entries when serializing in a json config file with Newtonsoft.JSON

问题

I am struggling a bit with the handling of a JSON settings file. Currently, we are deserializing a settings file on program startup into a defined object and serializing the object to the file when the application terminates. From time to time, we are changing the object structure (mostly adding new entities but sometimes removing obsolete entries, etc.) and this causes confusion for our developers as well as power users who are working with different releases (or development versions) because the settings file is always overwritten and forced to the current application version's format.

Now my question: Is there a good way to keep JSON entries that the currently running application version does not "know"? I already thought of achieving this behavior by only updating those entries that the current version knows and not touching others that are already in the file. Or by keeping a list of objects in memory the parser does not know after deserializing (this could be a little bit more complicated because of the different serialization of different data types) and then serializing those entries. But I am not a Newtonsoft expert, so I do not know how to get this into code.

Does anybody have advice or a kind of best-practice for this problem?

英文:

I am struggling a bit whith the handling of a JSON settings file. Currently we are deserializing a settings file on program startup into a defined object and serializing the object to the file when the application terminates. From time to time we are changing the object structure (mostly adding new entities but sometimes removing obsolete entries, etc.) and this causes confusion to our developers as well as power users which are working with different releases (or development versions) because the settings file is always overwritten and forced to the current application versions' format.

Now my question: Is there a good way to keep json entries that the currently running application version does not "know"? I already thought of achieving this behavior by only updating those entries that the current version knows and not touching others that are already in the file. Or by keeping a list of objects in memory the parser does not know after deserializing (this could be a little bit more complicated because of the different serialization of different data types) and the serializing those entries. But I am not a Newtonsoft expert so I do not know how to get this into code.

Does anyboady have advice or a kind of best-practice for this problem?

答案1

得分: 2

以下是您要翻译的内容:

解决方法是使用属性 JsonExtensionData。该属性指示设置未映射的属性在此处。

string json = @"{
  'UnknowProp1': 'Some value',
  'Id': 1,
  'Label': 'Hello',
  'UnknowProp2': 'Other value',
}";

var dto = JsonConvert.DeserializeObject<Dto>(json);
var result = JsonConvert.SerializeObject(dto, Formatting.Indented);

Console.WriteLine(result);


public class Dto
{
    public int Id { get; set; }
    public string Label { get; set; }

    [JsonExtensionData]
    private IDictionary<string, JToken> _additionalData;
}

结果:

{
  "Id": 1,
  "Label": "Hello",
  "UnknowProp1": "Some value",
  "UnknowProp2": "Other value"
}

但顺序未被保留。

英文:

A solution is to use the attribute JsonExtensionData. This attribute indicate to set unmapped property here.

string json = @"{
  'UnknowProp1': 'Some value',
  'Id': 1,
  'Label': 'Hello',
  'UnknowProp2': 'Other value',
}";

var dto = JsonConvert.DeserializeObject<Dto>(json);
var result = JsonConvert.SerializeObject(dto, Formatting.Indented);

Console.WriteLine(result);


public class Dto
{
    public int Id { get; set; }
    public string Label { get; set; }

    [JsonExtensionData]
    private IDictionary<string, JToken> _additionalData;
}

Result :

{
  "Id": 1,
  "Label": "Hello",
  "UnknowProp1": "Some value",
  "UnknowProp2": "Other value"
}

But the order isn't respected.

huangapple
  • 本文由 发表于 2023年6月26日 20:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定