Skip to content

json_encode(): option to only encode empty arrays as objects - #454

Closed
datibbaw wants to merge 4 commits into
php:masterfrom
datibbaw:json-empty-objects
Closed

datibbaw wants to merge 4 commits into
php:masterfrom
datibbaw:json-empty-objects

Conversation

@datibbaw

Copy link
Copy Markdown
Contributor

The JSON_FORCE_OBJECT option always encodes arrays as JSON objects, but there are cases in which you know the array will have only associate keys at a later stage.

I've created the JSON_FORCE_EMPTY_OBJECT to encode only empty arrays as objects. Numerically indexed arrays retain their regular [] encoding.

A specific use-case is when the encoded value is passed to JavaScript; if an array is passed, you can add properties to it, but when you later do JSON.stringify() it returns an empty array. This is quite unlike PHP of course, because arrays can be both things, depending on whether it contains only numeric keys or not.

@dsp

dsp commented Sep 17, 2013

Copy link
Copy Markdown
Member

This PR contains unrelated commits. Also I think the problem can be much easier be solved with a userland function instead of adding yet another constant.

@datibbaw

Copy link
Copy Markdown
Contributor Author

@dsp I had an earlier commit on my master branch that I had already pushed, so I did a local revert on them. I haven't figured out how to get rid of them altogether.

What would you suggest as a userland function, though?

@datibbaw datibbaw closed this Sep 17, 2013
@datibbaw

Copy link
Copy Markdown
Contributor Author

Damn, I think I forked up my forked repo =(

heyJordanParker added a commit to heyJordanParker/php-sharp that referenced this pull request Oct 13, 2026
Problem

sharp/baseline/spec lists the spec examples Guard 1 expects to fail. After
the merge of php-sharp master and of mago-sharp feat/slice-structs into
sharp/mago, 23 of them pass, and sharp/bin/spec fails while a passing example
stays listed.

Solution

sharp/bin/spec, run with the engine and the mago built from this tree,
printed these removals, and a second run passes against the new baseline.
Section 10 removes struct-changes. struct-parse stays until
generics R3c's Sharp\Internal\accepts lands, and struct-parse-object until the
Object type of task php#454 lands.

sharp/baseline/spec/
├── §2/keyword-members, §2/keyword-names    removed
├── §5/interface-members                    removed
├── §9.1/constructor-call-this, named-constructors, set-once, super-call  removed
├── §10/struct-changes                      removed
├── §12/collection-read-methods, collection-write-methods, enum-map-keys,
│   index-changes, natural-remove, nullable-map-values  removed
├── §15/emit-forms, extern-event, extern-event-effects, listener-errors,
│   standalone-event                        removed
├── §16/initializer-shorthand               removed
├── §17/braces                              removed
├── §23/import-rename                       removed
└── §28/module-law                          removed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants