jason
jason

Reputation: 2259

json.net is throwing an error on serializing and deserializing a very simple class

I'm taking my first serious foray into working with Json and returning it from one application to another.

My application is an ASP.NET MVC 3 application using the .Net 4.0 Framework.

I need to serialize and deserialize a very simple class to and from json.

public class ProxyRequestResultDetails
{
    public string ApplicationName { get; set; }
    public string ProxyValue { get; set; }
    public bool ProxyRelationshipExists { get; set; }
}

The class doesn't inherit or implement anything.

I can successfully convert it to json by doing:

string json = JsonConvert.SerializeObject(requestDetails);

An example of the json created is:

"{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}"

Then I make the following call:

ProxyRequestResultDetails deserializedTestRequestDetails = 
                JsonConvert.DeserializeObject<ProxyRequestResultDetails>(json);

This has a stack trace like the one below. It's been formatted a bet to include all inner exceptions.

Method: CustomHandleErrorAttribute
     Message:  Error converting value "{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}" to type 'ProxySetup.Models.ProxyRequestResultDetails'. Path '', line 1, position 98. Inner Error #1: Could not cast or convert from System.String to ProxySetup.Models.ProxyRequestResultDetails.
     stack trace:    at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
   at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)

This seems to be a very simple object. I'm probably missing something simple, but I can't find what exactly from the examples. Any thoughts would be appreciated.

EDIT 1

Serious is right. That's exactly what's happening, but I'm not sure how to fit it. What I'm trying to do is expose an action on the controller such that it could be used like a web service call. The json created in the action as a raw string is:

{"ApplicationName":"Awesome App","ProxyValue":"0","ProxyRelationshipExists":true}

but the json returned by the action (with a return type of JsonResult)

return Json(json, "application/json; charset=utf-8", JsonRequestBehavior.AllowGet);

is:

"{\"ApplicationName\":\"Awesome App\",\"ProxyValue\":\"0\",\"ProxyRelationshipExists\":true}"

Thoughts?

EDIT 2 - SOLVED

This was simple enough. I changed the actions return type to

string

and returned the resulting string from the:

string json = JsonConvert.SerializeObject(requestDetails);

call.

Thanks Serious!

Upvotes: 3

Views: 9370

Answers (1)

Pragmateek
Pragmateek

Reputation: 13354

It seems like your serialized string has been escaped or something like that, making it appears as a string representing a string, not a string representing a JSON stream.

As an example :

string json = "{}";
string notJson = "\"{}\"";

So check the raw value of your string using the VS debugger for example.

Upvotes: 10

Related Questions