joym8
joym8

Reputation: 4212

What am I doing wrong with JSON.NET's JsonConvert

json string

{
  "success": true,
  "challenge_ts": "2016-11-03T17:30:00Z",
  "hostname": "mydomain.com"
}

class

internal class reCaptchaResponse
{
    internal bool success { get; set; }
    internal DateTime challenge_ts { get; set; }  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
    internal string hostname { get; set; }        // the hostname of the site where the reCAPTCHA was solved
    internal string[] error_codes { get; set; }   // optional
}

attempt to serialize

reCaptchaResponse responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<reCaptchaResponse>(jsonResult);

attempt fails like...

Newtonsoft.Json.JsonConvert.SerializeObject(responseObject) returns {}

Upvotes: 1

Views: 674

Answers (4)

L.B
L.B

Reputation: 116108

Json.Net, by default, only serializes/deserialzes public fileds and properties, but you can also do it without changing access modifiers from internal to public.

Just use JsonProperty attribute

internal class reCaptchaResponse
{
    [JsonProperty]
    internal bool success { get; set; }
    [JsonProperty]
    internal DateTime challenge_ts { get; set; }  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
    [JsonProperty]
    internal string hostname { get; set; }        // the hostname of the site where the reCAPTCHA was solved
    [JsonProperty]
    internal string[] error_codes { get; set; }   // optional
}

(Without modifing the original class) You can even use ContractResolver to select which properties/fields should be used in serialization process

EDIT

Although this answer has already been accepted, I want to post a code where the original assembly can not be modified.

var settings = new JsonSerializerSettings() { 
                   ContractResolver = new AllPropertiesContractResolver() 
               };
reCaptchaResponse responseObject = 
                JsonConvert.DeserializeObject<reCaptchaResponse>(jsonResult ,settings);

public class AllPropertiesContractResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
                    .Select(x => new Newtonsoft.Json.Serialization.JsonProperty()
                    {
                        PropertyName = x.Name,
                        PropertyType = x.PropertyType,
                        Readable = true,
                        ValueProvider = new AllPropertiesValueProvider(x),
                        Writable = true
                    })
                    .ToList();

        return props;
    }
}

public class AllPropertiesValueProvider : Newtonsoft.Json.Serialization.IValueProvider
{
    PropertyInfo _propertyInfo;

    public AllPropertiesValueProvider(PropertyInfo p)
    {
        _propertyInfo = p;
    }

    public object GetValue(object target)
    {
        return _propertyInfo.GetValue(target);  //Serialization
    }

    public void SetValue(object target, object value)
    {
        _propertyInfo.SetValue(target, value, null); //Deserialization
    }
}

Upvotes: 5

Gilad Green
Gilad Green

Reputation: 37299

Change the properties to public. By default it does not deserialize non-public properties

internal class reCaptchaResponse
{
    public bool success { get; set; }
    public DateTime challenge_ts { get; set; }  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
    public string hostname { get; set; }        // the hostname of the site where the reCAPTCHA was solved
    public string[] error_codes { get; set; }   // optional
}

Upvotes: 3

Mixim
Mixim

Reputation: 972

You can try to change modificator from internal to public

Upvotes: 0

pm100
pm100

Reputation: 50140

the members must be public

internal class reCaptchaResponse
{
    public bool success { get; set; }
public DateTime challenge_ts { get; set; }  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
    public string hostname { get; set; }        // the hostname of the site where the reCAPTCHA was solved
    public  string[] error_codes { get; set; }   // optional
}

Upvotes: 1

Related Questions