/// <summary> /// Converts a JSON-file on string format to a list of corresponding NaceRegionData objects. /// </summary> /// <param name="jsonString">A JSON object represented as a string</param> /// <param name="attributeName">The name of the datafield. Used to find and set the correct property in NaceRegionObjects</param> /// <returns></returns> public async Task <List <NaceRegionData> > Convert(string jsonString, string attributeName) { un = new EuroStatJSONUnNester(jsonString); List <NaceRegionData> nrdList = new List <NaceRegionData>(); List <String> naceRegionYearFields = new List <String>(); List <int> numberOfItemsInFields = new List <int>(); if (!un.IsValidDataset()) { return(nrdList); } List <Region> regions = await databaseStore.getAllRegions(); List <Nace> naces = await databaseStore.getAllNaces(); foreach (KeyValuePair <String, String> entry in un.GetValues()) { List <int> totalElementsList = un.GetFields().ConvertAll(field => field.totalElements); List <int> indexes = findIndexesOfFields(int.Parse(entry.Key), totalElementsList); int naceId = indexes[un.GetFieldIndex("nace")]; int regionId = indexes[un.GetFieldIndex("region")]; int yearId = indexes[un.GetFieldIndex("year")]; string naceCode = un.GetNaceCode(naceId: naceId); string regionCode = un.GetRegionCode(regionId: regionId); int year = un.GetYear(yearId: yearId); double propValue = double.Parse(entry.Value, CultureInfo.InvariantCulture); Nace nace = naces.Find(nace => nace.naceCode == naceCode); Region region = regions.Find(region => region.regionCode == regionCode); if (nace != null && region != null) { NaceRegionData nrd = new NaceRegionData() { naceId = nace.naceId, regionId = region.regionId, year = year }; Type type = nrd.GetType(); PropertyInfo prop = type.GetProperty(attributeName); prop.SetValue(nrd, propValue, null); nrdList.Add(nrd); } } return(nrdList); }
public void GetValueShouldReturnExpectedList() { Dictionary <String, String> valueDict1 = jun1.GetValues(); string valuesString1 = "{"; foreach (KeyValuePair <String, String> entry in valueDict1) { valuesString1 = valuesString1 + "\"" + entry.Key + "\":" + entry.Value + ","; } valuesString1 = valuesString1.Remove(valuesString1.Length - 1, 1) + "}"; Assert.AreEqual(values1, valuesString1); Dictionary <String, String> valueDict2 = jun2.GetValues(); string valuesString2 = "{"; foreach (KeyValuePair <String, String> entry in valueDict2) { valuesString2 = valuesString2 + "\"" + entry.Key + "\":" + entry.Value + ","; } valuesString2 = valuesString2.Remove(valuesString2.Length - 1, 1) + "}"; Assert.AreEqual(values2, valuesString2); }