0

I'm using json.net for first time. It's really simple, but I have a problem: It seems to be JsonConvert.SerializeObject(Model, Formatting.Indented) should return no references and by using PreserveReferencesHandling = Ignore, it should dispose $id attributes as well, but I can't manage it.

My JsonConvert.SerializeObject call is in a razor view (MVC3) and JSON string I'm getting is

  {"Items": [
    {
      "$id": \"1\",
      "ID_DB": 18,
      "ID_SAP": null,
      ...
    }]
}

Perhaps, I need to change Web.config or put more config settings in another file(s) in my project. Please, can you tell me what I'm missing?

Thanks you all in advance.

Regards

Tistklehoff

  • There is probably some default configuration code in your project's global.asax.cs file or thereabouts which is causing this as the default behavior will definitely not preserve $id like that. Look for a call to config.Formatters or something similar. – Aluan Haddad Jun 02 '15 at 12:28
  • Hello, thanks a lot, I'm getting some data as "DATA": { $ref:XX } also, so, It's time to have a look to config.Formatters... – Tistklehoff Jun 02 '15 at 13:08
  • My global.asax.cs is clear, because I'm using one-shot transforms, as here: (Option 1, single jsonconvert) stackoverflow.com/questions/13274625/how-to-set-custom-jsonserializersettings-for-json-net-in-mvc-4-web-api Any ideas? – Tistklehoff Jun 02 '15 at 13:18
  • Well the question you in the link refers to AspNet WebApi and MVC4 but you are using working with an MVC3 project right? I believe MVC3 Controllers use the JsonDataContractsSerializer and it may be intercepting you somewhere in the request pipeline. https://msdn.microsoft.com/en-us/library/bb412179%28v=vs.110%29.aspx – Aluan Haddad Jun 02 '15 at 13:30
  • I think you need to use one of the approaches described in this thread to fix the problem. http://stackoverflow.com/questions/7109967/using-json-net-as-the-default-json-serializer-in-asp-net-mvc-3-is-it-possible – Aluan Haddad Jun 02 '15 at 13:43
  • @AluanHaddad: Yes, I'm using MVC3, but my code is in a razor view, and I'm using a string to assign json generated object to a javascript variable with only a Html.Raw(json) sentence. Anyway, I'll test this case, thanks! – Tistklehoff Jun 03 '15 at 06:44

1 Answers1

0

Thanks for all your tips Finally, I've just fix it: I was working with EntityObject generator, so, my data was not in POCO models. But, if I change to DbContext generator, Json.Net haven't any problems serializing to JSON my new POCO objects.

Anyway, perhaps, any of you could tell me if I missed any code to work with 'EntityObject style'. I'll try your suggestions, it's a matter of pride now... ;)

My Json.Net code remains untouched between EO and DbC versions: In DataView.cshtml, only this two lines:

 JsonSerializerSettings config = new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore, ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore, ContractResolver = new ExcludeEntityKeyContractResolver() };
 string json = JsonConvert.SerializeObject(Model, Formatting.Indented, config);

Thanks a lot for your time.

Regards.

Tistklehoff.