2

I am trying to serialize a JSON object to send it from the Controller to the View. Despite reading plenty of similar questions, I did not find a solution that works well.

In my case, I have a List<MyType> object, where is a pre-compiled class. But when I tried to serialize the data by means of:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MyType));

I got the following error:

Additional information: Type MyType cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.

However, since it is a pre-compiled class, I cannot mark the members. How can I solve this?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
AdaByron
  • 23
  • 3
  • Is there a particular reason you are using `DataContractJsonSerializer` as opposed to just using the `Json` method on your controller? Actually, I guess I should first ask- are you using ASP.NET MVC? – Brian Rogers Nov 10 '15 at 22:10
  • Hi Brian! No, I just used `DataContractJsonSerializer` but not for a particular reason. What would you recommend me instead? And yes, actually, my application is a ASP.NET MVC. I forgot to mention previously :) – AdaByron Nov 10 '15 at 22:25
  • Have a look at JSON.NET library. You should be able to do what you want to using the methods in this library. – h-rai Nov 10 '15 at 22:34
  • I will do it. I hope to find a eficient solution. Thanks! :) – AdaByron Nov 10 '15 at 22:54
  • @AdaByron What I would recommend is to use the built-in `Json` method (that's what it's there for after all), or switch to a robust 3rd-party serializer like Json.Net. I've added an answer talking about these options. – Brian Rogers Nov 10 '15 at 23:50

2 Answers2

0

I would not recommend using a precompiled class as your view model. Separation of concerns and whatnot.

Create a new class, MyTypeViewModel, which will have only the properties that the UI needs to know about, and map the properties from MyType to MyTypeViewModel. Then return like this in your controller method if you're returning this result as part of an API call: return Json(myTypeViewModel, JsonRequestBehavior.AllowGet);

or return View(myTypeViewModel) if you want to render an entire view.

Benjewman
  • 498
  • 4
  • 14
  • Thanks Ben!! Unfortunately, the election of the class is a constraint of the problem. Besides, I need lot of properties from the class, so it would not be easy create `MyTypeViewModel`. – AdaByron Nov 10 '15 at 22:10
0

The DataContractJsonSerializer uses opt-in semantics, which means that you must mark the class(es) to be serialized with a [DataContract] attribute, and mark all members of those classes that you want serialized with [DataMember]. If you cannot change the classes (because they are precompiled or you don't otherwise control the source code), then there's not much you can do with this serializer short of copying the data into another class that can be serialized. But, with ASP.NET MVC you shouldn't really be using DataContractJsonSerializer anyway-- this serializer was created for WCF and is not very flexible or user-friendly, IMO.

The ASP.NET MVC framework has a Json method built into the base controller, which uses the JavaScriptSerializer behind the scenes to serialize your model objects. This serializer does not require marking up classes with attributes, and because it is baked-in you don't have to put any special serialization code into your controller methods to use it. Just change your method to return JsonResult instead of ActionResult, then pass your object to the Json method as a last order of business.

For example:

[HttpGet]
public JsonResult GetItem(int id)
{
    PrecompiledClass pc = RetrieveObjectFromDatabaseOrWhatever(id);

    return Json(pc, JsonRequestBehavior.AllowGet);
}

The JavaScriptSerializer does have some limitations that I won't go into here, which you probably won't hit in most normal circumstances. But if you do find that the JavaScriptSerializer does not suit your needs, you can switch to a robust third-party serializer like Json.Net. See ASP.NET MVC and Json.NET and Using JSON.NET as the default JSON serializer in ASP.NET MVC - is it possible? to learn more about that option.

Community
  • 1
  • 1
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Wow! What a valuable information. I already did what you recommended me, and apparently I did not get any error, but how should I get the data from the View? – AdaByron Nov 11 '15 at 08:16
  • Aren't you using some sort of AJAX (e.g. [jQuery](https://learn.jquery.com/ajax/)) in your view? Presumably that is what led you to serialize your data to JSON in the first place. If you're not familiar with that, then it is probably time for you to work through some tutorials on the subject. – Brian Rogers Nov 11 '15 at 16:31
  • Yes, I used AJAX with the method `post` to submit the form, and I had to retrieve the data from the `success`. Thank you for your help!! – AdaByron Nov 12 '15 at 08:03
  • Thanks once again for all the information. It was very useful. Now the app is working perfectly! – AdaByron Nov 18 '15 at 10:18