I need to pass parameters in Json format from Sitecore item. I am getting Sitecore item using GlassMapper.
public interface ISomeType
{
//some fields
}
public interface ISomeFolder
{
IEnumerable<ISomeType> SubItems { get; set; }
}
...
var folder = _sitecoreContext.GetItem<ISomeFolder>(dataSourceId)
var list = folder.SubItems;
var json = JsonConvert.SerializeObject(list); // <= Here I get "Exception of type 'System.StackOverflowException' was thrown."
List type is Glass.Mapper.Sc.LazyItemEnumerable<ISomeType>
. It contains Castle.Proxies.ISomeTypeProxy
items, I can access to them. I can access to all their fields described in the interface. But I am getting System.StackOverflowException exception when trying to serialize it.
Is it possible to serialize GlassMapper objects to Json using Newtonsoft Json.NET?
If yes then what I am doing wrong? If no, what is the best way to convert GlassMapper object to Json?
StackOverflowException
is thrown because (depending on how you have your models set up) it tries to serialize all the parents/children, which in turn serializes their parents/children, etc.