public static ExchangeEntry FromJson(JObject jObject, Material material, ExchangeData exchange, bool tryApplyToExchange = true) { ExchangeEntry entry; try { entry = jObject.ToObject <ExchangeEntry>(); entry.BuyOrders = ((JArray)jObject.GetValue("BuyingOrders")).Select(token => token.ToObject <ExchangeOrder>()).ToList(); entry.SellOrders = ((JArray)jObject.GetValue("SellingOrders")).Select(token => token.ToObject <ExchangeOrder>()).ToList(); entry.Exchange = exchange; entry.Material = material; } catch (Exception ex) when(ex is NullReferenceException || ex is ArgumentException || ex is FormatException || ex is JsonSerializationException) { //This is lacklustre and undescriptive. Maybe break up the try/catch into one for each thing? throw new JsonSchemaException("Exchange entry information is improperly formatted", ex); } if (tryApplyToExchange && exchange != null) { exchange.UpdateWithCommodityEntry(entry); } return(entry); }
public static ExchangeEntry FromJson(JObject jObject, List <Material> allMaterials, ExchangeData exchange, bool tryApplyToExchange = true) { Material material; try { material = allMaterials.Where(mat => mat.Ticker.Equals(jObject.GetValue("MaterialTicker").ToObject <string>())).First(); } catch (InvalidOperationException) { throw new ArgumentException($"Incomplete material list provided, missing{jObject.GetValue("MaterialTicker")}"); } catch (Exception ex) when(ex is NullReferenceException || ex is ArgumentException || ex is FormatException) { throw new JsonSchemaException("Material ticker information improperly formatted or missing", ex); } return(FromJson(jObject, material, exchange, tryApplyToExchange)); }