Quantcast
Channel: Json.NET
Viewing all articles
Browse latest Browse all 1767

Commented Unassigned: Need public methods or a way to honor serializer settings for writing type information in JsonConverter.WriteJson method [24925]

$
0
0
I need to make sure type information is included in an object serialized via a custom JsonConverter.

The WriteJson method receives a JsonWriter and a JsonSerializer as parameters, but I cannot figure out how I can ensure the "$type" member is written. I think this should be done automatially based on the serializer's settings, but there is no apparent way to do so.

I could write the property manually using JObject's WritePropertyName and WriteValue methods, but in that case I'd have to check the serializer's settings myself, and furthermore I'd have to process other settings such as the "Simple" assembly format option and strip the version info off the assembly myself after calling Binder.BindToName.

If you were to make the ReflectionUtils class public rather than internal, I could call its GetTypeName method which shortens the assembly name based on the assembly format passed to it. As it stands, I have no way to ensuring the serializer's settings for embedding type information are honored in a custom JsonConverter, unless there is some way to construct a JObject that involves using the serializer settings before writing it to the JsonWriter.
Comments: ** Comment from web user: triynko **

Documentation confirms this issue: "Note that while a JsonConverter gives you complete control over that values JSON, many Json.NET serialization features are no longer available like type name and reference handling." - http://james.newtonking.com/json/help/index.html

I will look into manually building a JsonContract for every type that needs special serialization.


Viewing all articles
Browse latest Browse all 1767

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>