Wally_the_Walrus
Wally_the_Walrus

Reputation: 219

json deserialization error

Im getting this error while trying to deserialize JSON string to object :

    "System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> Newtonsoft.Json.JsonSerializationException: Error converting value "{"Admin":false,"Id":1,"Password":"heslo","Nick":"jozifek"}" to type 'Entities.dbo.User'. Path 'Frontman', line 1, position 105. ---> System.ArgumentException: Could not cast or convert from System.String to Entities.dbo.User.
   at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
   at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   --- End of inner exception stack trace ---
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type)
   at BussinessLogic.helpers.Serializer.Deserialize[T](String obj) in C:\Users\Ondra\documents\visual studio 2010\Projects\webServiceTestApp\BussinessLogic\helpers\Serializer.cs:line 14
   at webServiceTestApp.Service1.GetSongs(String band) in C:\Users\Ondra\documents\visual studio 2010\Projects\webServiceTestApp\webServiceTestApp\Service1.asmx.cs:line 142
   --- End of inner exception stack trace ---"

User.cs looks like this:

namespace Entities.dbo
{
    [TableName("tbl_user")]
    public class User : AbstractEntity
    {
        [MapField("nick")]
        public string Nick { get; set; }

        [MapField("password")]
        public string Password { get; set; }

        [MapField("admin")]
        public bool Admin { get; set; }
    }
}

and Band.cs looks like this:

namespace Entities.dbo
{
    [TableName("tbl_band")]
    public class Band : AbstractEntity
    {
        [MapField("name")]
        public string Name { get; set; }

        [MapField("frontman")]
        public int FrontmanId { get; set; }

        [Association(CanBeNull = false, ThisKey = "FrontmanId", OtherKey = "Id")]
        public User Frontman { get; set; }

AbstractEntity:

namespace Entities.helpers
{
    public abstract class AbstractEntity
    {
        [PrimaryKey, Identity, MapField("id")]
        public int Id { get; set; }

        public string Serialize()
        {
            return JsonConvert.SerializeObject(this);
        }
    }
}

and this error pops out when im sending string to deserialize as Band containing User object in Frontman property..

any idea where I went wrong?

thanks :)

Upvotes: 1

Views: 2605

Answers (1)

its hard to say, i have use jonson once few years ago, but

  1. in jonson you have property "Password" but in object you have MapField("password") - in lower case

  2. Is in AbstractEntity class "Id" property?

Upvotes: 1

Related Questions