I use DataContractJsonSerializerto deserialize objects from an external service. In most cases, it worked fine for me. However, there is one case where I need to deserialize JSON, which contains a list of objects that all inherit from the same base class, but there are many different types of objects in this list.
I know this can be done easily by including a list of known types in the serializer constructor, but I do not have access to the code that generated this JSON service. The types that I use will be different from the types used in the service (basically just the class name and namespace will be different). In other words, the classes that the data was associated with will not be the same classes that I will use for deserialization, even if they are very similar.
With XML, DataContractSerializerI can pass DataContractResolverfor mapping service types to my own types, but there is no such constructor DataContractJsonSerializer. Is there any way to do this? The only options I could find are: write my own deserializer or use Microsoft JsonObject , which is not tested, and "should not be used in production environments."
Here is an example:
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
}
[DataContract]
public class Student : Person
{
[DataMember]
public int StudentId { get; set; }
}
class Program
{
static void Main(string[] args)
{
var jsonStr = "[{\"__type\":\"Student:#UnknownProject\",\"Name\":\"John Smith\",\"StudentId\":1},{\"Name\":\"James Adams\"}]";
using (var stream = new MemoryStream())
{
var writer = new StreamWriter(stream);
writer.Write(jsonStr);
writer.Flush();
stream.Position = 0;
var s = new DataContractJsonSerializer(typeof(List<Person>), new Type[] { typeof(Student), typeof(Person) });
var personList = (List<Person>)s.ReadObject(stream);
}
}
}
Here is the error mentioned in the comment above:
Element ':item' contains data from a type that maps to the name
'http://schemas.datacontract.org/2004/07/UnknownProject:Student'. The
deserializer has no knowledge of any type that maps to this name. Consider using
a DataContractResolver or add the type corresponding to 'Student' to the list of
known types - for example, by using the KnownTypeAttribute attribute or by adding
it to the list of known types passed to DataContractSerializer.
source
share