0

I have a class as,

public class Parameters
{
   public int Id {get;set;}
   public string FilterParam {get;set;}
   public string NameParam {get;set;}
   public IList<int> CollectionsIds {get;set;}
   public DataTable ParamTable {get;set;}
}

When I try to serialize to JSON I get circular reference error "A circular reference was detected when serializing the object".

My serializer code is:

JSONSerializer.SerializeObject<Parameters>(parameters)

private static JavaScriptSerializer _jsonSerializer = null;
private static JavaScriptSerializer JSONSerializer
{
    get
    {
        if (_jsonSerializer == null)
        {
            _jsonSerializer = new JavaScriptSerializer();
            _jsonSerializer.MaxJsonLength = 999999999;
        }
        return _jsonSerializer;
    }
}

public static string SerializeObject<T>(T obj)
{
    return JSONSerializer.Serialize(obj);
}

How do I serialize class into JSON format if the class contains dataTable as well?

Note: if I set dataTable to null it works fine but whenever I try to populate dataTable and call method to serialize it fails.

4
  • I thin Paramtable has same field from Parameters class. look at this stackoverflow.com/questions/17818386/… Commented Sep 12, 2014 at 10:10
  • Would ignoring DataTable be enough for you? Commented Sep 12, 2014 at 10:13
  • @Tolga No I need to store Datatable to JSON as well along with Parameters class. In a worst case scenario probably I need to do workaround by creating class corresponding to DataTable. But I'm trying to get a solution for the existing class structure. Commented Sep 12, 2014 at 10:14
  • A V2Solutions, No its DataTable and name is completely different. I meant the column name of datatable is different from property declared in class parameters. Commented Sep 12, 2014 at 10:15

2 Answers 2

1

Just add a field(list of dictionaries) to fake your DataTable to your class. And ignore the DataTable member. Then you can serialize an object of this class as you needed.

public class Parameters
{
    public int Id {get;set;}
    public string FilterParam {get;set;}
    public string NameParam {get;set;}
    public IList<int> CollectionsIds {get;set;}
    [ScriptIgnore]
    public DataTable ParamTable {get;set;}

    public List<Dictionary<string, object>> _fakeParamTable
    {
        get
        {
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;

            foreach ( DataRow dr in ParamTable .Rows )
            {
                row = new Dictionary<string, object>();
                foreach ( DataColumn col in dt.Columns )
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            return rows;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use this library: https://www.nuget.org/packages/newtonsoft.json/

This JSON:

{
 Parameters :
 {
   id: 1,
   FilterParam: "2",
   NameParam: "2",
   CollectionsIds: [1,2,3,1],
   ParamTable: {id:1,name:2,code:"3"}
 }
}

Will have classes (http://json2csharp.com/):

p

ublic class ParamTable
{
    public int id { get; set; }
    public int name { get; set; }
    public string code { get; set; }
}

public class Parameters
{
    public int id { get; set; }
    public string FilterParam { get; set; }
    public string NameParam { get; set; }
    public List<int> CollectionsIds { get; set; }
    public ParamTable ParamTable { get; set; }
}

public class RootObject
{
    public Parameters Parameters { get; set; }
}

So you will siple serialize, what you want like this:

JsonConvert.DeserializeObject<RootObject>(myJSON);

2 Comments

I'm aware of this approach. I have kept it as worst case scenario if there is absolutely no solution to fix dataTable to JSON. Thats my last hope to convert dataTable to meaningfull class and then call serialzer code.
Just use SerializationBinder then msdn.microsoft.com/en-us/library/ffas09b2 realization for JSON you will find there: james.newtonking.com/json/help/index.html?topic=html/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.