How can I know if a type is convertible using Json.NET?
I am trying to write an interface for a collection that internally stores
data as a JObject
internal class JsonDataSet : IDataSet
{
private JObject Document { get; set; }
// The following methods are from the IDataSet interface
public int Count { ... }
public void Add<T>(string key, T value) { ... }
public T GetItem<T>(string key) { ... }
public bool ContainsKey(string key) { ... }
}
In the Add<T> method I want to provide a useful exception if a custom type
does not have the DataContract annotation. For example, if someone calls:
dataSet.Add<IDictionary<string, IList<CustomType>>>(dict);
it will throw an exception "Cannot serialize type 'CustomType'.
DataContract annotations not found." if CustomType does not have the
proper annotation.
So far I have found a way to get every generic argument in the type
definition so I can check them:
private IEnumerable<Type> GetGenericArgumentsRecursively(Type type)
{
if (!type.IsGenericType) yield return type;
foreach (var genericArg in type.GetGenericArguments())
foreach (var yieldType in
GetGenericArgumentsRecursively(genericArg ))
yield return yieldType;
}
and tried implementing the add method like this:
public void Add<T>(string key, T value)
{
foreach(var type in GetGenericArgumentsRecursively(typeof(T)))
{
if(!type.IsPrimitive && !Attribute.IsDefined(type,
typeof(DataContractAttribute)))
throw new Exception("Cannot serialize type '{0}'. DataContract
annotations not found.", typeof(T));
}
Document.Add(new JProperty(key,
JToken.Parse(JsonConvert.SerializeObject(value))));
}
I think this will work for primitive types and custom types but not for
non-generic .NET types since they don't all have DataContract annotations.
Is there a way to know which types can be serialized by JsonConvert?
No comments:
Post a Comment