json string
{
\"success\": true,
\"challenge_ts\": \"2016-11-03T17:30:00Z\",
\"hostname\": \"mydomain.com\"
}
class
inter
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
}
You can try to change modificator from internal to public
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
}
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
}
}