Here is my model:
namespace RESTalm
{
[DataContract]
[KnownType(typeof(Entity))]
[KnownType(typeof(Field))]
[KnownType(typeof(Value))]
public class Entities
{
[DataMember(IsRequired = true)]
public List<Entity> entities;
[DataMember(IsRequired = true)]
public int TotalResults;
}
[DataContract]
[KnownType(typeof(Field))]
[KnownType(typeof(Value))]
public class Entity
{
[DataMember(IsRequired = true)]
public Field[] Fields;
[DataMember(IsRequired = true)]
public String Type;
}
[DataContract]
[KnownType(typeof(Value))]
public class Field
{
[DataMember(IsRequired = true)]
public String Name;
[DataMember(IsRequired = true)]
public Value[] values;
}
[DataContract]
[KnownType(typeof(Value))]
public class Value
{
[DataMember(IsRequired = true)]
public String value;
}
}
Here is my program:
private String toJSON(Object poco)
{
String json;
DataContractJsonSerializer jsonParser = new DataContractJsonSerializer(poco.GetType());
MemoryStream buffer = new MemoryStream();
jsonParser.WriteObject(buffer, poco);
StreamReader reader = new StreamReader(buffer);
json = reader.ReadToEnd();
reader.Close();
buffer.Close();
return json;
}
When the object jsonParser initializes it doesn't seem to recognize my model at all.
This leads to an empty MemoryStream().
Please help.
P.S. I have cut-out exception-handling in my program because it's distracting. Thanks.
Also, for the moment the object poco is always assumed to be a type in my model.
Positionbefore you can read from it.