0

I am using ServiceStack nuget package for JSON Serialization/ Deserialization since it is fast compares to Newtonsoft. I have a data structure which contains some properties which is a List of custom objects, here are my classes

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; } 
 }

and I tried to serialize a JsonObject which contains the data

 {"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}

Here is my Serialization method , I am not sure How to set the value for the Hierarchy

   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;
    }
jereesh thomas
  • 113
  • 1
  • 13

1 Answers1

0

This has a couple of issues, you should avoid using interfaces like IList which are a highly discouraged anti-pattern on DTO's. The other issue is that by default ServiceStack.Text only serialisers public properties by default so you should change your serializable model to:

public class MainBO
{
    List<TaxItem> Taxes { get; set; }
    List<string> Parentlevels { get; set; }
}

Otherwise you can configure ServiceStack to serialize public fields with:

JsConfig.IncludePublicFields = true;
Community
  • 1
  • 1
mythz
  • 141,670
  • 29
  • 246
  • 390
  • I forget to mention that all my properties are public only , with getters and setters. ie My Class is public class MainBO { public Taxes IList {get;set;} public Parentlevels IList{get;set;} --- some other standard type properties } – jereesh thomas Dec 15 '15 at 04:50
  • @jereeshthomas unless you provide the code and json that has the error, it's unlikely anyone will be able to identify the issue and help you. Also never use interfaces like IList, it's a useless interface in practice since it's nearly always hiding a concrete List and it's even worse in serialization where it only adds coupling. Please also provide the **exact** JSON you're trying to deserialise, your question shows sloppy JSON with spaces in property names and values, that we can't tell if your JSON is wrong or it was carelessly pasted. – mythz Dec 15 '15 at 05:03
  • @jereeshthomas what's the issue with just using `var dto = json.FromJson()`? The dto appears to be populated, what's missing? – mythz Dec 15 '15 at 16:52
  • @ mythz My Parameter is JsonObject , I can't change it , I have serialized it as string and then used json.FromJson and it seems to be working , thank you for the input . Is there any way I can directly convert from JsonObject? I have tried ConvertTo method but not getting proper values . – jereesh thomas Dec 16 '15 at 06:28
  • @jereeshthomas JsonObject is created from a JSON string so you should have access to the raw JSON string which I recommend you doing, it's also just a wrapper around a `Dictionary` which you can cast it to. There's only `ConvertTo` to convert it to a different object. – mythz Dec 16 '15 at 14:48