Nancy.Serialization.JsonNet icon indicating copy to clipboard operation
Nancy.Serialization.JsonNet copied to clipboard

Unable to deserialize to Newtonsoft.Json.Linq.JToken

Open gimmi opened this issue 9 years ago • 2 comments

Hi,

given the following dto

public class Dto
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

this code work as expected, receiving application/json body and responding with application/json

Post["/echo"] = arg => this.Bind<Dto>();

the same request return an empty json ({}) with this implementation

Post["/echo"] = arg => this.Bind<JObject>();

What's wrong with this endpoint? Isn't deserialization to JToken working?

gimmi avatar Jan 11 '17 17:01 gimmi

Seems that JObject deserialization work as expected for inner field like these:

public class Dto
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public JObject Props { get; set; }
}

gimmi avatar Jan 11 '17 17:01 gimmi

Currently the exception is the following: Message of InvalidDataContractException: Type 'Newtonsoft.Json.Linq.JToken' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.

techaimail avatar Feb 05 '18 17:02 techaimail