为非常简单的 类 调整和简化 DataContract 序列化?
Tweak and simplify DataContract serialization for very simple classes?
假设我有一个类型,其值可以完全由整数表示但具有验证逻辑,并且将在 Web API 中返回和接受。为了保持良好的代码并避免原始的痴迷,我可能会这样做:
public class FiscalPeriod
{
public FiscalPeriod(int id)
{
if (!Validator.IsValidPeriod(id))
throw new ArgumentException($"Invalid fiscal period ID '{id}'", nameof(id));
Id = id;
}
public int Id { get; private set; }
public override string ToString() => Id.ToString();
public static implicit operator int(FiscalPeriod period) => period.Id;
}
然后,当 Web API 调用返回此类型时,它会序列化为 JSON 为:
"FiscalPeriod" : {"Id": 201601},
有没有办法强制它序列化为以下内容?
"FiscalPeriod" : 201601,
以便在客户端更容易将其作为普通数字处理?
您可以告诉 Json.Net 如何使用 JsonConverter 属性处理类型。
public class ViewModel
{
public FiscalPeriod FiscalPeriod { get; set; }
}
public class FiscalPeriodConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var fiscalPeriod = value as FiscalPeriod;
writer.WriteStartObject();
serializer.Serialize(writer, fiscalPeriod.Id);
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return typeof(FiscalPeriod).IsAssignableFrom(objectType);
}
}
[JsonConverter(typeof(FiscalPeriodConverter))]
public class FiscalPeriod
{
public FiscalPeriod(int id)
{
if (!Validator.IsValidPeriod(id))
throw new ArgumentException($"Invalid fiscal period ID '{id}'", nameof(id));
Id = id;
}
public int Id { get; private set; }
public override string ToString() => Id.ToString();
public static implicit operator int(FiscalPeriod period) => period.Id;
}
http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverterAttribute.htm
假设我有一个类型,其值可以完全由整数表示但具有验证逻辑,并且将在 Web API 中返回和接受。为了保持良好的代码并避免原始的痴迷,我可能会这样做:
public class FiscalPeriod
{
public FiscalPeriod(int id)
{
if (!Validator.IsValidPeriod(id))
throw new ArgumentException($"Invalid fiscal period ID '{id}'", nameof(id));
Id = id;
}
public int Id { get; private set; }
public override string ToString() => Id.ToString();
public static implicit operator int(FiscalPeriod period) => period.Id;
}
然后,当 Web API 调用返回此类型时,它会序列化为 JSON 为:
"FiscalPeriod" : {"Id": 201601},
有没有办法强制它序列化为以下内容?
"FiscalPeriod" : 201601,
以便在客户端更容易将其作为普通数字处理?
您可以告诉 Json.Net 如何使用 JsonConverter 属性处理类型。
public class ViewModel
{
public FiscalPeriod FiscalPeriod { get; set; }
}
public class FiscalPeriodConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var fiscalPeriod = value as FiscalPeriod;
writer.WriteStartObject();
serializer.Serialize(writer, fiscalPeriod.Id);
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return typeof(FiscalPeriod).IsAssignableFrom(objectType);
}
}
[JsonConverter(typeof(FiscalPeriodConverter))]
public class FiscalPeriod
{
public FiscalPeriod(int id)
{
if (!Validator.IsValidPeriod(id))
throw new ArgumentException($"Invalid fiscal period ID '{id}'", nameof(id));
Id = id;
}
public int Id { get; private set; }
public override string ToString() => Id.ToString();
public static implicit operator int(FiscalPeriod period) => period.Id;
}
http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverterAttribute.htm