I am having a problem trying to deserialize a JSON using my classes. I create a JSON retrun a my custom type in my WEN API controller (gert method) and it is creating the JSON weel however when I am trying to desearilze I have an error in bold bellow.
I am following the pattern from here to create my object (http://www.dofactory.com/Patterns/PatternComposite.aspx#_self1) which then in my model I create a list of this objects and that final object(model) is what I expose in my get as JSON.
I just try the same example with the classes on the link and it works properly so I dont have club. Thank you very much for the help in advance
This part of my code
....
Controller
public ValuesModel Get(int id, int displayFieldID)
{
ValuesModel dataValue = new ValuesModel(id, displayFieldID);
#region Methods
public abstract void Add(FilterControlValuesParent filterCrtlValueParent);
public abstract void Remove(FilterControlValuesParent filterCrtlValueParent);
public abstract void Get(int depth);
public abstract void Get();
#endregion
}
public class FilterControlValuesChild: FilterControlValuesParent
{
#region Properties
public List<FilterControlValuesParent> filterControlValueChild = new List<FilterControlValuesParent>();
#endregion
#region Constructors
public FilterControlValuesChild()
: base()
{
public override void Add(FilterControlValuesParent filterCrtlValueParent)
{
filterControlValueChild.Add(filterCrtlValueParent);
}
public override void Remove(FilterControlValuesParent filterCrtlValueParent)
{
filterControlValueChild.Remove(filterCrtlValueParent);
}
public override void Get(int depth)
{
//TODO: Work in this method
int itemp = 0;
itemp++;
string tempstring = itemp.ToString();
foreach (FilterControlValuesParent filterCrtlValueParent in filterControlValueChild)
{
filterCrtlValueParent.Get(depth + 2);
}
}
public override void Get()
{
throw new NotImplementedException();
}
#endregion
....
....
Could not create an instance of type .FilterControlValuesParent. Type is an interface or abstract class and cannot be instantated. Line 1, position 78.
sinedyip
Member
308 Points
154 Posts
Deserialization issue
Nov 29, 2012 09:52 PM|LINK
Hi Team,
I am having a problem trying to deserialize a JSON using my classes. I create a JSON retrun a my custom type in my WEN API controller (gert method) and it is creating the JSON weel however when I am trying to desearilze I have an error in bold bellow.
I am following the pattern from here to create my object (http://www.dofactory.com/Patterns/PatternComposite.aspx#_self1) which then in my model I create a list of this objects and that final object(model) is what I expose in my get as JSON.
I just try the same example with the classes on the link and it works properly so I dont have club. Thank you very much for the help in advance
This part of my code
....
Controller
public ValuesModel Get(int id, int displayFieldID)
{
ValuesModel dataValue = new ValuesModel(id, displayFieldID);
//-------------------------------------Test--------------------------------------------------
string json = JsonConvert.SerializeObject(ipFilterControlDataValue); //GOOD
ValuesModel deserializedValues = JsonConvert.DeserializeObject<ValuesModel>(json); //ERROR HERE
Models
public class ValuesModel:BaseModel
{
public List<FilterControlValuesChild> ListOfFilterValues = new List<FilterControlValuesChild>();
public ValuesModel(int scheduleID, int diplayFieldID)
{
DataTable dataTableValues = new DataTable();
dataTableValues = GetValuesByDisplayScheduleIDFieldIDTable(scheduleID, diplayFieldID);
ProcessValueTable(dataTableValues); // Update the object -> ListOfFilterValues
}
public IPFilterValuesModel()
{
ListOfFilterValues.Add(new FilterControlValuesChild(string.Empty, string.Empty, null));
}
My class Parent child relation
abstract public class FilterControlValuesParent
{
#region Properties
protected string valueID;
protected string displayValue;
protected Dictionary<string, string> extraControlProperties;
public string ValueID
{
get
{
return valueID;
}
set
{
valueID = value;
}
}
public string DisplayValue
{
get
{
return displayValue;
}
set
{
displayValue = value;
}
}
public Dictionary<string, string> ExtraControlProperties
{
get
{
return extraControlProperties;
}
set
{
extraControlProperties = value;
}
}
#endregion
#region Contructors
public FilterControlValuesParent()
{
this.valueID = string.Empty;
this.displayValue = string.Empty;
this.extraControlProperties = null;
}
public FilterControlValuesParent(string valueID, string displayValue, Dictionary<string, string> extraProperties)
{
this.valueID = valueID;
this.displayValue = displayValue;
this.extraControlProperties = extraProperties;
}
#endregion
#region Methods
public abstract void Add(FilterControlValuesParent filterCrtlValueParent);
public abstract void Remove(FilterControlValuesParent filterCrtlValueParent);
public abstract void Get(int depth);
public abstract void Get();
#endregion
}
public class FilterControlValuesChild: FilterControlValuesParent
{
#region Properties
public List<FilterControlValuesParent> filterControlValueChild = new List<FilterControlValuesParent>();
#endregion
#region Constructors
public FilterControlValuesChild()
: base()
{
}
public FilterControlValuesChild(string valueID, string displayValue, Dictionary<string, string> extraProperties)
: base(valueID, displayValue, extraProperties)
{
}
#endregion
#region Methods
public override void Add(FilterControlValuesParent filterCrtlValueParent)
{
filterControlValueChild.Add(filterCrtlValueParent);
}
public override void Remove(FilterControlValuesParent filterCrtlValueParent)
{
filterControlValueChild.Remove(filterCrtlValueParent);
}
public override void Get(int depth)
{
//TODO: Work in this method
int itemp = 0;
itemp++;
string tempstring = itemp.ToString();
foreach (FilterControlValuesParent filterCrtlValueParent in filterControlValueChild)
{
filterCrtlValueParent.Get(depth + 2);
}
}
public override void Get()
{
throw new NotImplementedException();
}
#endregion
....
....
Could not create an instance of type .FilterControlValuesParent. Type is an interface or abstract class and cannot be instantated. Line 1, position 78.
sinedyip
Member
308 Points
154 Posts
Re: Deserialization issue
Nov 30, 2012 03:38 PM|LINK
When I create a nested object is when I have the proble basaccly is when I am trying to desealize the
json like this:
{
"ListOfFilterValues": [{
"filterControlValueChild": [{
"filterControlValueChild": [],
"ValueID": "5",
"DisplayValue": "Orlando",
"ExtraControlProperties": null
}],
"ValueID": "4",
"DisplayValue": "FL",
"ExtraControlProperties": null
},
{
"filterControlValueChild": [],
"ValueID": "7",
"DisplayValue": "NY",
"ExtraControlProperties": null
},
{
"filterControlValueChild": [],
"ValueID": "8",
"DisplayValue": "WA",
"ExtraControlProperties": null
},
{
"filterControlValueChild": [],
"ValueID": "9",
"DisplayValue": "Tacoma",
"ExtraControlProperties": null
}]
}