/// <summary>
/// Creates new instance of <see cref="SampleStructureMap"/> by deserializing it from the json file provided.
/// Returned instance will be fully loaded including other information that is not provided
/// in the json file like samples.
/// </summary>
/// <param name="groupsJSON">Full path to the groups JSON file</param>
/// <returns>Deserialized <see cref="SampleStructureMap"/></returns>
internal static SampleStructureMap Create(Stream groupsJSON)
{
var serializer = new DataContractJsonSerializer(typeof(SampleStructureMap));
SampleStructureMap structureMap = null;
try
{
// KD - Need two MemoryStreams? Need to investigate. Has to do with needing to open the json from the Android
// Activity which gives you a stream. Then you need to get back to bytes.
using (groupsJSON)
{
using (MemoryStream ms = new MemoryStream())
{
groupsJSON.CopyTo(ms);
var jsonInBytes = ms.ToArray();
using (MemoryStream ms2 = new MemoryStream(jsonInBytes))
{
structureMap = serializer.ReadObject(ms2) as SampleStructureMap;
structureMap.Samples = new List <SampleModel>();
}
}
}
}
catch (System.Exception ex)
{
Logger.WriteLine(ex.Message);
}
#region CreateSamples
List <string> pathList = new List <string>();
foreach (var category in structureMap.Categories)
{
foreach (var subCategory in category.SubCategories)
{
if (subCategory.SampleInfos != null)
{
foreach (var sample in subCategory.SampleInfos)
{
pathList.Add(sample.Path.Replace("/", "."));
}
}
}
}
var sampleModel = new SampleModel();
foreach (var samplePath in pathList)
{
sampleModel = SampleModel.Create(samplePath);
if (sampleModel != null)
{
structureMap.Samples.Add(sampleModel);
}
}
foreach (var category in structureMap.Categories)
{
foreach (var subCategory in category.SubCategories)
{
if (subCategory.Samples == null)
{
subCategory.Samples = new List <SampleModel>();
}
foreach (var sampleName in subCategory.SampleInfos)
{
var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName.SampleName);
if (sample == null)
{
continue;
}
subCategory.Samples.Add(sample);
}
}
}
#endregion
return(structureMap);
}