我使用JSON包进行ServiceStack序列化/反序列化,因为与Newtonsoft相比,它更快。我有一个数据结构,其中包含一些属性,这是自定义对象的列表,下面是我的类
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;
}发布于 2015-12-15 12:37:32
这有几个问题,你应该避免使用像IList这样的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;https://stackoverflow.com/questions/34281062
复制相似问题