如何添加评论Json.NET输出? [英] How do I add comments to Json.NET output?
问题描述
有没有一种方法,我可以自动从JSON.Net
Is there a way I can automatically add comments to the serialised output from JSON.Net?
在理想情况下我想像它类似于下面的内容:
Ideally I'd imagine it's something similar to the following:
public class MyClass
{
[JsonComment("My documentation string")]
public string MyString { get; set; }
}
或(甚至更好,如果是可以避免的注释):
or (even better if annotations can be avoided):
public class MyClass
{
/// <summary>
/// My documentation string
/// </summary>
public string MyString { get; set; }
}
这将产生:
that would produce:
{
//My documentation string
"MyString": "Test"
}
这是我想问的是,我们用Json.NET以连载这可以通过手以后更改配置文件中的原因。我想包括在我的C#配置类的文档,并有再现的JSON帮谁就可能以后更改该文件。
The reason that I ask is that we use Json.NET to serialise a configuration file which could be changed by hand later on. I'd like to include documentation in my C# configuration classes and have that reproduced in the JSON to help whoever may have to change the file later.
更新:作为RoToRa低于所指出的,意见不是技术上的JSON规范允许的(见 HTTP得心应手的语法图://www.json。组织)。然而,在 Json.NET网站功能表包括:
Update: As RoToRa points out below, comments are not technically allowed in the JSON spec (see the handy syntax diagrams at http://www.json.org). However, the features table on the Json.NET site includes:
的支持读,写评论的
和 Newtonsoft.Json .JsonTextWriter.WriteComment(字符串)
存在哪些不输出注释。我感兴趣的创建意见,而不是使用 JsonTextWriter
直接
and Newtonsoft.Json.JsonTextWriter.WriteComment(string)
exists which does output a comment. I'm interested in a neat way of creating the comments rather than using the JsonTextWriter
directly.
推荐答案
的Json.NET JsonSerializer序列化时不会自动输出的意见。你需要手工编写JSON,或者使用JsonTextWriter或LINQ到JSON,如果你想评论
The Json.NET JsonSerializer doesn't automatically output comments when serializing. You'll need to write your JSON manually, either using JsonTextWriter or LINQ to JSON if you want comments
这篇关于如何添加评论Json.NET输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!