在C#代码中,我希望使用Int32属性,因为它们被设计为int,但当使用Json.NET转换为Json时,我希望它们被序列化为URI,这样我就不必为了Json输出而将所有模型映射到另一个模型。例如简化模型:
public class Order
{
public int? AccountID { get; set; }
public int ProductID { get; set; }
public decimal Total { get; set; }
}
我希望这个像这样呈现:
{ "accountUri": "/account/123", "productUri": "/product/456", "total": 789.01 }
请注意,大小写和属性重命名已更改。
如果AccountID为null,那么json必须如下呈现:
{ "productUri": "/product/456", "total": 789.01 }
在C#代码中,我仍然希望像使用普通int一样使用属性,所以我想使用int运算符重写。
我不想在模型属性上使用属性,但我很乐意为Int32使用包装类,如果需要,也不介意在包装类上使用属性。
下面的代码离答案还有很长的路要走,但你会明白要点:
public class Order
{
public AccountIdentifier AccountID { get; set; }
public ProductIdentifier ProductID { get; set; }
public decimal Total { get; set; }
}
public abstract class IdentifierBase
{
private readonly string _uriPrefix;
private int? _value;
protected IdentifierBase(string uriPrefix, int? value)
{
_uriPrefix = uriPrefix;
_value = value;
}
public override string ToString()
{
if (_value.HasValue)
return _uriPrefix + _value.Value;
return null;
}
// insert int operator overrides here.
}
public class AccountIdentifier : IdentifierBase
{
public AccountIdentifier(int? value)
: base("/account/", value)
{
}
}
public class ProductIdentifier : IdentifierBase
{
public ProductIdentifier(int? value)
: base("/product/", value)
{
}
}
[Test]
public void JsonConvert()
{
var order = new Order
{
AccountID = new AccountIdentifier(123),
ProductID = new ProductIdentifier(456),
Total = 789.01M
};
using (var stringWriter = new StringWriter())
{
var writer = new JsonTextWriter(stringWriter) {Formatting = Formatting.None};
var settings = new JsonSerializerSettings();
var serializer = JsonSerializer.Create(settings);
// Camel case the properties.
serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
serializer.Serialize(writer, order);
writer.Flush();
var json = stringWriter.GetStringBuilder().ToString();
Console.Write(json);
}
}
该输出:
{"accountID":{},"productID":{},"total":789.01}
三个问题:
如何将"accountID"重命名为"accountUri"(将"productID"重命名成"productUri")?
如何呈现这些属性的值(用包装类的ToString()结果替换"{}"?
当一个属性为空时,我如何完全删除它?
谢谢。
EDIT:尽管为每个模型编写一个转换器需要做很多工作,但它确实节省了编写两个映射器的费用。这是我的概念验证测试:
[TestFixture]
public class MyPoC
{
public class OrderJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
var order = value as Order;
if (order.AccountID.HasValue)
{
writer.WritePropertyName("accountUri");
serializer.Serialize(writer, "/account/" + order.AccountID);
}
writer.WritePropertyName("productUri");
serializer.Serialize(writer, "/product/" + order.ProductID);
writer.WritePropertyName("total");
serializer.Serialize(writer, order.Total);
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var order = new Order();
var jsonObject = JObject.Load(reader);
order.AccountID = jsonObject.GetNullableIntFromUri("accountUri");
order.ProductID = jsonObject.GetIntFromUri("productUri");
order.Total = jsonObject["total"].Value<decimal>();
return order;
}
public override bool CanConvert(Type objectType)
{
return typeof(Order).IsAssignableFrom(objectType);
}
}
[Test]
public void JsonConvert_Is_Successful()
{
var order = new Order
{
AccountID = 123,
ProductID = 456,
Total = 789.01M
};
var json = JsonConvert.SerializeObject(order, Formatting.None, new OrderJsonConverter());
Console.WriteLine(json);
var deserialized = JsonConvert.DeserializeObject<Order>(json, new OrderJsonConverter());
Console.WriteLine("AccountID: {0}", deserialized.AccountID);
Console.WriteLine("ProductID: {0}", deserialized.ProductID);
Console.WriteLine("Total: {0}", deserialized.Total);
}
}
}
public static class JObjectExtensions
{
public static int GetIntFromUri(this JObject jsonObject, string propertyName)
{
var id = jsonObject.GetNullableIntFromUri(propertyName);
return id.Value;
}
public static int? GetNullableIntFromUri(this JObject jsonObject, string propertyName)
{
var uri = jsonObject[propertyName].ToObject<string>();
var s = Regex.Replace(uri, @".*/(d+)$", "$1");
int id;
if (int.TryParse(s, out id))
{
return id;
}
return null;
}
}
输出:
{"accountUri":"/account/123","productUri":"/product/456","total":789.01}
AccountID: 123
ProductID: 456
Total: 789.01
额外的工作将是验证uri的正确性,而不仅仅是通用的"从uri的末尾提取id"。
1)使用JsonProperty属性的PropertyName参数,例如:
[JsonProperty(PropertyName = "accountUri")]
public AccountIdentifier AccountID { get; set; }
2) {}是AccountIdentifier和ProductIdentifier类的内容。您需要为Order类编写一个自定义Json Converter,以便自定义(反)序列化内容。
以下是相关的JSON.NET文档。
3) 您也可以通过JsonProperty进行设置。
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
EDIT:这是Order类的自定义JsonConvert的一部分。它不使用上面提到的属性,而是手动处理它。如果其他人可以提供更好(更完整)的解决方案,请这样做。
public class OrderJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
var obj = value as Order;
writer.WritePropertyName("accountUri");
serializer.Serialize(writer, obj.AccountID.ToString());
writer.WritePropertyName("productUri");
serializer.Serialize(writer, obj.ProductID.ToString());
writer.WritePropertyName("Total");
serializer.Serialize(writer, obj.Total);
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(Order).IsAssignableFrom(objectType);
}
}
由于您已经决定考虑使用适配器模式,下面是一个带有测试的示例实现:
[TestFixture]
public class When_serializing_Order
{
[SetUp]
public void SetUp()
{
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};
}
[TestCase(123, 456, 789.01, "{"accountUri":"/account/123","productUri":"/product/456","total":789.01}")]
[TestCase(null, 456, 789.01, "{"productUri":"/product/456","total":789.01}")]
public void Should_render_exact_json(int? accountId, int productId, decimal total, string expectedJson)
{
var order = new Order
{
AccountID = accountId,
ProductID = productId,
Total = total
};
string jsonOrder = JsonConvert.SerializeObject(new OrderAdapter(order));
Assert.That(jsonOrder, Is.EqualTo(expectedJson));
}
}
public class Order
{
public int? AccountID { get; set; }
public int ProductID { get; set; }
public decimal Total { get; set; }
}
public class OrderAdapter
{
private readonly Uri _accountUri;
private readonly Uri _productUri;
private readonly decimal _total;
public OrderAdapter(Order order)
{
_accountUri = order.AccountID != null ? CreateRelativeUri("account", order.AccountID.Value) : null;
_productUri = CreateRelativeUri("product", order.ProductID);
_total = order.Total;
}
public Uri AccountUri { get { return _accountUri; } }
public Uri ProductUri { get { return _productUri; } }
public decimal Total { get { return _total; } }
private Uri CreateRelativeUri(string resourceType, int id)
{
return new Uri(String.Format("/{0}/{1}", resourceType, id), UriKind.Relative);
}
}
如果你有任何问题,请发表评论,我会注释任何需要进一步解释的内容。