i am trying to add field in json while deserializing the response from server and then storing it into database
here is the model of Response
public class Response : RealmObject
{
[JsonConverter(typeof(QuotationConverter))]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public IList<Quotation> quotationsList { get; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public IList<Order> ordersList { get; }
}
QuotationConverter code where i am fetching customer name from another json and store it into quotation
protected override IList<Quotation> parseArray(Type objectType, JArray jsonArray)
{
try
{
Realm realm = Realm.GetInstance();
foreach (JObject data in jsonArray)
{
String customerId = data.GetValue("customerId").ToString();
Customer customer = realm.All<Customer>().Where(c => c.customerId == Java.Lang.Long.ParseLong(customerId)).FirstOrDefault();
if (customer != null)
{
String customerName = customer.customerName;
data.Add("customerName", customerName);
}
}
realm.Dispose();
var quotationsList = jsonArray.ToObject<IList<Quotation>>();
List<Quotation> quotation = new List<Quotation>(quotationsList);
return quotationsList;
}
catch(Exception e)
{
Debug.WriteLine(" exception "+e.StackTrace);
}
return null;
}
protected override Quotation parseObject(Type objectType, JObject jsonObject)
{
throw new NotImplementedException();
}
}
here is the JsonConverter
public abstract class JsonConverter<T> : Newtonsoft.Json.JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(JsonConverter<T>));
}
protected abstract T parseObject(Type objectType, JObject jsonObject);
protected abstract IList<T> parseArray(Type objectType, JArray jsonArray);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
{
var jsonArray = JArray.Load(reader);
var data= parseArray(objectType, jsonArray);
return data;
}
catch(Exception e)
{
Debug.WriteLine(e.StackTrace);
}
return null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Debug.WriteLine("Mo " + value);
}
}
the issue is when i am getting Response object inside that quotationsList is coming blank.
Json received from server
quotationsList: [
{
"account": null,
"contactId": 0,
"currency": "USD",
"customerId": 5637144583,
"deliveryAddress": "19TH and Edwardsville RD (RT203)\nGranite City,IL62040\nUSA",
"expiryDate": "2017-09-04",
"followUpDate": "2017-09-01",
"mQuotationId": null,
"opportunityId": 0,
"postedOn": null,
"prospectId": 0,
"quotationFor": null,
"quotationId": 5637155076,
"quotationName": "United States Steel",
"quotationNumber": "UST1-000022",
"quotationStatus": "Confirmed",
"requestReceiptDate": "2017-08-05",
"requestShipDate": "2017-08-05",
"siteId": "GLEN1",
"wareHouseId": "37"
}
expected json
quotationsList: [
{
"account": null,
"contactId": 0,
"currency": "USD",
"customerId": 5637144583,
"deliveryAddress": "19TH and Edwardsville RD (RT203)\nGranite City,IL62040\nUSA",
"expiryDate": "2017-09-04",
"followUpDate": "2017-09-01",
"mQuotationId": null,
"opportunityId": 0,
"postedOn": null,
"prospectId": 0,
"quotationFor": null,
"quotationId": 5637155076,
"quotationName": "United States Steel",
"quotationNumber": "UST1-000022",
"quotationStatus": "Confirmed",
"requestReceiptDate": "2017-08-05",
"requestShipDate": "2017-08-05",
"siteId": "GLEN1",
"wareHouseId": "37",
"customerName":"Jhon Caro"
}
Quotation model
public class Quotation : RealmObject , IMaster, IMedia, IProduct
{
[PrimaryKey]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public String mQuotationId { get; set; } = Guid.NewGuid().ToString();
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public long quotationId { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public String customerName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string quotationName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string quotationNumber{ get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string deliveryAddress { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string expiryDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string requestShipDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string requestReceiptDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public long prospectId { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string followUpDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public long opportunityId { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string postedOn { get; set; }
}
updated
protected override IList<Quotation> parseArray(Type objectType, JArray jsonArray)
{
try
{
Realm realm = Realm.GetInstance();
var data = jsonArray.ToObject<IList<Quotation>>();
List<Quotation> quotationList = new List<Quotation>(data);
foreach (Quotation quotation in quotationList)
{
long customerId = quotation.customerId;
Customer customer = realm.All<Customer>().Where(c => c.customerId == customerId).FirstOrDefault();
if (customer != null)
{
String customerName = customer.customerName;
quotation.customerName = customerName;
}
}
realm.Dispose();
return quotationList;
}
catch(Exception e)
{
Debug.WriteLine(" exception "+e.StackTrace);
}
return null;
}
this is how my deserialize get called
Response responseData = await Task.Run(() => JsonConvert.DeserializeObject(content));
customerName
, i am adding it as a new field – HuntcustomerName
property on theQuotation
after this lineList<Quotation> quotation = new List<Quotation>(quotationsList);
? – mjwillsquotationsList
has so many records and i am addingcustomerName
for all list items exist inquotationsList
so once i get the quotationsList prepared i am creating a new List and sending it back – HuntjsonArray
, fiddling withjsonArray
, and then converting it toList<Quotation>
. Have you tried getting JSON intojsonArray
, converting it toList<Quotation>
and then fiddling withList<Quotation>
instead? – mjwills