The Json.Net `JsonSerializer` uses at least `JsonConverterAttribute`, `IConverter` implementations, and built-in and CLR conversions to determine which `JsonValue` matches a provided object. The lower-level `BsonWriter` accepts only a `JsonArray` or `JsonObject` at the top level of a document due to BSON format restrictions. When the discovered conversions result in something else (a `JsonPrimitive` or `null`), the `BsonWriter` throws a `JsonWriterException`.
Our `BsonMediaTypeFormatter` attempts to avoid the above exceptions using an `IsSimpleType()` method which is based on `TypeDescriptor` and that class' idea of whether a value may be converted to a `string`. This determination does not align with what Json.Net actually does (as summarized above). I haven't found a top-level type which will see the exception but these cases definitely exist.
My expectation is types with attributes `TypeDescriptor` understands but Json.Net does not will cause the BSON formatter to use its workaround when it's not necessary. The opposite case (Json.Net conversions `TypeDescriptor` isn't aware of) should lead to `JsonWriterException`.
Comments: Json.NET team recommendation is to use a contract resolver (get the contract resolver from the serializer, `Serializer.ContractResolver` is the property -- or something close to that name): ``` C# JsonContract contract = Serializer.ContractResolver.ResolveContract(type); return (contract is JsonPrimitiveContract); ``` And then check the current converters the serializer has in `Serializer.Converters`. Loop through those and check `CanConvert`. A converter also has `CanRead` and `CanWrite` properties so check `CanRead` is true when deserializing and `CanWrite` is true when serializing.
Our `BsonMediaTypeFormatter` attempts to avoid the above exceptions using an `IsSimpleType()` method which is based on `TypeDescriptor` and that class' idea of whether a value may be converted to a `string`. This determination does not align with what Json.Net actually does (as summarized above). I haven't found a top-level type which will see the exception but these cases definitely exist.
My expectation is types with attributes `TypeDescriptor` understands but Json.Net does not will cause the BSON formatter to use its workaround when it's not necessary. The opposite case (Json.Net conversions `TypeDescriptor` isn't aware of) should lead to `JsonWriterException`.
Comments: Json.NET team recommendation is to use a contract resolver (get the contract resolver from the serializer, `Serializer.ContractResolver` is the property -- or something close to that name): ``` C# JsonContract contract = Serializer.ContractResolver.ResolveContract(type); return (contract is JsonPrimitiveContract); ``` And then check the current converters the serializer has in `Serializer.Converters`. Loop through those and check `CanConvert`. A converter also has `CanRead` and `CanWrite` properties so check `CanRead` is true when deserializing and `CanWrite` is true when serializing.