如何防止 JsonWriter 在序列化多维数组时添加太多换行符?

Posted

技术标签:

【中文标题】如何防止 JsonWriter 在序列化多维数组时添加太多换行符?【英文标题】:How do I prevent JsonWriter from adding too many newlines while serializing a multidimensional array? 【发布时间】:2020-01-26 01:41:42 【问题描述】:

最近我一直在研究瓷砖地图编辑器。我正在使用 C#、MonoGame 和 Newtonsoft.Json 基本上我想用一个图层类列表来序列化一个 Map 类。 问题是,当在 Layer 类中序列化 int[,] MAP 时,它会为数组的每个元素添加换行符。

我想实现这个输出:

"MAP": 
  [
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17],
    [17,17,17,17,17,17,17,17,17,17]
  ]

我得到的却是:

"MAP": 
  [
    17,
    17,
    17,
    17,
    17,
    17,
    17,
    17,
    17,
    17
  ],
  [
    17,
    17,
    17,
    17,
    17,
    17,
    17,
    17,
    17,
    17
  ],
  and so on

我怎样才能避免这种情况?

我的序列化代码:

public void Save(string path, object obj)

   using (TextWriter sw = new StreamWriter(path))
   using (JsonWriter writer = new JsonTextWriter(sw))
   
        JsonSerializer serializer = new JsonSerializer();

        writer.Formatting = Formatting.Indented;
        serializer.Serialize(writer, obj);
        writer.Close();
    

【问题讨论】:

【参考方案1】:

您可以自定义JsonConverter 以在适当的时间间隔将作者的Formatting 设置切换为None

public class CustomFormattingMatrixConverter : JsonConverter

    public override bool CanConvert(Type objectType)
    
        return objectType == typeof(int[,]);
    

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    
        int[,] array = (int[,])value;
        writer.WriteStartArray();
        for (int i = 0, bound0 = array.GetUpperBound(0); i <= bound0; i++)
        
            writer.WriteStartArray();
            writer.Formatting = Formatting.None;
            for (int j = 0, bound1 = array.GetUpperBound(1); j <= bound1; j++)
            
                writer.WriteValue(array[i, j]);
            
            writer.WriteEndArray();
            writer.Formatting = Formatting.Indented;
        
        writer.WriteEndArray();
    

    public override bool CanRead
    
        get  return false; 
    

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    
        throw new NotImplementedException();
    

然后将它与您的序列化程序一起使用,如下所示:

JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new CustomFormattingMatrixConverter());

writer.Formatting = Formatting.Indented;
serializer.Serialize(writer, obj);

这里的工作演示:https://dotnetfiddle.net/q3P0en

【讨论】:

没问题;很高兴我能帮上忙。【参考方案2】:
writer.Formatting = Formatting.None;

而不是

writer.Formatting = Formatting.Indented;

【讨论】:

是的,很好 Formatting.None 将所有内容放入一行 Json 数据中

以上是关于如何防止 JsonWriter 在序列化多维数组时添加太多换行符?的主要内容,如果未能解决你的问题,请参考以下文章

如何将多维数组添加到序列化的 POST 数据中?

使用 ProtoBuf-Net,如何(反)序列化多维数组?

如何反序列化包含多维数组的json对象?

Newtonsoft.Json序列化和反序列之javascriptConvert.SerializeObject,DeserializeObject,JsonWriter,JsonReader(示例代

使用Jettison的多维数组

从 JSON 反序列化多维数组