/// <summary> /// Returns a string that represents the dictionary of buildable items in json format. /// </summary> /// <param name="items"></param> /// <returns></returns> public string ItemsToJSON(Dictionary<BuildableItemEnum, BuildableItem> items) { BuildableItemAPIMorph morph = new BuildableItemAPIMorph(); List<BuildableItemAPI> itemList = new List<BuildableItemAPI>(); foreach (BuildableItem item in items.Values) { itemList.Add((BuildableItemAPI)morph.ToAPIFormat(item)); } return Converter.Serlialize<List<BuildableItemAPI>>(itemList); }
// GET api/buildableitem public IEnumerable<BuildableItemAPI> Get() { BuildableItemAPIMorph morph = new BuildableItemAPIMorph(); List<BuildableItemAPI> items = new List<BuildableItemAPI>(); foreach (BuildableItem item in ItemManager.Instance.BuildableItems.Values) { items.Add((BuildableItemAPI)morph.ToAPIFormat(item)); } return items; }
/// <summary> /// Returns a dictionary of buildable items as represented by the supplied json string. /// </summary> /// <param name="json"></param> /// <returns></returns> public Dictionary<BuildableItemEnum, BuildableItem> ItemsFromJSON(string json) { Dictionary<BuildableItemEnum, BuildableItem> itemDictionary = new Dictionary<BuildableItemEnum, BuildableItem>(); BuildableItemAPIMorph morph = new BuildableItemAPIMorph(); List<BuildableItemAPI> itemList = Converter.Deserialize<List<BuildableItemAPI>>(json); foreach (BuildableItemAPI item in itemList) { itemDictionary[item.ItemCode] = (BuildableItem)morph.ToBusinessFormat(item); } return itemDictionary; }