Dealing With PHP Encoding Oddities When Deserializing JSON
A quick look at how to create a custom JSON converter to overcome some of the quirks of PHP .
Join the DZone community and get the full member experience.
Join For FreeI've recently been consuming APIs written in PHP and found an unexpected behaviour in the JSON returned.
It appears the default behaviour for PHP is to automatically convert an empty object ("bacon" : {}
) into an empty array ("bacon" : []
).
Apart from being quite confusing this adds an extra complication when deserializing to strongly typed objects. When the object returned is populated, deserializing works fine. However, trying to deserialize an empty array will throw an exception.
Custom JSON Converter in Json.Net
Json.Net allows you to create custom converters to deal with non-standard behavior such as this. In this case, I decided to use a CustomCreationConverter.
The CustomCreationConverter is a JSON converter that provides a way to customize how an object is created during JSON deserialization. Once the object has been created it will then have values populated onto it by the serializer.
My converter looks like this.
public class EmptyArrayToObjectConverter<T> : CustomCreationConverter<T> where T : class, new()
{
public override T Create(Type objectType)
{
return new T();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
reader.Read();
if (reader.TokenType == JsonToken.EndArray)
return new T();
throw new JsonSerializationException($"Cannot convert non-empty JSON Array to an object type {typeof(T)}");
}
return serializer.Deserialize(reader, objectType);
}
}
In the ReadJson
method we check for an empty array and if it is return a new instance of T
. Otherwise, deserialize as normal.
The JsonConverter
attribute is used on the property like so:
public class Hamburger
{
[JsonConverter(typeof(EmptyArrayToObjectConverter<Bacon>))
public Bacon Bacon { get; set; }
}
Both cases are now handled and will be deserialized to the correct type with no exceptions thrown.
Summary
Mapping JSIN to strongly typed objects is not always straight forward and some customization can be needed. Luckily, converters allow us to overcome any difficulties or non-standard behavior.
Published at DZone with permission of $$anonymous$$, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments