Req #69422 [Com]: json_encode() needs encoding specification

From: Date: Mon, 01 Nov 2021 10:11:51 +0000
Subject: Req #69422 [Com]: json_encode() needs encoding specification
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237484@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69422&edit=1

 ID:                 69422
 Comment by:         glash dot gnome at gmail dot com
 Reported by:        yohgaki@php.net
 Summary:            json_encode() needs encoding specification
 Status:             Open
 Type:               Feature/Change Request
 Package:            JSON related
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Hello, I propose an incomplete solution but it may solve some problems.


Like array_intersect() we could do :

class ExpectedObject {
  string $test;
  float  $poc;
}
$obj = json_decode_spec('{"test":1234567890.12345678, "poc":
"1Gm", "key":'val'}', ExpectedObject::class, ...);
var_dump($obj);

object(ExpectedObject)#1 {
   test => (string) '1234567890.12345678'
   poc  => (float) 1.0
}

Regards


Previous Comments:
------------------------------------------------------------------------
[2015-04-19 22:13:04] yohgaki@php.net

Related bug
https://bugs.php.net/bug.php?id=68456

------------------------------------------------------------------------
[2015-04-10 21:57:21] yohgaki@php.net

Description:
------------
Currently, json_encode() encode PHP int/float/bool as JSON numeric(int/float) and bool. Since JSON
numeric does not map to PHP int/float as JSON numeric could be any precision, large numbers need to
be stored as "string" which is converted to JSON string.

Some "string" needed to be encoded as numeric. To do that json_encode() needs some kind of
specification to encode data as programmer intended.

http://json-schema.org/examples.html

This could be used as the specification.



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=69422&edit=1


Thread (4 messages)

« previous php.bugs (#237484) next »