예제 #1
0
파일: DataSet.cs 프로젝트: Levrum/Levrum
        public static DataSet <T> DeserializeJson(string json, bool forceOldStyle = false)
        {
            Exception lastEx = null;

            if (!forceOldStyle)
            {
                try
                {
                    DataSet <T> output = JsonConvert.DeserializeObject <DataSet <T> >(json);
                    return(output);
                }
                catch (Exception ex)
                {
                    lastEx = ex;
                }
            }

            // Try and load old-style DataSets here
            try
            {
                if (typeof(T) == typeof(IncidentData))
                {
                    json = json.Replace("DataSet", "DataSet016");
                    json = json.Replace("IncidentData", "IncidentData016");
                    json = json.Replace("ResponseData", "ResponseData016");

                    DataSet016 <IncidentData016> oldDataSet = JsonConvert.DeserializeObject <DataSet016 <IncidentData016> >(json, new JsonSerializerSettings()
                    {
                        PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.Auto
                    });
                    DataSet <IncidentData> output = new DataSet <IncidentData>();
                    foreach (IncidentData016 oldIncident in oldDataSet)
                    {
                        IncidentData incident = new IncidentData(data: oldIncident.Data);
                        foreach (ResponseData016 oldResponse in oldIncident.Responses)
                        {
                            ResponseData response = new ResponseData(oldResponse.Id, oldResponse.Data, oldResponse.TimingData.ToArray());
                            incident.Responses.Add(response);
                        }
                        output.Add(incident);
                    }
                    return(output as DataSet <T>);
                }
                else
                {
                    throw new NotImplementedException();
                }
            } catch (Exception ex2)
            {
                throw new Exception(string.Format("String does not contain valid DataSet JSON: {0} {1}", lastEx, ex2));
            }
        }
예제 #2
0
        public ResponseData016(string id = "", InternedDictionary <string, object> data = null, TimingData[] benchmarks = null)
        {
            Id         = id;
            TimingData = new DataSet016 <TimingData>(this);

            foreach (KeyValuePair <string, object> kvp in data)
            {
                if (!Data.ContainsKey(kvp.Key))
                {
                    Data.Add(kvp.Key, kvp.Value);
                }
                else
                {
                    Data[kvp.Key] = kvp.Value;
                }
            }

            if (benchmarks != null)
            {
                TimingData.AddRange(benchmarks);
            }
        }