Restsharp, MonoDroid, and the '@ symbol' issue

briannelsonbriannelson USMember
edited April 2013 in Xamarin.Android

I have a third party JSON rest API i am accessing data with a monodroid project. I am using RestSharp to do the automatic deserialization. This has worked fine with other API's, but this one has the @ResourceName for the many of the default names of the resources. example response below

{
    "array": {
        "@total_resources": 2,
        "@pages": 1,
        "@page": 1,
        "@per_page": 20,
        "@url": "https://URL_I_Cant_Show",
        "@type": "Array",
        "resources": [
            {
                "@type": "TestCollection",
                "@url": "https://URL_I_Cant_Show",
                "@collection_url": "https://URL_I_Cant_Show",
                "@version": 1,
                "@trash": false,
                "@created_at": "2013-01-24T21:34:53Z",
                "@updated_at": "2013-01-24T21:34:53Z",
                "name": "Billy Joe Bob",
                "short_name": "Billy"
            },
            {
                "@type": "TestCollection",
                "@url": "https://URL_I_Cant_Show",
                "@collection_url": "https://URL_I_Cant_Show",
                "@version": 1,
                "@trash": false,
                "@created_at": "2013-04-27T05:37:36Z",
                "@updated_at": "2013-04-27T05:37:36Z",
                "name": "Phil",
                "short_name": "Last Person"
            }
        ]
    }
}

Essentially the only attributes i added to this resource are

            "name": "Phil",
            "short_name": "Last Person"

This is StorageRoomApp, if that helps. If i run this through Json2cSharp I get This.
Which doesn't work. It seems to come down to the @symbol added before the resource name.

public class Resource
{
    public string [email protected] { get; set; }
    public string [email protected] { get; set; }
    public string [email protected]_url { get; set; }
    public int [email protected] { get; set; }
    public bool [email protected] { get; set; }
    public string [email protected]_at { get; set; }
    public string [email protected]_at { get; set; }
    public string name { get; set; }
    public string short_name { get; set; }
}

public class Array
{
    public int [email protected]_resources { get; set; }
    public int [email protected] { get; set; }
    public int [email protected] { get; set; }
    public int [email protected]_page { get; set; }
    public string [email protected] { get; set; }
    public string [email protected] { get; set; }
    public List<Resource> resources { get; set; }
}

public class RootObject
{
    public Array array { get; set; }
}

I also Tried Adding DataContract

    [DataContract]
    public class Resource
    {
        [DataMember(Name="@type")]
        public string Type { get; set; }

        [DataMember(Name="@url")]
        public string Url { get; set; }

        [DataMember(Name="@collection_url")]
        public string Collection_url { get; set; }

        [DataMember(Name="@version")]
        public int Version { get; set; }

        [DataMember(Name="@trash")]
        public bool Trash { get; set; }

        [DataMember(Name="@created_at")]
        public string Created_at { get; set; }

        [DataMember(Name="@updated_at")]
        public string Updated_at { get; set; }

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

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

    [DataContract]
    public class Array
    {
        [DataMember(Name="@total_resources")]
        public int Total_resources { get; set; }

        [DataMember(Name="@pages")]
        public int Pages { get; set; }

        [DataMember(Name="@page")]
        public int Page { get; set; }

        [DataMember(Name="@per_page")]
        public int Per_page { get; set; }

        [DataMember(Name="@url")]
        public string Url { get; set; }

        [DataMember(Name="@type")]
        public string Type { get; set; }

        [DataMember(Name="@resources")]
        public List<Resource> Resources { get; set; }
    }

    [DataContract]
    public class RootObject
    {
        public List<Resource> Resources { get; set; }
        //public Array array { get; set; }
    }

Here is the Request

public void getRESTTest (string url, string resourceDir, string responseType, string rootElement)
{
    // Rest Request and deserialize
    var client = new RestClient (url);      // Base Url
    var request = new RestRequest();
    request.RootElement = rootElement;
    //request.RequestFormat = Method.GET;

    request.OnBeforeDeserialization = resp => {
        //resp.Content = resp.Content.Replace("@","sa");
        resp.ContentType = responseType; 
    };  // Set Type

    // Get It Async
    client.ExecuteAsync<List<JsonTestApi.Resource>> (request, response =>  {

        if (!string.IsNullOrWhiteSpace (response.Content) || response.StatusCode == System.Net.HttpStatusCode.OK) {

            foreach(JsonTestApi.Resource r in response.Data)
            {

            Console.WriteLine(r.Name);
            Console.WriteLine(r.Short_name);
            }
        }
    });
}

One Last Thing i tried was changing the symbol '@' to 'sa' in restsharp, commented out in the
code below

Sign In or Register to comment.