public static List <Dictionary <string, object> > GenerateRows(IRFDataSet dataSet) { var result = new List <Dictionary <string, object> >(); var rowType = dataSet.GetRowType(); foreach (var r in dataSet.GetRows()) { var row = new Dictionary <string, object>(); foreach (var propertyInfo in rowType.GetProperties()) { if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo)) { var valueStruct = propertyInfo.GetValue(r); foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties()) { if (!row.ContainsKey(innerPropertyInfo.Name)) { row.Add(innerPropertyInfo.Name, innerPropertyInfo.GetValue(valueStruct)); } // also add children if no conflict, so 2 levels of drill-down if (RFReflectionHelpers.IsStruct(innerPropertyInfo) || RFReflectionHelpers.IsMappingKey(innerPropertyInfo)) { var innerStruct = innerPropertyInfo.GetValue(valueStruct); foreach (var inner2PropertyInfo in innerPropertyInfo.PropertyType.GetProperties()) { if (!row.ContainsKey(inner2PropertyInfo.Name)) { row.Add(inner2PropertyInfo.Name, inner2PropertyInfo.GetValue(innerStruct)); } } } } } else { if (!row.ContainsKey(propertyInfo.Name)) { row.Add(propertyInfo.Name, propertyInfo.GetValue(r)); } } } result.Add(row); } return(result); }
public static string ExportToJSON(IRFDataSet dataSet, List <KeyValuePair <string, Type> > columnTypes) { var array = new JArray(); // header row var rowType = dataSet.GetRowType(); foreach (var r in dataSet.GetRows()) { var obj = new JObject(); foreach (var propertyInfo in rowType.GetProperties()) { if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo)) { var valueStruct = propertyInfo.GetValue(r); foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties()) { if (valueStruct != null) { var columnName = String.Format("{0},{1}", propertyInfo.Name, innerPropertyInfo.Name); obj.Add(new JProperty( columnName, GetValue(columnName, innerPropertyInfo.GetValue(valueStruct), columnTypes))); } } } else { var columnName = propertyInfo.Name; obj.Add(new JProperty( columnName, GetValue(columnName, propertyInfo.GetValue(r), columnTypes))); } } array.Add(obj); } return(array.ToString()); }