JSOn Serializing PageData

Vote:
 

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?

#81206
Feb 11, 2014 15:41
Vote:
 

Hi Oliver,

As I am not sure what exactly you are trying to accomplish, so just a link to a post a wrote while back. It might do or help you do what you are trying to accomplish.

Best,

Jeroen.

#81209
Edited, Feb 11, 2014 16:41
Vote:
 

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.

#81212
Feb 11, 2014 17:08
Vote:
 

I am using handbars as template framework, and I need to json-serialize my episerver page properties.

#81214
Feb 11, 2014 19:02
Vote:
 

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.

#81215
Feb 11, 2014 20:05
Vote:
 

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;
        }

    

#81235
Feb 12, 2014 10:07
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.