DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Dealing With PHP Encoding Oddities When Deserializing JSON

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 .

$$anonymous$$ user avatar by
$$anonymous$$
·
May. 07, 19 · Tutorial
Like (2)
Save
Tweet
Share
6.17K Views

Join the DZone community and get the full member experience.

Join For Free

I'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.

JSON PHP

Published at DZone with permission of $$anonymous$$, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Key Assumption of Modern Work Culture
  • Top 5 PHP REST API Frameworks
  • Using AI and Machine Learning To Create Software
  • Kubernetes vs Docker: Differences Explained

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: