将对象序列化为 JSON 时 DataContract 框架不起作用
DataContract framework not working when serializing objects to JSON
这是我的模型:
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;
}
}
这是我的程序:
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;
}
当对象 jsonParser
初始化时,它似乎根本无法识别我的模型。
这导致空 MemoryStream()
。
请帮忙。
P.S。我在我的程序中删除了异常处理,因为它会分散注意力。谢谢。
此外,目前对象 poco
始终被假定为我模型中的一种类型。
您需要通过重置其 Position
将流倒回到开头,然后才能从中读取,例如:
public static string ToJson<T>(T obj, DataContractJsonSerializer serializer = null)
{
serializer = serializer ?? new DataContractJsonSerializer(obj == null ? typeof(T) : obj.GetType());
using (var memory = new MemoryStream())
{
serializer.WriteObject(memory, obj);
memory.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(memory))
{
return reader.ReadToEnd();
}
}
}
这是我的模型:
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;
}
}
这是我的程序:
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;
}
当对象 jsonParser
初始化时,它似乎根本无法识别我的模型。
这导致空 MemoryStream()
。
请帮忙。
P.S。我在我的程序中删除了异常处理,因为它会分散注意力。谢谢。
此外,目前对象 poco
始终被假定为我模型中的一种类型。
您需要通过重置其 Position
将流倒回到开头,然后才能从中读取,例如:
public static string ToJson<T>(T obj, DataContractJsonSerializer serializer = null)
{
serializer = serializer ?? new DataContractJsonSerializer(obj == null ? typeof(T) : obj.GetType());
using (var memory = new MemoryStream())
{
serializer.WriteObject(memory, obj);
memory.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(memory))
{
return reader.ReadToEnd();
}
}
}