private void LoadFromNode(XmlNode obj) { // loop through the nodes to get component info foreach (XmlNode dataNode in obj.ChildNodes) { switch (dataNode.Name) { case CreatedAtKey: _createdAt = dataNode.GetNodeContentAsDateTime(); break; case IDKey: case ComponentIDKey: _id = dataNode.GetNodeContentAsInt(); break; case NameKey: _name = dataNode.GetNodeContentAsString(); break; case DescriptionKey: _description = dataNode.GetNodeContentAsString(); break; case PricePerUnitInCentsKey: _pricePerUnit = dataNode.GetNodeContentAsInt(); break; case PricingSchemeKey: _pricingScheme = dataNode.GetNodeContentAsPricingSchemeType(); break; case ProductFamilyIDKey: _productFamilyID = dataNode.GetNodeContentAsInt(); break; case UnitNameKey: _unitName = dataNode.GetNodeContentAsString(); break; case UpdatedAtKey: _updatedAt = dataNode.GetNodeContentAsDateTime(); break; case KindKey: _kind = dataNode.GetNodeContentAsComponentType(); break; case UnitPriceKey: _unitPrice = dataNode.GetNodeContentAsDecimal(); break; case PricesKey: _prices = new List <IPriceBracketInfo>(); foreach (XmlNode priceNode in dataNode.ChildNodes) { var bracketInfo = new PriceBracketInfo(); foreach (XmlNode bracketNode in priceNode.ChildNodes) { switch (bracketNode.Name) { case "starting_quantity": bracketInfo.StartingQuantity = bracketNode.GetNodeContentAsNullableInt() ?? int.MinValue; break; case "ending_quantity": bracketInfo.EndingQuantity = bracketNode.GetNodeContentAsNullableInt() ?? int.MaxValue; break; case "unit_price": bracketInfo.UnitPrice = bracketNode.GetNodeContentAsDecimal(); break; default: break; } } _prices.Add(bracketInfo); } break; case ArchivedKey: _archived = dataNode.GetNodeContentAsBoolean(); break; default: break; } } }
private void LoadFromJSON(JsonObject obj) { // loop through the keys of this JsonObject to get component info, and parse it out foreach (string key in obj.Keys) { switch (key) { case CreatedAtKey: _createdAt = obj.GetJSONContentAsDateTime(key); break; case IDKey: case ComponentIDKey: _id = obj.GetJSONContentAsInt(key); break; case NameKey: _name = obj.GetJSONContentAsString(key); break; case DescriptionKey: _description = obj.GetJSONContentAsString(key); break; case PricePerUnitInCentsKey: _pricePerUnit = obj.GetJSONContentAsInt(key); break; case PricingSchemeKey: _pricingScheme = obj.GetJSONContentAsPricingSchemeType(key); break; case ProductFamilyIDKey: _productFamilyID = obj.GetJSONContentAsInt(key); break; case UnitNameKey: _unitName = obj.GetJSONContentAsString(key); break; case UpdatedAtKey: _updatedAt = obj.GetJSONContentAsDateTime(key); break; case KindKey: _kind = obj.GetJSONContentAsComponentType(key); break; case UnitPriceKey: _unitPrice = obj.GetJSONContentAsDecimal(key); break; case PricesKey: _prices = new List <IPriceBracketInfo>(); JsonArray pricesArray = obj[key] as JsonArray; if (pricesArray != null) { foreach (JsonObject priceObj in pricesArray.Items) { if (priceObj == null) { continue; } var bracketInfo = new PriceBracketInfo(); foreach (var bracketKey in priceObj.Keys) { switch (bracketKey) { case "starting_quantity": bracketInfo.StartingQuantity = priceObj.GetJSONContentAsNullableInt(bracketKey) ?? int.MinValue; break; case "ending_quantity": bracketInfo.EndingQuantity = priceObj.GetJSONContentAsNullableInt(bracketKey) ?? int.MaxValue; break; case "unit_price": bracketInfo.UnitPrice = priceObj.GetJSONContentAsDecimal(bracketKey); break; default: break; } } _prices.Add(bracketInfo); } } // Sanity check, should be equal. if (pricesArray != null && pricesArray.Length != _prices.Count) { throw new JsonParseException(string.Format("Unable to parse price brackets ({0} != {1})", pricesArray.Length, _prices.Count)); } break; case ArchivedKey: _archived = obj.GetJSONContentAsBoolean(key); break; default: break; } } }