Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"Nuclear war can ruin your whole compile." -- Karl Lehenbauer


devel / comp.lang.php / Re: JSON encode "corrupted"?

SubjectAuthor
* JSON encode "corrupted"?Torakiki
+* Re: JSON encode "corrupted"?Arno Welzel
|`* Re: JSON encode "corrupted"?Lew Pitcher
| `- Re: JSON encode "corrupted"?Arno Welzel
`* Re: JSON encode "corrupted"?Torakiki
 +- Re: JSON encode "corrupted"?J.O. Aho
 `- Re: JSON encode "corrupted"?Arno Welzel

1
JSON encode "corrupted"?

<seh0um$ket$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=328&group=comp.lang.php#328

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!NzUc306BTg4UesFXWD6AIg.user.46.165.242.91.POSTED!not-for-mail
From: torakiki.sanNIENTE@SPAMgmail.com (Torakiki)
Newsgroups: comp.lang.php
Subject: JSON encode "corrupted"?
Date: Thu, 5 Aug 2021 17:43:18 +0200
Organization: Aioe.org NNTP Server
Message-ID: <seh0um$ket$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="20957"; posting-host="NzUc306BTg4UesFXWD6AIg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
X-Mozilla-News-Host: news://news.aioe.org:119
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: it-IT
 by: Torakiki - Thu, 5 Aug 2021 15:43 UTC

json_encode it seems that, in the case of only one element with index =
0, it returns an incorrect result.

$array = [];
$array[0] = "Some text"

it should return (in Firefox 90.0.2)

{"0": "Some text"}

and instead returns a string like

["Some text"]

If I add other elements, like

$array[101] = "Some text 2"
$array[1234] = "Some text 3"

the result is correct:

{"0":"Some text","101":"Some text 2","1234":"Some text 3"}

Try this code:

<?php

$array=[];
$array[0]="Some text";
var_dump($array);

$json=json_encode($array);
var_dump($json);

$array=[];
$array[0]="Some text";
$array[101]="Some text 2";
$array[1234]="Some text 3";
var_dump($array);

$json=json_encode($array);
var_dump($json);

?>

Thank's

--
>> Torakiki

Re: JSON encode "corrupted"?

<in2il0Fg6j9U1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=329&group=comp.lang.php#329

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: usenet@arnowelzel.de (Arno Welzel)
Newsgroups: comp.lang.php
Subject: Re: JSON encode "corrupted"?
Date: Thu, 5 Aug 2021 18:41:04 +0200
Lines: 26
Message-ID: <in2il0Fg6j9U1@mid.individual.net>
References: <seh0um$ket$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 7bit
X-Trace: individual.net +AON0hOe9Ga98VdhUk1wFg3NGDe8zjN8pZg0m9nR/VA1G8E5eV
Cancel-Lock: sha1:mLtw0d+NpfRNcjQWDyJ3dDATqPk=
In-Reply-To: <seh0um$ket$1@gioia.aioe.org>
 by: Arno Welzel - Thu, 5 Aug 2021 16:41 UTC

Torakiki:

> json_encode it seems that, in the case of only one element with index =
> 0, it returns an incorrect result.
>
> $array = [];
> $array[0] = "Some text"
>
> it should return (in Firefox 90.0.2)
>
> {"0": "Some text"}

Why? $array is still just an array with one scalar element.

> and instead returns a string like
>
> ["Some text"]

Which is valid json as well. This defines an array with one element.

See: <https://www.json.org/json-en.html>

--
Arno Welzel
https://arnowelzel.de

Re: JSON encode "corrupted"?

<seh6k8$opv$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=330&group=comp.lang.php#330

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.php
Subject: Re: JSON encode "corrupted"?
Date: Thu, 5 Aug 2021 17:20:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <seh6k8$opv$1@dont-email.me>
References: <seh0um$ket$1@gioia.aioe.org>
<in2il0Fg6j9U1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 5 Aug 2021 17:20:08 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6f8a0a102bc0965ead63dbc6f13ac6ff";
logging-data="25407"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Am4IkVic2VRRdzecjSzZR9rt4YY2akV4="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:/JmGo/6TZCvIrvz70RwKlSwK+Qo=
 by: Lew Pitcher - Thu, 5 Aug 2021 17:20 UTC

On Thu, 05 Aug 2021 18:41:04 +0200, Arno Welzel wrote:

> Torakiki:
>
>> json_encode it seems that, in the case of only one element with index =
>> 0, it returns an incorrect result.
>>
>> $array = [];
>> $array[0] = "Some text"
>>
>> it should return (in Firefox 90.0.2)
>>
>> {"0": "Some text"}
>
> Why? $array is still just an array with one scalar element.
>
>> and instead returns a string like
>>
>> ["Some text"]
>
> Which is valid json as well. This defines an array with one element.
>
> See: <https://www.json.org/json-en.html>

From that link, the OP's "unexpected" string
>> ["Some text"]
expresses an array, which (in this case) contains one element.

Had the OP added
$array[1] = "Other text"
the results probably would have been expressed as
["Some text","Other text"]

But...

The OP changed the sequential array into a sparse array by
assigning additional elements asequentially:
>> $array[101] = "Some text 2"
>> $array[1234] = "Some text 3"

Because of the "sparseness" of this object, it can't be
expressed as a JSON array, so json_encode() encoded the
input sparse array as a JSON object.

--
Lew Pitcher
"In Skills, We Trust"

Re: JSON encode "corrupted"?

<sehdv4$1ah6$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=331&group=comp.lang.php#331

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!aioe.org!NzUc306BTg4UesFXWD6AIg.user.46.165.242.91.POSTED!not-for-mail
From: torakiki.sanNIENTE@SPAMgmail.com (Torakiki)
Newsgroups: comp.lang.php
Subject: Re: JSON encode "corrupted"?
Date: Thu, 5 Aug 2021 21:25:25 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sehdv4$1ah6$1@gioia.aioe.org>
References: <seh0um$ket$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="43558"; posting-host="NzUc306BTg4UesFXWD6AIg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Content-Language: it-IT
X-Notice: Filtered by postfilter v. 0.9.2
 by: Torakiki - Thu, 5 Aug 2021 19:25 UTC

> Why? $array is still just an array with one scalar element.

Try this (last PHP 7.4.22 x64):

<?php

$array=[];
$array[0]="Some text";
var_dump($array);

$json=json_encode($array);
var_dump($json);

$array=[];
$array[1]="Some text";
var_dump($array);

$json=json_encode($array);
var_dump($json);

?>

you get

---------------------------------------------
array(1) {

[0]=>

string(9) "Some text"

}

string(13) "["Some text"]"

array(1) {

[1]=>

string(9) "Some text"

}

string(17) "{"1":"Some text"}"
---------------------------------------------

IMHO, seem to be a bug!

--
>> Torakiki

Re: JSON encode "corrupted"?

<in2tmsFidbeU1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=332&group=comp.lang.php#332

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: user@example.net (J.O. Aho)
Newsgroups: comp.lang.php
Subject: Re: JSON encode "corrupted"?
Date: Thu, 5 Aug 2021 21:49:48 +0200
Lines: 31
Message-ID: <in2tmsFidbeU1@mid.individual.net>
References: <seh0um$ket$1@gioia.aioe.org> <sehdv4$1ah6$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net lyjhxfxwucXyNVbt2/4DMgL+vYwxjotabwnp8au7i5zzHYz4R0
Cancel-Lock: sha1:QfwdkWeMR1d2hJp39DKVexVy8Yc=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
In-Reply-To: <sehdv4$1ah6$1@gioia.aioe.org>
Content-Language: en-US-large
 by: J.O. Aho - Thu, 5 Aug 2021 19:49 UTC

On 05/08/2021 21:25, Torakiki wrote:
>
> > Why? $array is still just an array with one scalar element.
>
> Try this (last PHP 7.4.22 x64):
>
> <?php
>
> $array=[];
> $array[0]="Some text";
> var_dump($array);
>
> $json=json_encode($array);
> var_dump($json);
>
>
> $array=[];
> $array[1]="Some text";
> var_dump($array);
>
> $json=json_encode($array);
> var_dump($json);
>
> ?>
>
> IMHO, seem to be a bug!

nope, not a bug, just a standard how to represent different types of arrays.
https://cswr.github.io/JsonSchema/spec/arrays/

Re: JSON encode "corrupted"?

<in9n9dFu47pU4@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=333&group=comp.lang.php#333

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: usenet@arnowelzel.de (Arno Welzel)
Newsgroups: comp.lang.php
Subject: Re: JSON encode "corrupted"?
Date: Sun, 8 Aug 2021 11:43:11 +0200
Lines: 55
Message-ID: <in9n9dFu47pU4@mid.individual.net>
References: <seh0um$ket$1@gioia.aioe.org> <in2il0Fg6j9U1@mid.individual.net>
<seh6k8$opv$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Trace: individual.net Db6kgM8/ZAMDKtkJEEFXMQC2YYs/ZzrYSKChieFL2tE4KpIAsn
Cancel-Lock: sha1:hf9gsGLVQX1lp7QtmFIGwhF3tts=
In-Reply-To: <seh6k8$opv$1@dont-email.me>
 by: Arno Welzel - Sun, 8 Aug 2021 09:43 UTC

Lew Pitcher:

> On Thu, 05 Aug 2021 18:41:04 +0200, Arno Welzel wrote:
>
>> Torakiki:
>>
>>> json_encode it seems that, in the case of only one element with index =
>>> 0, it returns an incorrect result.
>>>
>>> $array = [];
>>> $array[0] = "Some text"
>>>
>>> it should return (in Firefox 90.0.2)
>>>
>>> {"0": "Some text"}
>>
>> Why? $array is still just an array with one scalar element.
>>
>>> and instead returns a string like
>>>
>>> ["Some text"]
>>
>> Which is valid json as well. This defines an array with one element.
>>
>> See: <https://www.json.org/json-en.html>
>
> From that link, the OP's "unexpected" string

Why? The string

["some text"]

is a valid JSON array.

Also see here:

<https://jsonformatter.curiousconcept.com>
<http://json.parser.online.fr>

[...]
> The OP changed the sequential array into a sparse array by
> assigning additional elements asequentially:
>>> $array[101] = "Some text 2"
>>> $array[1234] = "Some text 3"
>
> Because of the "sparseness" of this object, it can't be
> expressed as a JSON array, so json_encode() encoded the
> input sparse array as a JSON object.

Correct.

--
Arno Welzel
https://arnowelzel.de

Re: JSON encode "corrupted"?

<in9nchFu47pU5@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=334&group=comp.lang.php#334

  copy link   Newsgroups: comp.lang.php
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: usenet@arnowelzel.de (Arno Welzel)
Newsgroups: comp.lang.php
Subject: Re: JSON encode "corrupted"?
Date: Sun, 8 Aug 2021 11:44:50 +0200
Lines: 38
Message-ID: <in9nchFu47pU5@mid.individual.net>
References: <seh0um$ket$1@gioia.aioe.org> <sehdv4$1ah6$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 7bit
X-Trace: individual.net fyRKS3bXTicN0cNaItadsA+sG3CdlVASkPd7aTr/uMrfgKSZ+r
Cancel-Lock: sha1:+Kb255eD1QAPudsVP9uF8kQO9i4=
In-Reply-To: <sehdv4$1ah6$1@gioia.aioe.org>
 by: Arno Welzel - Sun, 8 Aug 2021 09:44 UTC

Torakiki:

>
> > Why? $array is still just an array with one scalar element.
>
> Try this (last PHP 7.4.22 x64):
>
> <?php
>
> $array=[];
> $array[0]="Some text";
> var_dump($array);
>
> $json=json_encode($array);
> var_dump($json);
>
>
> $array=[];
> $array[1]="Some text";
> var_dump($array);
>
> $json=json_encode($array);
> var_dump($json);
>
> ?>
>
> you get

Can you PLEASE format your output to NOT take up so much space, thank you!

And no this is not a bug, since JSON arrays can't have keys and therefor
PHP MUST create a JSON object list instead of an array.

--
Arno Welzel
https://arnowelzel.de

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor