[JsonObject(MemberSerialization.OptIn)]//默认情况下,所有的成员不会被序列化,类中的成员只有标有特性JsonProperty的才会被序列化,当类的成员很多,但客户端仅仅需要一部分数据时,很有用
public class testData
{
[JsonProperty]
public string name;
public int index;
public Vector3 pos;
public Color color;
public bool isOn;
}
OptOut 默认值,类中所有公有成员会被序列化,如果不想被序列化,可以用特性JsonIgnore
JsonSerializerSettings setting = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};
如果序列化Color 字段 需要加上上面忽略循环引用 否则会报错 无法序列化,但是可以序列化Vector3
[JsonConverter(typeof(LongDateTimeConvert))] //可以使用自己定义的格式序列化 日期
public DateTime Birthday;
public class LongDateTimeConvert : IsoDateTimeConverter
{
public LongDateTimeConvert() : base()
{
base.DateTimeFormat = “yyyy-MM-dd HH:mm:ss”;
}
}