NewtonSoft.Json.Linq Namespace Entities not marked as [Serializable]?
description
I've got a caching mechanism that's using the DataContractSerializer as a best-effort serializer for anything the cache receives. The problem is the JObject (and it's children) aren't marked as [Serializable] and cannot be serialized by this method.
I know the library is a serialization framework in itself, but It's a bit upsetting that it doesn't play nice with other serializers.
Here's the stack trace into the serializer when a JObject is attempted to be serialized:
System.Runtime.Serialization.InvalidDataContractException: Type 'Newtonsoft.Json.Linq.JObject' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
at System.Runtime.Serialization.CollectionDataContract.GetValidContract(SerializationMode mode)
at System.Runtime.Serialization.NetDataContractSerializer.GetDataContract(RuntimeTypeHandle typeHandle, Type type, Hashtable& surrogateDataContracts)
at System.Runtime.Serialization.NetDataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(Stream stream, Object graph)
Marking the object with [Serializable] and potentially ISerializable (to hook the proper serialization methods in the library) should allow it to be serialized properly.