5

Does the Json.Encode() Helper use the JavaScriptSerializer class to encode a string to json?

I am getting a circular reference exception when using Json.Encode(Model) even though my class properties that are being serialized have the [ScriptIgnore] attribute.

My only guess is that maybe the Json.Encode() helper doesn't use the JavaScriptSerializer to serialize to json but I can't find the documentation anywhere on msdn.

@Html.Raw(Json.Encode(Model))

Here's an example of one of the models that has a property that should not be serialized...

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.Script.Serialization;

namespace RobotDog.Entities {
    public class Character {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [MaxLength(200)]
        public string Name { get; set; }

        public virtual Person Person { get; set; }

        [ScriptIgnore]
        public virtual Movie Movie { get; set; }
    }
}
bflemi3
  • 6,698
  • 20
  • 88
  • 155
  • 1
    `Json.Encode` most likely uses the `System.Web.Mvc` namespace which differs from the `System.Web.Script.Serialization.JavaScriptSerializer`. What are you trying to achieve? – juanreyesv Jan 25 '13 at 02:26
  • In my controller I query my repository to get an `IEnumerable` and then in the view I serialize the model because I'm using KnockoutJS for the UI. – bflemi3 Jan 25 '13 at 14:22
  • I should also mention that my entities are in a separate class library project from my web project. – bflemi3 Jan 25 '13 at 19:52
  • In that case, in the action of your controller you could `return Json(jsonData, JsonRequestBehavior.AllowGet);` where "jsonData" is an array with the proper json structure that you need. I also use something similar to get data for populating a jqGrid in a razor view – juanreyesv Jan 28 '13 at 22:13

2 Answers2

6

Does the Json.Encode() Helper use the JavaScriptSerializer class to encode a string to json?

Yes.

From the source code:

private static readonly JavaScriptSerializer _serializer = Json.CreateSerializer();

public static string Encode(object value)
{
  DynamicJsonArray dynamicJsonArray = value as DynamicJsonArray;
  if (dynamicJsonArray != null)
    return Json._serializer.Serialize((object) (object[]) dynamicJsonArray);
  else
    return Json._serializer.Serialize(value);
}

where JavaScriptSerializer is System.Web.Script.Serialization.JavaScriptSerializer

also to assist your issue see this thread

Community
  • 1
  • 1
wal
  • 17,409
  • 8
  • 74
  • 109
  • Could someone put in or comment with a link to the source quoted above? I presume that Json.Decode also uses JavaScriptSerializer, but presumptions can get one into trouble so I'd like to check the source for Decode as well. Thanks. – David I. McIntosh Oct 24 '19 at 15:27
  • OK, I found this - perhaps someone can comment if it's the correct source: https://github.com/mono/aspnetwebstack/blob/master/src/System.Web.Helpers/Json.cs – David I. McIntosh Oct 24 '19 at 15:29
0

http://msdn.microsoft.com/en-us/library/system.web.helpers.json.encode(v=vs.111).aspx

according to the above link Json.Encode uses system.web.helpers.

What does your Model contain?

Also, are you sure that [ScriptIgnore] will ignore what you have it assigned to?

Anthony Nichols
  • 1,586
  • 2
  • 25
  • 50
  • Yeah I've looked at the msdn docs, and it wasn't really that helpful. I've added one of the entities that has a property that shouldn't be serialized. – bflemi3 Jan 25 '13 at 14:22