Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return {} as empty object instead of empty array #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thekid
Copy link
Member

@thekid thekid commented Aug 5, 2024

...and be consistent with emitting empty objects as {}.

Before

Emitting an empty object as JSON after reading it results in it being output as an empty array:

$ xp -w 'use text\json\Json; Json::of(Json::read("{}"))'
[]

# There is no problem with non-empty objects though!
$ xp -w 'use text\json\Json; Json::of(Json::read("{\"key\":\"value\"}"))'
{"key":"value"}

After

Consistent result:

$ xp -w 'use text\json\Json; Json::of(Json::read("{}"))'
{}

Potential pitfalls

Objects (even ones without a single property) are never considered empty:

empty([]);         // true
empty((object)[]); // false

Also, using sizeof() will raise the following exception: lang.Error (sizeof(): Argument 1 ($value) must be of type Countable|array, stdClass given) - similar to all cases where we use type hints; which would then need to be widened to the (ugly-looking) array|stdClass to support this.

See also


⚠️ Heads up: This is a BC break and would require a major version release, 6.0 at the time of writing.

...and be consistent with emitting empty objects as `{}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant