viagogo / HalKit

A lightweight .NET library for creating and consuming HAL hypermedia APIs
MIT License
16 stars 8 forks source link

Accessing properties #20

Closed PaulBullivant closed 8 years ago

PaulBullivant commented 8 years ago

Hello,

Your HalKit is really simple and easy to use. Thanks!

I am unable to extract the properties being returned by the API

E.g. When I call the API root, there is a property in the returned JSON called 'message'

I have tried adding a property to my RootResource class: public string message { get; set; } but it is never populated after my call to GetRootAsync<RootResource>();

All my links work ok. What am I doing wrong?

public class RootResource : Resource
{
        public string message { get; set; }

        [Rel("fx:user")]
        public Link User { get; set; }

        [Rel("fx:store")]
        public Link Store { get; set; }

        [Rel("fx:stores")]
        public Link Stores { get; set; }

        [Rel("fx:reporting")]
        public Link Reporting { get; set; }

        [Rel("fx:token")]
        public Link Token { get; set; }
}
PaulBullivant commented 8 years ago

I have resolved this by adding the NewtonSoft JsonProperty attribute to the property. The deserialisation now works perfectly.

It wasn't clear that you were using NewtonSoft for your deserialisation and that I could use NewtonSoft attributes on my Resource classes. Would you be able to update the 'ReadMe'?

public class RootResource : Resource
{
        [JsonProperty(PropertyName="message")]
        public string Message { get; set; }

        [Rel("fx:user")]
        public Link User { get; set; }

        [Rel("fx:store")]
        public Link Store { get; set; }

        [Rel("fx:stores")]
        public Link Stores { get; set; }

        [Rel("fx:reporting")]
        public Link Reporting { get; set; }

        [Rel("fx:token")]
        public Link Token { get; set; }
}
akilb commented 8 years ago

Thanks for the feedback @PaulBullivant! We'll get the README updated.

Just FYI you could use the System.Runtime.Serialization attributes so that your models don't have to take a direct dependency on Newtonsoft:

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

With these attributes your model can be (de)serialized correctly by lots of different serializer libraries

PaulBullivant commented 8 years ago

That's a good tip! Thanks @akilb