# Patch and test cases too for

- Thread: https://externals.io/message/43223
- Original archive: https://news-web.php.net/php.internals/43223
- Started by Richard Quadling on 2009-03-02T16:21:33+00:00
- Messages: 11
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #43223 — Richard Quadling — 2009-03-02T16:21:33+00:00

URL: https://externals.io/message/43223#43223

Hi.

Regarding http://bugs.php.net/bug.php?id=47493, I've supplied a patch
to the unit tests too.

Any chance this could get committed to 5.2+



Index: tests/002.phpt
===================================================================
RCS file: /repository/php-src/ext/json/tests/002.phpt,v
retrieving revision 1.3
diff -u -r1.3 002.phpt
--- tests/002.phpt	27 May 2008 18:16:03 -0000	1.3
+++ tests/002.phpt	2 Mar 2009 16:16:13 -0000
@@ -1,4 +1,4 @@
---TEST--
+﻿--TEST--
 `json_encode()` tests
 --SKIPIF--
 <?php if (!extension_loaded("json")) print "skip"; ?>
@@ -8,8 +8,14 @@
 var_dump(json_encode(""));
 var_dump(json_encode(NULL));
 var_dump(json_encode(TRUE));
+
 var_dump(json_encode(array(""=>"")));
 var_dump(json_encode(array(array(1))));
+var_dump(json_encode(array());
+
+var_dump(json_encode(array(""=>"")), PHP_JSON_FORCE_HASH);
+var_dump(json_encode(array(array(1))),PHP_JSON_FORCE_HASH);
+var_dump(json_encode(array(),PHP_JSON_FORCE_HASH);

 var_dump(json_encode(1));
 var_dump(json_encode("руссиш"));
@@ -23,6 +29,10 @@
 string(4) "true"
 string(7) "{"":""}"
 string(5) "[[1]]"
+string(2) "[]"
+string(7) "{"":""}"
+string(15) "{"0":{"0":"1"}}"
+string(2) "{}"
 string(1) "1"
 string(38) ""\u0440\u0443\u0441\u0441\u0438\u0448""
 Done



-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #43224 — Scott MacVicar — 2009-03-02T16:27:22+00:00

URL: https://externals.io/message/43223#43224 · In reply to: #43223

Richard Quadling wrote:
> Hi.
> 
> Regarding http://bugs.php.net/bug.php?id=47493, I've supplied a patch
> to the unit tests too.
> 
> Any chance this could get committed to 5.2+
> 

I'm too convinced of the parameter name and I don't think its something
that should be added to 5.2.9 since its a feature.

Scott

---

## #43226 — Richard Quadling — 2009-03-02T16:36:36+00:00

URL: https://externals.io/message/43223#43226 · In reply to: #43224

2009/3/2 Scott MacVicar <scott@macvicar.net>:
> Richard Quadling wrote:
> > […]
>
> I'm too convinced of the parameter name and I don't think its something
> that should be added to 5.2.9 since its a feature.
>
> Scott
>

5.3+ would be OK then.

As to the param name, I'm not actually changing the name, just adding
another flag.

In javascript's terminology,the output is an array ...

[]

or a hash

{}

So forcing a hash for arrays is pretty much what would be wanted and I
as I comment in the request, we have a "force" option when coming FROM
json data ... json_decode ( string $json [, bool $assoc= false [, int
$depth= 512 ]] )

And the "name" of the param here is appropriate to PHP's use (assoc is
not part of JSON's or JS's terminology).

If you can think of a better name for the flag, then I'd be glad to use it.

Richard.

-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #43227 — Scott MacVicar — 2009-03-02T16:43:35+00:00

URL: https://externals.io/message/43223#43227 · In reply to: #43226

Richard Quadling wrote:
> 2009/3/2 Scott MacVicar <scott@macvicar.net>:
> > […]
> 
> 5.3+ would be OK then.
> 
> As to the param name, I'm not actually changing the name, just adding
> another flag.
> 
> In javascript's terminology,the output is an array ...
> 
> []
> 
> or a hash
> 
> {}
> 
> So forcing a hash for arrays is pretty much what would be wanted and I
> as I comment in the request, we have a "force" option when coming FROM
> json data ... json_decode ( string $json [, bool $assoc= false [, int
> $depth= 512 ]] )
> 
> And the "name" of the param here is appropriate to PHP's use (assoc is
> not part of JSON's or JS's terminology).
> 
> If you can think of a better name for the flag, then I'd be glad to use it.
> 

The JSON spec refers to them as arrays or objects though, as does our
documentation I believe.

PHP_JSON_FORCE_OBJECT

I however have another patch for more strict encoding so I'll try to
roll this in a single update.

Scott

---

## #43230 — Richard Quadling — 2009-03-02T20:02:52+00:00

URL: https://externals.io/message/43223#43230 · In reply to: #43227

2009/3/2 Scott MacVicar <scott@macvicar.net>:
> Richard Quadling wrote:
> > […]
>
> The JSON spec refers to them as arrays or objects though, as does our
> documentation I believe.
>
> PHP_JSON_FORCE_OBJECT
>
> I however have another patch for more strict encoding so I'll try to
> roll this in a single update.
>
> Scott
>

When it is in JSON, it isn't an object, but an object hash, similar,
as I understand things, to the differences between a class definition
and an instance. Or, I suppose more like an object serialization and
an object.

PHP_JSON_FORCE_OBJECT would be ideal.

Thank you.

-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #43232 — Ilia Alshanetsky — 2009-03-02T20:32:55+00:00

URL: https://externals.io/message/43223#43232 · In reply to: #43223

I believe that this is a feature, so it would not go into 5.2, sorry.


Ilia Alshanetsky






> Hi.
>
> Regarding http://bugs.php.net/bug.php?id=47493, I've supplied a patch
> to the unit tests too.
>
> Any chance this could get committed to 5.2+
>
>
>
> Index: tests/002.phpt
> ===================================================================
> RCS file: /repository/php-src/ext/json/tests/002.phpt,v
> retrieving revision 1.3
> diff -u -r1.3 002.phpt
> --- tests/002.phpt	27 May 2008 18:16:03 -0000	1.3
> +++ tests/002.phpt	2 Mar 2009 16:16:13 -0000
> @@ -1,4 +1,4 @@
> ---TEST--
> +--TEST--
> `json_encode()` tests
> --SKIPIF--
> <?php if (!extension_loaded("json")) print "skip"; ?>
> @@ -8,8 +8,14 @@
> var_dump(json_encode(""));
> var_dump(json_encode(NULL));
> var_dump(json_encode(TRUE));
> +
> var_dump(json_encode(array(""=>"")));
> var_dump(json_encode(array(array(1))));
> +var_dump(json_encode(array());
> +
> +var_dump(json_encode(array(""=>"")), PHP_JSON_FORCE_HASH);
> +var_dump(json_encode(array(array(1))),PHP_JSON_FORCE_HASH);
> +var_dump(json_encode(array(),PHP_JSON_FORCE_HASH);
>
> var_dump(json_encode(1));
> var_dump(json_encode("руссиш"));
> @@ -23,6 +29,10 @@
> string(4) "true"
> string(7) "{"":""}"
> string(5) "[[1]]"
> +string(2) "[]"
> +string(7) "{"":""}"
> +string(15) "{"0":{"0":"1"}}"
> +string(2) "{}"
> string(1) "1"
> string(38) ""\u0440\u0443\u0441\u0441\u0438\u0448""
> Done
>
>
>
> -- 
> -----
> Richard Quadling
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> "Standing on the shoulders of some very clever giants!"

---

## #43233 — Richard Quadling — 2009-03-02T20:35:00+00:00

URL: https://externals.io/message/43223#43233 · In reply to: #43232

2009/3/2 Ilia Alshanetsky <ilia@prohost.org>:
> I believe that this is a feature, so it would not go into 5.2, sorry.
>
>
> Ilia Alshanetsky
>

That's OK. Just like to try these things sometimes.

>
>
>
> 
>
> > […]
>
>



-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #43288 — Richard Quadling — 2009-03-10T10:32:40+00:00

URL: https://externals.io/message/43223#43288 · In reply to: #43227

2009/3/2 Scott MacVicar <scott@macvicar.net>:
> Richard Quadling wrote:
> > […]
>
> The JSON spec refers to them as arrays or objects though, as does our
> documentation I believe.
>
> PHP_JSON_FORCE_OBJECT
>
> I however have another patch for more strict encoding so I'll try to
> roll this in a single update.
>
> Scott
>

First of all, this isn't a "why is this not done yet" message. Honestly.

Do you have any timeframe for this though?

Do you think it will be ready for 5.3RC1?

Thank you,

Richard Quadling.

-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #43312 — christopher.oestlund@gmail.com — 2009-03-11T19:25:13+00:00

URL: https://externals.io/message/43223#43312 · In reply to: #43288

I think this behavior is a bit odd too:

php -r "echo json_encode(array(0=>'test'));" // ["test"]
php -r "echo json_encode(array(1=>'test'));" // {"1":"test"}



On Tue, Mar 10, 2009 at 11:32 AM, Richard Quadling <rquadling@googlemail.com
> wrote:

> 2009/3/2 Scott MacVicar <scott@macvicar.net>:
> > […]
> it.
> > […]
>
> First of all, this isn't a "why is this not done yet" message. Honestly.
>
> Do you have any timeframe for this though?
>
> Do you think it will be ready for 5.3RC1?
>
> Thank you,
>
> Richard Quadling.
>
> --
> -----
> Richard Quadling
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> "Standing on the shoulders of some very clever giants!"

---

## #43313 — Richard Quadling — 2009-03-11T19:47:02+00:00

URL: https://externals.io/message/43223#43313 · In reply to: #43312

2009/3/11 Christopher Östlund <christopher.oestlund@gmail.com>:
> I think this behavior is a bit odd too:
>
> php -r "echo json_encode(array(0=>'test'));" // ["test"]
> php -r "echo json_encode(array(1=>'test'));" // {"1":"test"}
>
>

And

php -r "echo json_encode(array(0=>'test',3=>'foo'));" // {"0":"test","3":"foo"}

So it seems that zero-base, sequential, integer indexed arrays will be
output as a JS array and any other type of array will be an object
hash. The PHP_JSON_FORCE_OBJECT (thank you Scott for the name) would
at least clarify the output for dynamically created output.



>
> On Tue, Mar 10, 2009 at 11:32 AM, Richard Quadling <rquadling@googlemail.com
> > […]
>
> > […]
>



-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #43387 — Lewis Wright — 2009-03-18T09:20:40+00:00

URL: https://externals.io/message/43223#43387

Exactly. In javascript terms, that is what you'd expect since:

array(1=>'test')

..Isn't a valid array.

2009/3/17 Scott MacVicar <scott@macvicar.net>:
> 
>
> > […]
>
> The reason for this is encoding looks to see if the array is sequentially
> numbered from 0 to x, and if so its a compatible javascript array. If it
> starts at any other value or skips a number then it creates an object for
> its output.
>
> Scott
