Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

Error for enumerable when using JsonConvert.SerializeObject

I am getting error, when i try to convert object into json using json.net.

Error:

  Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type
 'MyNamespace.Domain.Entity'.

Class to be serialized:

[Serializable]
public class Business:Entity
    {
        public virtual string TemplateName { get; set; }

        public virtual CalculationBasis CalculationBasis { get; set; }

        public virtual PeriodSelectionType PeriodSelectionType { get; set; }

        public virtual DateTime PeriodEndDate { get; set; }

        public virtual IEnumerable<int> mainKeys { get; set; }
  }

Serialization Code:

JsonSerializerSettings settings = new JsonSerializerSettings();
    settings.NullValueHandling = NullValueHandling.Ignore;
    settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
    var strJson = JsonConvert.SerializeObject( ObjectOfBusiness, settings);

Deserialization Code:

JsonConvert.DeserializeObject<Business>(ObjectOfBusiness, settings);

I am only getting this error, when i have values in IEnumerable<int> mainKeys

NOTE: mainKeys is a List<int>

Looks like error is because of its parent class "Entity" , the class is like this:

  [Serializable]
    public abstract class Entity
    {
        public Entity()
        {
        }

        public Entity(int id)
        {
            this.Id = id;
        }

        public virtual int Id { get; set; }

        public override bool Equals(object obj)
        {
            Entity other = (Entity)obj;
            return this.Id == other.Id;
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
    }

Don't know why json.net is trying to convert IEnumerable into "entity" type (its parent class)..

I can't remove entity (parent class) as its been used from lots of places..

Please suggest.

Thanks

Upvotes: 1

Views: 3423

Answers (2)

Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

Finally found the solution. the issue is because of poor code in override bool Equals(object obj) method. The correct code is:

  public override bool Equals(object obj)
        {
            if (obj is Entity)
            {
                Entity other = (Entity) obj;
                return this.Id == other.Id;
            }
            else
            {
                return false;
            }
        }

http://json.codeplex.com/workitem/16554

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062905

This isn't really an answer - it is a "this works fine":

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
public class Entity { }
public class CalculationBasis { }
public class PeriodSelectionType { }
[Serializable]
public class Business : Entity
{
    public virtual string TemplateName { get; set; }
    public virtual CalculationBasis CalculationBasis { get; set; }
    public virtual PeriodSelectionType PeriodSelectionType { get; set; }
    public virtual DateTime PeriodEndDate { get; set; }
    public virtual IEnumerable<int> mainKeys { get; set; }
}
class program
{
    static void Main()
    {
        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.NullValueHandling = NullValueHandling.Ignore;
        settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;

        var ObjectOfBusiness = new Business
        {
            TemplateName = "abc",
            CalculationBasis = new CalculationBasis(),
            PeriodSelectionType = new PeriodSelectionType(),
            PeriodEndDate = new DateTime(),
            mainKeys = new int[] { 1, 2, 3, 4, 5 }
        };
        var strJson = JsonConvert.SerializeObject(ObjectOfBusiness, settings);
        //...
        var obj = JsonConvert.DeserializeObject<Business>(strJson, settings);
        // ^^^^ all good
    }
}

So: it would be really good if you could show a failing case, or give us more clues to go on. For info, the JSON in the above is:

{"TemplateName":"abc","CalculationBasis":{},"PeriodSelectionType":{},"PeriodEndDate":"\/Date(-62135596800000)\/","mainKeys":[1,2,3,4,5]}

Upvotes: 0

Related Questions