I have a POCO class which looks like this:
public class Item : Asset
{
public int PlaylistId { get; set; }
public int AssetId { get; set; }
public double Duration { get; set; }
public int Order { get; set; }
}
and Asset looks like this:
public enum AssetType
{
Image = 1,
Video,
Website
}
public class Asset
{
public int Id { get; set; }
public string Name { get; set; }
public string Filename { get; set; }
public AssetType Type { get; set; }
public string CreatedById { get; set; }
public string ModifiedById { get; set; }
[Display(Name="Created by")] public string CreatedBy { get; set; }
[Display(Name="Modified by")] public string ModifiedBy { get; set; }
}
and then I have a json file which looks like this:
{
"Items":[
{
"PlaylistId":1,
"Type":2,
"Duration":19,
"Filename":"stream1_mpeg4.avi"
},
{
"PlaylistId":1,
"Type":2,
"Duration":21,
"Filename":"stream2_mpeg4.avi"
}
]
}
and finally I have my code which looks like this:
public IList<Item> GetAll()
{
if (File.Exists(itemsPath))
{
using (var fs = new FileStream(itemsPath, FileMode.Open))
using (var sr = new StreamReader(fs))
{
var text = sr.ReadToEnd();
var array = JsonConvert.DeserializeObject<Item[]>(sr.ReadToEnd());
return array.ToList();
}
}
else
throw new FileNotFoundException("Unable to find the playlist, please make sure that " + itemsPath + " exists.");
}
The text variable contains the correct json string as I would expect, but the array is null, therefore array.ToList(); throws an error. Does anyone know what I am doing wrong?
Cheers in advance /r3plica