Registering a custom JsonConverter globally in Json.Net
Yes, this is possible using Json.Net 5.0.5 or later. See JsonConvert.DefaultSettings
.
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Converters = new List<JsonConverter> { new SomeConverter() }
};
// Later on...
string json = JsonConvert.SerializeObject(someObject); // this will use SomeConverter
If you're using Web API, you can set up a converter globally like this instead:
var config = GlobalConfiguration.Configuration;
var jsonSettings = config.Formatters.JsonFormatter.SerializerSettings;
jsonSettings.Converters.Add(new SomeConverter());
How to implement custom JsonConverter in JSON.NET?
Using the standard CustomCreationConverter
, I was struggling to work how to generate the correct type (Person
or Employee
), because in order to determine this you need to analyse the JSON and there is no built in way to do this using the Create
method.
I found a discussion thread pertaining to type conversion and it turned out to provide the answer. Here is a link: Type converting (archived link).
What's required is to subclass JsonConverter
, overriding the ReadJson
method and creating a new abstract Create
method which accepts a JObject
.
The JObject class provides a means to load a JSON object and
provides access to the data within this object.
The overridden ReadJson
method creates a JObject
and invokes the Create
method (implemented by our derived converter class), passing in the JObject
instance.
This JObject
instance can then be analysed to determine the correct type by checking existence of certain fields.
Example
string json = "[{
\"Department\": \"Department1\",
\"JobTitle\": \"JobTitle1\",
\"FirstName\": \"FirstName1\",
\"LastName\": \"LastName1\"
},{
\"Department\": \"Department2\",
\"JobTitle\": \"JobTitle2\",
\"FirstName\": \"FirstName2\",
\"LastName\": \"LastName2\"
},
{\"Skill\": \"Painter\",
\"FirstName\": \"FirstName3\",
\"LastName\": \"LastName3\"
}]";
List<Person> persons =
JsonConvert.DeserializeObject<List<Person>>(json, new PersonConverter());
...
public class PersonConverter : JsonCreationConverter<Person>
{
protected override Person Create(Type objectType, JObject jObject)
{
if (FieldExists("Skill", jObject))
{
return new Artist();
}
else if (FieldExists("Department", jObject))
{
return new Employee();
}
else
{
return new Person();
}
}
private bool FieldExists(string fieldName, JObject jObject)
{
return jObject[fieldName] != null;
}
}
public abstract class JsonCreationConverter<T> : JsonConverter
{
/// <summary>
/// Create an instance of objectType, based properties in the JSON object
/// </summary>
/// <param name="objectType">type of object expected</param>
/// <param name="jObject">
/// contents of JSON object that will be deserialized
/// </param>
/// <returns></returns>
protected abstract T Create(Type objectType, JObject jObject);
public override bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}
public override bool CanWrite
{
get { return false; }
}
public override object ReadJson(JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
// Load JObject from stream
JObject jObject = JObject.Load(reader);
// Create target object based on JObject
T target = Create(objectType, jObject);
// Populate the object properties
serializer.Populate(jObject.CreateReader(), target);
return target;
}
}
Globally use a JsonConverter on a class without the attribute
Yes, you can use a custom IContractResolver
to programmatically apply a JsonConverter
to a class or property. The simplest way to do this is to derive your resolver from the DefaultContractResolver
class and then override the appropriate method. Below is an example resolver that instructs Json.Net to use an ObjectIdConverter
on all instances of the ObjectId
type, regardless of what class they might appear in.
class CustomResolver : DefaultContractResolver
{
protected override JsonObjectContract CreateObjectContract(Type objectType)
{
JsonObjectContract contract = base.CreateObjectContract(objectType);
if (objectType == typeof(ObjectId))
{
contract.Converter = new ObjectIdConverter();
}
return contract;
}
}
To use the resolver, you can construct a JsonSerializer
instance and set the ContractResolver
property on it, then use that instance to do your serialization/deserialization. If you are using JObject.ToObject()
and JObject.FromObject()
, note that both methods have overloads that accept a JsonSerializer
instance.
JsonSerializer serializer = new JsonSerializer();
serializer.ContractResolver = new CustomResolver();
JObject jo = JObject.FromObject(foo, serializer);
Alternatively, if you are using the JsonConvert
class to do your serialization/deserialization, you can create an instance of JsonSerializerSettings
, set the ContractResolver
property on that, then pass the settings to the SerializeObject()
and DeserializeObject()
methods.
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new CustomResolver();
Foo foo = JsonConvert.DeserializeObject<Foo>(json, settings);
Hope this helps.
Is there any way to add JsonSerializerSettings to a custom JsonConverter
JsonSerializerSettings
has a Converters
collection. So add your converter to that collection and then pass the settings to DeserializeObject
.
mySerializerSettings.ss.Converters.Add(new myConverter());
var obj = JsonConvert.DeserializeObject<SomeType>(someString, mySerializerSettings.ss);
custom Newtonsoft JsonConverter for arrays and collections for further manipulation
It is basically the same idea:
internal sealed class TrimmedStringCollectionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType.IsArray && objectType.GetElementType() == typeof(string);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (existingValue is null)
{
// Returning empty array???
return new string[0];
}
var array = (string[])existingValue;
return array.Where(s => !String.IsNullOrEmpty(s)).ToArray();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
}
Perhaps you might want to do the same for the write part.
Related Topics
C# - Detect Time of Last User Interaction with the Os
Appsettings Get Value from .Config File
Failed to Serialize the Response in Web API
What Is Quicker, Switch on String or Elseif on Type
How to "Flatten" or "Index" 3D-Array in 1D Array
Windows Shell Extension with C#
Mixing C# & Vb in the Same Project
Suppressing "Is Never Used" and "Is Never Assigned To" Warnings in C#
Fastest Way to Convert Image to Byte Array
How to Catch a Specific SQLexception Error
"The Given Path's Format Is Not Supported."
Automapper Convert from Multiple Sources
Execute Specified Function Every X Seconds
Inject Service into Action Filter
Linq to SQL Using Group by and Count(Distinct)
Why Does My C# Gzip Produce a Larger File Than Fiddler or PHP