Hi, On Tue, Apr 14, 2015 at 7:33 AM, Alexey Zakhlestin <indey...@gmail.com> wrote:
> > > On 14 Apr 2015, at 07:31, Alexey Zakhlestin <indey...@gmail.com> wrote: > > > > Feels a bit hackish > > I think it is possible to introduce an overall better solution > > > > We can expose result of json-tokenizing as a tree of objects: > > > > JSON\Object > > JSON\Array > > JSON\String > > JSON\Number > > JSON\False > > JSON\True > > JSON\Null > > > > then, it would be possible to introduce any number of experimental > userland implementations like the one proposed here > > I decided to elaborate > > <?php > namespace JSON; > > abstract class Value > { > public function toJson(); > public function toNative(); // this would do, whatever > json_decode does > } > > > class Object extends Value implements \ArrayAccess, \IteratorAggregate > { > } > > class Array extends Value implements \ArrayAccess, \IteratorAggregate > { > } > > abstract class Literal extends Value > { > public function toString(); > public function __toString() > { > return $this->toString(); > } > } > > class String extends Literal > { > } > > class Number extends Literal > { > public function toInt(); > public function toFloat(); > } > > class False extends Literal > { > } > > class True extends Literal > { > } > > class Null extends Literal > { > } > > > So, in case of Number, there would be a way to get the value the way it > was stored in document using toString(), use toInt() or toFloat() to get a > value with possible precision loss or use toNative(), which would be > “smart” and return int or float using the same logic, which json_decode() > uses now. > > It would work the other way round too. Object::toJson() would return > json-representation of the tree. > This could of course can't be introduced as a default (at least not in 7) as it's a huge BC break. I don't like introducing too many new flags either (it might be the case that you would like to use this just for objects but not for Literals). Also it can't be mixed (meaning you can't have some values as int and some as Json\Number). However I think this could work nicely with json-scheme type hints and using that only if explicitly specified in the scheme. The only problem is that it's not a small feature so it will probably take some time to make that happen. JSON_FLOAT_AS_STRING is meant like a quick fix for something that is causing issue. However I it's a bit "hackish" as it applies on all floats which is not very nice. Maybe it will really make sense to wait for json-scheme. More I look into more I think that it would be a better solution. Cheers Jakub