使用JSON.Net序列化/反序列化结构时,内置结构类型(如System.Drawing.Size)序列化为字符串,而自定义结构类型序列化为JSON对象。JSON.Net结构序列化差异
例如:
using System;
using System.Drawing;
using Newtonsoft.Json;
namespace TestJsonNet
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(JsonConvert.SerializeObject(new Size(50, 50)));
Console.WriteLine(JsonConvert.SerializeObject(new Size2(50, 50)));
}
}
struct Size2
{
public int Width { get; set; }
public int Height { get; set; }
public Size2(int w, int h) : this()
{
Width = w; Height = h;
}
}
}
输出如下:
"50, 50"
{"Width":50,"Height":50}
我能理解后面序列化结构为一个字符串的思维,因为内存布局始终是相同的;然而,为什么序列化自定义结构时出现差异?
另外,我会(为了内部遗留的原因),喜欢让JSON.Net序列化结构像后一种情况(即作为JSON,而不是字符串)。如果可能的话,那怎么能实现呢?
我没有找到这篇文章[JSON.NET系列化麻烦(http://stackoverflow.com/questions/9750836/json-net-serialization-trouble) ,但是我仍然想要一举关闭所有结构类型的这种行为。 –