Unexpected token when deserializing object: String

May 6, 2009 at 4:01 PM
First let me join the chorus and say what a great library this is.

I'm getting an "Unexpected token when deserializing object: String" exception inside JsonSerializer.Deserialize:

    "request": "ux.settings.update",
    "sid": "14c561bd-32a8-457e-b4e5-4bba0832897f",
    "uid": "30c39065-0f31-de11-9442-001e3786a8ec",
    "fidOrder": [
    "entityName": "Andy Test",
    "setting": "entity.field.order"

The error is occurring at the "id" string in the array.

I believe the above is valid JSON but perhaps I'm missing something here. 

Anyone see what I'm doing wrong?

May 6, 2009 at 9:24 PM
We've pinned down what's happening here, although I'm not sure if this would be a bug or just expected behavior.

We are doing something perhaps a bit unusual.  From the above JSON, we deserialize twice, into two different C# classes, one that just contains a single property to hold a portion of the JSON string, ignoring all other JSON data:

    string Request { get; set; }

And the other that contains properties to hold all the other elements of data from the source JSON string except the "request" data.  We do this because our generic ajax request routing system only needs to know the request to dispatch the request to the proper handler.

We weren't sure that the deserialize method would gracefully handle the fact that the target object didn't have properties for much of the source JSON string.  But as it turned out, it handled it just fine up until we added the "fidOrder" field from above.  So we're theorizing that perhaps Deserialize cannot happily ignore when a nested structure is encountered in the JSON string if no matching target property exists in for the nested structure's data.

Before we code up a workaround, we're wondering if this is expected behavior.  Or is Deserialize supposed to ignore any and all JSON fields for which there are no matching target object properties even if the missing property is a composite or aggregate property?

May 8, 2009 at 6:40 AM

That works for me. Are you using the latest version of Json.NET?

May 8, 2009 at 1:32 PM

We are using the latest version, although when we first noticed the problem, we were using 3.0.  So it's happening with both 3.0 and the latest beta version. 

Let me just confirm that the code is intended to handle it okay when the deserialization target object does not contain fields to store all the values contained in the JSON string, even when the JSON contains nested data?

I'll build and run w/the source code and see if I can pick up more information.



May 8, 2009 at 11:58 PM

This is the test I wrote for your issue and works for me with the latest source:

    public class RequestOnly
      public string Request { get; set; }

    public class NonRequest
      public Guid Sid { get; set; }
      public Guid Uid { get; set; }
      public IList<string> FidOrder { get; set; }

    public void PartialClassDeserialize()
      string json = @"{
    ""request"": ""ux.settings.update"",
    ""sid"": ""14c561bd-32a8-457e-b4e5-4bba0832897f"",
    ""uid"": ""30c39065-0f31-de11-9442-001e3786a8ec"",
    ""fidOrder"": [
    ""entityName"": ""Andy Test"",
    ""setting"": ""entity.field.order""

      RequestOnly r = JsonConvert.DeserializeObject<RequestOnly>(json);
      Assert.AreEqual("ux.settings.update", r.Request);

      NonRequest n = JsonConvert.DeserializeObject<NonRequest>(json);
      Assert.AreEqual(new Guid("14c561bd-32a8-457e-b4e5-4bba0832897f"), n.Sid);
      Assert.AreEqual(new Guid("30c39065-0f31-de11-9442-001e3786a8ec"), n.Uid);
      Assert.AreEqual(8, n.FidOrder.Count);
      Assert.AreEqual("id", n.FidOrder[0]);
      Assert.AreEqual("titleId", n.FidOrder[n.FidOrder.Count - 1]);

May 12, 2009 at 1:38 PM
Edited May 12, 2009 at 1:40 PM

Okay - it's working with the latest code.  I had copied the latest binaries into the wrong project in our solution.

Thanks so much for your help and for the library.

Incidentally, I tried to make a donation via Pay Pal.   It told me it won't accept credit card payments.  Is that by design?  We're making good use of your library and my company would like to make a donation but I'm not really interested in giving Pay Pal my banking information (which appears like the only option to pay outside of using a credit-card which PP won't apparently accept for this donation).


May 13, 2009 at 2:14 AM
Edited May 13, 2009 at 3:53 AM

Hey thanks for pointing that out. I noticed my Paypal account was Personal rather than Premier which is limited to a certain number of credit card transactions in a given period. I've upgraded my account.

Could you have another try and let me know whether a credit card works now? :D

May 13, 2009 at 1:34 PM

Gave it another try and it still refuses to accept credit cards.

May 13, 2009 at 10:28 PM


I just tried using a credit card and it worked.

How are you going about donating? Are you logging into your paypal account? It worked for me without logging in.

What is the message you are getting?

May 14, 2009 at 12:16 PM

I was able to do the donation this morning by not logging in first.  If I log in to my account first, I got a message along the lines of "you cannot pay with this credit card - please chose another method for payment".  Or something to that effect.  Then, if I clicked the link to list payment methods, it showed credit-card as being grayed out.  However I did use that exact same credit card to make the donation.  Very strange.

Anyway, the donation is in my name (Andy Kay), but on behalf of my company (WAM Systems, Inc.).  Thanks again for this great library.

May 15, 2009 at 9:45 AM

Thanks. Glad you've found it helpful