If you don't need/want the blockdata in your JSON output you might also add a [JsonIgnore] attribute to your ContentArea? And all properties you do not want in your output. But as I said, I am not sure what exactly you are trying to accomplish, so I am not sure if I'm answering your question.
I am using handbars as template framework, and I need to json-serialize my episerver page properties.
So you need to json-serialize only properties that need displaying, correct? I think you'd best try to add the [JsonIgnore] attribute to all properties that you don't use for display.
Additionally, if you have rich text properties, you might want to check if the html in them gets "javascript encoded" when it gets converted to json.
This was my workaround... Makes a non-chokable structure for Json serializing...
/// <summary>
/// This method converts a PageData into Dictionary objects. This methods is convinient when the serializer chokes on a PageData
/// </summary>
/// <param name="content"></param>
/// <param name="result"></param>
/// <returns>A key/value dictionary</returns>
public Dictionary<string, object> GetAllContentProperties(IContentData content, Dictionary<string, object> result)
{
foreach (var prop in content.Property)
{
if (prop.GetType().IsGenericType &&
prop.GetType().GetGenericTypeDefinition() == typeof (PropertyBlock<>))
{
var newStruct = new Dictionary<string, object>();
result.Add(prop.Name,newStruct);
GetAllContentProperties((IContentData)prop, newStruct);
continue;
}
if(prop.Value != null)
result.Add(prop.Name,prop.Value.ToString());
}
return result;
}
I am trying to serialize a PageData object into Json.
JsonConvert.SerializeObject(Model,Formatting.None, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore,NullValueHandling = NullValueHandling.Ignore }))
I am getting infinite loops until my server runs out of memory. My page has block data on them.
Is there another approach to this?