英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论