1

Couldn't find an answer from the other Json Serialization issue questions, so maybe someone can help me:

I'm getting a JSON object from a REST api and attempting to Deserialize it to an object. Below is the JSON Object I receive:

{"id":"6wVcZ9ZF67ECUQ8xuIjFT2",
"userId":"83ca0ab5-3b7c-48fe-8019-000320081b00",
"authorizations":["employee","API","trainer","queueAdmin","supervisor","workflowAdmin","realtimeManager","forecastAnalyst","qualityEvaluator","contactCenterManager","teamLead","personnelAdmin","telephonyAdmin","qualityAdmin","businessAdmin","businessUser","accountAdmin","dialerAdmin","contentManagementUser","contentManagementAdmin","admin","api","scriptDesigner","agent","user"],
"primaryAuthorization":"employee",
"thirdPartyOrgName":"in",
"username":"somebody",
"selfUri":"https://blahblahblah.com/api/v1/auth/sessions/6wVcZ9ZF67ECUQ8xuIjFT2"}

And my object I'm attempting to DeSerialize to:

 [Serializable]
public class Session : BaseRequest, ISession
{
    public Session(string url) : base(url)
    {
    }

    #region Members

    [JsonProperty(PropertyName = "userId")]
    public string UserId { get; set; }

    [JsonProperty(PropertyName = "authorizations")]
    public object[] Authorizations { get; set; }

    [JsonProperty(PropertyName = "primaryAuthorization")]
    public string PrimaryAuthorization { get; set; }

    [JsonProperty(PropertyName = "thirdPartyOrgName")]
    public string ThirdPartyOrgName { get; set; }

    [JsonProperty(PropertyName = "username")]
    public string Username { get; set; }

    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "selfUri")]
    public string SelfUri { get; set; }

    #endregion
}

I simply make the web request and get the response stream using a stream reader and return the string. Pretty standard.

However, when I attempt to Deserialize into my Session object it always throws an error: Value Cannot be Null

var serializer = new JsonSerializer();
response = MakePostRequest(true);
var obj = serializer.Deserialize<Session>(new JsonTextReader(new StringReader(response)));

The response is the JSON string I get back from the web request and is exact to what I specified above.

I've done this before but normally I've been the one that designed the REST api. Not the case this time but I can't for the life of my figure out why this won't deserialize? I've specified the JSonProperty PropertyName to avoid issues with proper casing, is this not working right maybe? Any help is appreciated!

UDPATE

I think I found part of the problem. It is attempting to deserialize my base class which consists of :

 public abstract class BaseRequest
{
    protected BaseRequest(string apiUrl)
    {
        ApiUrl = apiUrl;

        Request = (HttpWebRequest)WebRequest.Create(apiUrl);
    }

    public string ApiUrl { get; set; }

    public string JsonPayload { get; set; }

    public HttpWebRequest Request { get; private set; }
}

Is there any directive I can give to prevent it from doing so? Or will I need to refactor around this?

1
  • Your code work fine for me. Just give us the full exception message/stacktrace. Are you sure that response is not null ? You can get this exception if you pass null to StringReader constructor. Other option is exception to come from BaseRequest constructor. Post it's code too :) Commented Jun 18, 2014 at 17:07

3 Answers 3

1

Below code works (using Json.Net):

var session = JsonConvert.DeserializeObject<Session>(json);

public class Session
{
    public string Id { get; set; }
    public string UserId { get; set; }
    public List<string> Authorizations { get; set; }
    public string PrimaryAuthorization { get; set; }
    public string ThirdPartyOrgName { get; set; }
    public string Username { get; set; }
    public string SelfUri { get; set; }
}

EDIT

How should I tell it to ignore the base class?

var session = (Session)System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(typeof(Session));
JsonConvert.PopulateObject(DATA, session);

But I don't think this is a nice way of doing it. Changing your design may be a better solution.

Sign up to request clarification or add additional context in comments.

4 Comments

I think I see my issue finally. How should I tell it to ignore the base class?
@Encryption I have no idea what BaseRequest is. If you post it, I can try to help.
Posted what I have in my base class
Thanks. Definitely didn't expect that so I went ahead and did a slight refactor. Whenever I serialized it to Json I realized all the objects that were being pulled in...meh.
0

I've tested your code and it works fine, only change I made was removing the constructor, I take it that the serializer can't create an instance on the object for some reason, can you remove

public Session(string url) : base(url)
{
}

Comments

0

Your code works just fine for me but I haven't the BaseRequest source code so I made class with empty constructor.

IMO the exception is coming exactly from there. In the Session constructor the url parameter is null because your JSON object doesn't have url property. May be in the BaseRequest class you use this url param and you receive the Value Can't be Null error.

You can change just the name of parameter if this is the issue:

  public Session(string selfUri ) : base(selfUri)
  {
  }

Check also if the 'response' variable is null. StringReader can throw this exception if you pass null to its constructor.

1 Comment

If selfUri is the url OP wants to use in base class then this is the best answer. (I'll remove mine in that case)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.