<bdo id='twgVg'></bdo><ul id='twgVg'></ul>
    <legend id='twgVg'><style id='twgVg'><dir id='twgVg'><q id='twgVg'></q></dir></style></legend>
  1. <small id='twgVg'></small><noframes id='twgVg'>

  2. <tfoot id='twgVg'></tfoot>
  3. <i id='twgVg'><tr id='twgVg'><dt id='twgVg'><q id='twgVg'><span id='twgVg'><b id='twgVg'><form id='twgVg'><ins id='twgVg'></ins><ul id='twgVg'></ul><sub id='twgVg'></sub></form><legend id='twgVg'></legend><bdo id='twgVg'><pre id='twgVg'><center id='twgVg'></center></pre></bdo></b><th id='twgVg'></th></span></q></dt></tr></i><div id='twgVg'><tfoot id='twgVg'></tfoot><dl id='twgVg'><fieldset id='twgVg'></fieldset></dl></div>

      如何仅将缩进序列化应用于某些属性?

      时间:2023-08-24
    1. <tfoot id='L6nwz'></tfoot>

          <small id='L6nwz'></small><noframes id='L6nwz'>

            <bdo id='L6nwz'></bdo><ul id='L6nwz'></ul>
          • <i id='L6nwz'><tr id='L6nwz'><dt id='L6nwz'><q id='L6nwz'><span id='L6nwz'><b id='L6nwz'><form id='L6nwz'><ins id='L6nwz'></ins><ul id='L6nwz'></ul><sub id='L6nwz'></sub></form><legend id='L6nwz'></legend><bdo id='L6nwz'><pre id='L6nwz'><center id='L6nwz'></center></pre></bdo></b><th id='L6nwz'></th></span></q></dt></tr></i><div id='L6nwz'><tfoot id='L6nwz'></tfoot><dl id='L6nwz'><fieldset id='L6nwz'></fieldset></dl></div>
                <legend id='L6nwz'><style id='L6nwz'><dir id='L6nwz'><q id='L6nwz'></q></dir></style></legend>
                  <tbody id='L6nwz'></tbody>

              1. 本文介绍了如何仅将缩进序列化应用于某些属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想以一种人类可读的方式将 .NET 对象序列化为 JSON,但我想更好地控制对象的属性或数组的元素是否以它们自己的一行结束.

                I want to serialize .NET objects to JSON in a human-readable way, but I would like to have more control about whether an object's properties or array's elements end up on a line of their own.

                目前我正在使用 JSON.NET 的 JsonConvert.SerializeObject(object, Formatting, JsonSerializerSettings) 方法进行序列化,但似乎我只能应用 Formatting.Indented(单行上的所有元素)或 Formatting.None (单行上的所有内容,没有任何空格)整个对象的全局格式规则.有没有办法在默认情况下全局使用缩进,但对于某些类或属性将其关闭,例如使用属性或其他参数?

                Currently I'm using JSON.NET's JsonConvert.SerializeObject(object, Formatting, JsonSerializerSettings) method for serialization, but it seems I can only apply the Formatting.Indented (all elements on individual lines) or Formatting.None (everything on a single line without any whitespace) formatting rules globally for the entire object. Is there a way to globally use indenting by default, but turn it off for certain classes or properties, e.g. using attributes or other parameters?

                为了帮助您理解问题,这里有一些输出示例.使用 Formatting.None:

                To help you understand the problem, here are some output examples. Using Formatting.None:

                {"array":["element 1","element 2","element 3"],"object":{"property1":"value1","property2":"value2"}}
                

                使用Formatting.Indented:

                {
                  "array": [
                    "element 1",
                    "element 2",
                    "element 3"
                  ],
                  "object": {
                    "property1": "value1",
                    "property2":"value2"
                  }
                }
                

                我想看什么:

                {
                  "array": ["element 1","element 2","element 3"],
                  "object": {"property1":"value1","property2":"value2"}
                }
                

                (我意识到我的问题可能与 这个,但那里的评论完全没有抓住重点,实际上并没有提供有效的答案.)

                (I realize my question may be slightly related to this one, but the comments there totally miss the point and don't actually provide a valid answer.)

                推荐答案

                一种可能是为您需要特殊处理的特定类型编写自定义 Json 转换器并为它们切换格式:

                One possibility would be to write a custom Json converter for the specific types you need special handling and switch the formatting for them:

                class Program
                {
                    static void Main()
                    {
                        var root = new Root
                        {
                            Array = new[] { "element 1", "element 2", "element 3" },
                            Object = new Obj
                            {
                                Property1 = "value1",
                                Property2 = "value2",
                            },
                        };
                        var settings = new JsonSerializerSettings
                        {
                            Formatting = Formatting.Indented,
                        };
                        settings.Converters.Add(new MyConverter());
                
                        string json = JsonConvert.SerializeObject(root, settings);
                        Console.WriteLine(json);
                    }
                }
                
                public class Root
                {
                    public string[] Array { get; set; }
                    public Obj Object { get; set; }
                }
                
                public class Obj
                {
                    public string Property1 { get; set; }
                    public string Property2 { get; set; }
                }
                
                class MyConverter : JsonConverter
                {
                    public override bool CanConvert(Type objectType)
                    {
                        return objectType == typeof(string[]) || objectType == typeof(Obj);
                    }
                
                    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                    {
                        throw new NotImplementedException();
                    }
                
                    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                    {
                        writer.WriteRawValue(JsonConvert.SerializeObject(value, Formatting.None));
                    }
                }
                

                这将输出:

                {
                  "Array": ["element 1","element 2","element 3"],
                  "Object": {"Property1":"value1","Property2":"value2"}
                }
                

                这篇关于如何仅将缩进序列化应用于某些属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何告诉 Json.NET 忽略第 3 方对象中的属性? 下一篇:如何从 JSON 字符串中获取深度嵌套的属性?

                相关文章

                最新文章

                  <bdo id='WZbxl'></bdo><ul id='WZbxl'></ul>

                  <tfoot id='WZbxl'></tfoot>
                    <legend id='WZbxl'><style id='WZbxl'><dir id='WZbxl'><q id='WZbxl'></q></dir></style></legend>

                    <small id='WZbxl'></small><noframes id='WZbxl'>

                  1. <i id='WZbxl'><tr id='WZbxl'><dt id='WZbxl'><q id='WZbxl'><span id='WZbxl'><b id='WZbxl'><form id='WZbxl'><ins id='WZbxl'></ins><ul id='WZbxl'></ul><sub id='WZbxl'></sub></form><legend id='WZbxl'></legend><bdo id='WZbxl'><pre id='WZbxl'><center id='WZbxl'></center></pre></bdo></b><th id='WZbxl'></th></span></q></dt></tr></i><div id='WZbxl'><tfoot id='WZbxl'></tfoot><dl id='WZbxl'><fieldset id='WZbxl'></fieldset></dl></div>