views:

181

answers:

1

I'm trying to use RestSharp (http://restsharp.org/) in a Windows Phone 7 project, but I'm having an issue it seems with the Newtonsoft Json.NET library that RestSharp uses. When I'm trying to execute my code like so:

_restClient.ExecuteAsync<Model.Song>(restRequest, (response) =>
{
    if (response.StatusCode == HttpStatusCode.OK) { }
    else { }
});

I'm getting the following error:

Could not load type 'Newtonsoft.Json.Linq.JArray' from assembly 'Newtonsoft.Json.Compact, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30AD4FE6B2A6AEED'.

Newtonsoft.Json.Compact.dll is copied to the Bin folder of my Windows Phone 7 application, so I'm assuming it gets deployed to the device, but somehow it won't load it. Has anyone experienced/solved something similar? Thanks.


As requested, an example of the JSON: [{"type":"Song","id":60097,"title":"A Place Where You Belong","artist":{"type":"Artist","id":17,"nameWithoutThePrefix":"Bullet For My Valentine","useThePrefix":false}}]

And the classes:

[DataContract]
public class Song
{
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "title")]
    public string Title { get; set; }

    [DataMember(Name = "artist")]
    public Artist Artist { get; set; }
}

[DataContract]
public class Artist
{
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "nameWithoutThePrefix")]
    public string Name { get; set; }

    [DataMember(Name = "useThePrefix")]
    public bool UsePrefix { get; set; }
}
+2  A: 

You don't need any of the [DataMember] attributes, they're not used by RestSharp.

Since the JSON returned is an array, you need to deserialize that to an array:

client.ExecuteAsync<List<Song>>(request, callback);
John Sheehan
That doesn't seem to do the trick either. I'm still getting the same error with `List<Song>`: `"Could not load type 'Newtonsoft.Json.Linq.JArray' from assembly 'Newtonsoft.Json.Compact, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30AD4FE6B2A6AEED'."`Could it be that since RestSharp doesn't use the `[DataMember]` attributes, that it doesn't translate from the JSON id's to my own Property names? Ie. `title->Title`, `nameWithoutThePrefix->Name`, thus causing the error?
RajenK
The names are translated automatically http://github.com/johnsheehan/RestSharp/wiki/DeserializationCan you try it in a non-WP7 project and tell me if it works?
John Sheehan
Let me see if I understand correctly: does my Class need to have the exact naming as the JSON properties, or does it use `[DataMember]` properties to link the names from the JSON to my class property names? I'll try in a non-WP7 project and will let you know if it works.
RajenK
RestSharp does automatic name matching. For instance for the 'nameWithoutThePrefix' element, your property would need to be named NameWithoutThePrefix. The link I sent over early shows all the steps it goes through to match the name.
John Sheehan
So I tried it in a Silverlight project and got an error (with the exact same code) in `RestSharp.cs` at the `GetHandler` method: `NullReferenceException` at `var semicolonIndex = contentType.indexOf(';');`. `contentType` seems to be `null` here. I also tried it in a Windows Forms application with the same code, which does seem to fill my `response.Data` property with the correct data, so I'm assuming right now it's an issue with the Silverlight/Windows Phone components of RestSharp.
RajenK
We'll have to work this out either in the new Google Group http://groups.google.com/group/RestSharp or email [email protected]
John Sheehan