ServiceSatck JSON国际化
问题描述:
由于与Newtonsoft相比,我使用ServiceStack nuget包进行JSON序列化/反序列化。我有一个数据结构,其中包含了一些特性,这是自定义对象的列表,这里有我的课ServiceSatck JSON国际化
public class MainBO
{
public IList<BasketItem> Items{get;set;}
}
public class BasketItem
{
public int BasketItemId { get; set; }
public string ItemId { get; set; }
public string Upc { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public Decimal OrginalPrice { get; set; }
public Decimal CurrentPrice { get; set; }
public IList<Decimal> OfferPrice { get; set; }
public Decimal ShippingCost { get; set; }
public Decimal DeliveryCharge { get; set; }
public string ReceiptMessage { get; set; }
public int Quantity { get; set; }
public bool? Discountable { get; set; }
public bool? Taxable { get; set; }
public bool IsPriceOveriddedItem { get; set; }
public string Taxcode { get; set; }
public string PriceOverrideReason { get; set; }
public string TaxOverrideReason { get; set; }
public int OriginalQuantity { get; set; }
public int RemainingQuantity { get; set; }
public IList<string> Hierarchy { get; set; }
public IList<LineDiscountBO> AppliedDiscounts { get; set; }
public IList<LineTaxBO> AppliedTaxes { get; set; }
public BasketItemStatus Status { get; set; }
public decimal EffectiveTaxPercent { get; set; }
public string ProductImage { get; set; }
public bool ShippingRequired { get; set; }
public string ReturnProductReason { get; set; }
public string OtherReason { get; set; }
}
public class LineTaxBO
{
public long TaxId { get; set; }
public string TaxClassId { get; set; }
public Decimal Amount { get; set; }
public Decimal Percentage { get; set; }
public string TaxOverrideReason { get; set; }
}
public class LineDiscountBO
{
public long DiscountId { get; set; }
public Decimal Amount { get; set; }
}
,我试图序列化一个JSONObject其中包含的数据
{"Items":[{"BasketItemId":1,"ItemId":"SK1XXX78","Upc":"671873084895","Name":"HTC 620","OrginalPrice":12,"CurrentPrice":8.4,"OfferPrice":[8.4],"ShippingCost":1.1,"DeliveryCharge":0,"ReceiptMessage":"Buy 1 Get 30% 2 Get 40% 3 Get 50% Discount","Quantity":1,"Discountable":true,"Taxable":true,"IsPriceOveriddedItem":false,"Taxcode":"12","OriginalQuantity":0,"RemainingQuantity":1,"Hierarchy":["760","760-001","760-001-002","760-001-002-001","760-001-002-001-YLGSNTSH","760-001-002-001-YLGSNTSH-10526160"],"AppliedTaxes":[{"TaxId":0,"TaxClassId":"12","Amount":0.25,"Percentage":3}],"Status":"Added","EffectiveTaxPercent":3,"ProductImage":"Mobiles\\6.jpg","ShippingRequired":false},{"BasketItemId":2,"ItemId":"SKXXX08","Upc":"400000331621","Name":"Wings of fire","OrginalPrice":9,"CurrentPrice":9,"OfferPrice":[9],"ShippingCost":1.1,"DeliveryCharge":0,"Quantity":1,"Discountable":false,"Taxable":true,"IsPriceOveriddedItem":false,"Taxcode":"11","OriginalQuantity":0,"RemainingQuantity":1,"Hierarchy":["600","600-001","600-001-001","600-001-001-001","600-001-001-001-PPPSHIRT1","600-001-001-001-PPPSHIRT1-90013155"],"AppliedTaxes":[{"TaxId":0,"TaxClassId":"11","Amount":0.18,"Percentage":2}],"Status":"Added","EffectiveTaxPercent":2,"ProductImage":"Books\\1.jpg","ShippingRequired":false}],"TotalAppliedDiscount":3.6,"TotalAppliedTax":0.43,"TotalApplicableTaxes":[{"TaxClass_Id":"12","Amount":0.25},{"TaxClass_Id":"11","Amount":0.18}],"ClientID":"'523e64ea-7748-48f6-94af-5433a2909bc2'","Total":17.83,"SubTotal":17.4,"AmountPaid":17.83,"BalanceDue":0,"Tenders":[{"TenderModeId":1,"TenderMode":"Cash","TenderedAmount":17.83}],"CustomerId":0,"IsReturnTransaction":false,"IndividualQuantityDisplay":false,"HasPromotion":true,"IsMember":false,"IsAssosiate":false,"TerminalId":"100","StoreId":"1001","ShippingAndHandlingCharge":0,"NumberOfItems":2}
这里是我的序列化方法,我不知道如何设置的值的层次
public static BasketBO DeserializeBasket(ServiceStack.Text.JsonObject data)//JObject data)
{
var basketBo = new MainBO;
basketBo.Items = data.ArrayObjects("Items").ConvertAll<BasketItem>(x => new BasketItem
{
BasketItemId = Convert.ToInt32(x["BasketItemId"]),
CurrentPrice = Convert.ToDecimal(x["CurrentPrice"]),
OriginalQuantity = Convert.ToInt32(x["OriginalQuantity"]),
ItemId = x["ItemId"],
Upc = x["Upc"],
Quantity = Convert.ToInt32(x["Quantity"]),
Name = x["Name"],
Taxable = Convert.ToBoolean(x["Taxable"]),
Taxcode = x["Taxcode"],
TaxOverrideReason = x["TaxOverrideReason"],
ShippingCost = Convert.ToDecimal(x["ShippingCost"]),
AppliedTaxes = x.ArrayObjects("AppliedTaxes") != null ? x.ArrayObjects("AppliedTaxes").ConvertAll<LineTaxBO>(tax => new LineTaxBO
{
Amount = Convert.ToDecimal(tax["Amount"]),
Percentage = Convert.ToDecimal(tax["Percentage"]),
TaxClassId = tax["TaxClassId"],
TaxId = Convert.ToInt64(tax["TaxId"]),
TaxOverrideReason = tax["TaxOverrideReason"]
}) : null,
AppliedDiscounts = x.ArrayObjects("AppliedDiscounts") != null ? x.ArrayObjects("AppliedDiscounts").ConvertAll<LineDiscountBO>(discount => new LineDiscountBO
{
Amount = Convert.ToDecimal(discount["Amount"]),
DiscountId = Convert.ToInt64(discount["DiscountId"])
}) : null,
Hierarchy = x.ArrayObjects("Hierarchy").ConvertAll<string>(str => str.ToString()),
});
return basketBo;
}
答
这有几个问题,您应该避免使用像接口哪一个是highly discouraged anti-pattern on DTO's。另一个问题是,默认情况下只ServiceStack.Text序列化器默认公共属性所以你应该改变你的序列化模式:
public class MainBO
{
List<TaxItem> Taxes { get; set; }
List<string> Parentlevels { get; set; }
}
否则,您可以配置ServiceStack连载公共领域有:
JsConfig.IncludePublicFields = true;
我忘了提及我的所有属性都是公共的,只有getter和setter。即我的班级是公共类美宝 { 公共税收的IList {获得;设置;} 公共Parentlevels的IList {获得;设置;} ---其他一些标准的类型属性 } –
@jereeshthomas除非您提供代码和json有错误,所以任何人都不可能识别问题并帮助你。也不要使用像IList这样的接口,因为它几乎总是隐藏一个具体的列表,所以它在实践中是一个无用的界面,而且它在串行化中更加糟糕,因为它只增加了耦合。还请提供**正在尝试反序列化的**精确** JSON,您的问题显示了属性名称和值中包含空格的马虎JSON,我们无法判断您的JSON是错误的还是粗心粘贴的。 –
mythz
我已经使用实际代码编辑了我的帖子。 –