Skip to content

json_decode model or collection return an empty object / array #77

@krisanalfa

Description

@krisanalfa

I have a model with attribute like this:

[
  "$type" => "TestCollection",
  "$id" => "f3023064-feea-11e4-8147-28d2442a84f9",
  "field_a" => "a1",
  "field_b" => "b1",
]

When I want to call json_decode($model) it returns an empty object:

{}

I know that JsonKit\JsonSerializer interface is used as a contract of Norm\Model to customize the model's JSON representation when decoded with json_decode() refer to this manual.

But when I change the contract of Norm\Model with JsonSerializable (instead of JsonKit\JsonSerializer). It returns:

{"$type":"TestCollection","$id":"404c6a00-feec-11e4-9b41-28d2442a84f9","field_a":"a1","field_b":"b1"}

This is modified Norm\Model declaration:

class Model implements \JsonKit\JsonSerializer, ArrayAccess // before
{
    // ...
}

class Model implements \JsonSerializable, ArrayAccess // after
{
    // ...
}

Here is a screenshot before and after I change the contract:
Some proof of concept

I know, this is dilemma, because JsonSerializable interface is only supported in PHP 5.4. Should we upgrade the minimum requirement? This is some articles for your consideration to upgrade the minimum requirement to PHP 5.4:

I didn't mean to change the minimum requirement. IMHO, if you really want to support PHP 5.3, I think JsonKit\JsonSerializer implementation (json_decode($model)-ing feature) is not used anymore (should we remove it?), since this feature will break in PHP 5.4. Or we can just upgrade the minimum requirement, and replace the JsonKit\JsonSerializer contract with \JsonSerializable.

Thanks for your very kind attention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions