Json.NET Media Formatter: {"Cannot deserialize JSON object into type 'System.Json.JsonValue'."}

Topics: Web Api
Sep 12, 2011 at 5:31 AM
Edited Sep 12, 2011 at 5:48 AM

Hi there,

I am using Preview 4.

I started using the JsonNetMediaTypeFormatter available on NuGet: : http://nuget.org/List/Packages/netfx-WebApi.JsonNetFormatter

I have a MessageHandler where I do some special processing of the request and response.
When using the Json.NET Media Type Formatter I was getting an unhandled exception when deserializing during the call to OnReadFromStream:

{"Cannot deserialize JSON object into type 'System.Json.JsonValue'."}

I was able to avoid this by implementing:

 

    // sb: 11/sept/2011 implemented this as a workaround on preview 4
    // to avoid unhandeled exceptions:
    // {"Cannot deserialize JSON object into type 'System.Json.JsonValue'."}
    // TODO: check once RTM is out, if still needed.
    protected override bool OnCanReadType(Type type)
    {
        if (typeof(JsonValue) == type)
        {
            return false;
        }
        return true;
    }

Has anyone else run into this before?
Is this the recommended solution?

I'm not sure if this is to be even considered a bug with web api, could well be a json.net issue.

Anyhow, in case it helps, the following test code fails with the same exception:

            string jsonTest = "{\"MemberId\":1,\"AchievementId\":1}";
            var test1 = JsonConvert.DeserializeObject(jsonTest, typeof(JsonValue));
Thank you!
 

 

Sep 12, 2011 at 4:42 PM
Edited Sep 12, 2011 at 11:03 PM

After I shut down my laptop it hit me.
I need to use JObject in my ServiceOperation instead of JsonValue.
Then the workaround described above will not be needed.

 

*** EDIT ***

Actually, now that I've tried it, using JObject nor JValue as an argument does not work.
I am still using the workaround described in the question.
I will continue the discussion in the following thread:
http://wcf.codeplex.com/discussions/271575