0

I know PHP does not support unsigned 64 bit integers.

However I simply need to read unsigned 64 bit integer values from a MySQL table via PDO query, and pass them through to clients (without performing any calculation or comparison); the PHP page is publishing a very simple REST interface to clients and should output the query result as JSON data only.

Right now the PDO query returns untouched integers as long as they are 63bit wide (no sign), but when the original value in the table has the 64th bit set, the PDO driver converts it to a string (I have set PDO::ATTR_STRINGIFY_FETCHES attribute to false, since I want types to be preserved as much as possible).

When I call the json_encode() function, the related fields get the same treatment: they become strings when they exceed 63 bit width.

I do not want to leave them as strings in json output, since it would require "breaking" changes in client code to have them handling json-string to ulong conversion upon decoding.

Is there a way to customize json_encode behavior somehow to output them as "UInt64" ?

I've taken a look at JsonSerializable but it seems it cannot solve my issue.

Sampath Wijesinghe
  • 789
  • 1
  • 11
  • 33
Alberto Pastore
  • 193
  • 2
  • 14
  • You could use a math extension to support unsigned 64 bit integers in PHP, if it doesn't: http://php.net/manual/en/intro.gmp.php (this is a comment, because it is not an answer to your question).As to your question: Yes, you can customize json encoding, see: http://php.net/manual/en/class.jsonserializable.php – KIKO Software Jun 06 '17 at 07:19
  • check this answer https://stackoverflow.com/questions/30589965/json-decode-and-json-encode-long-integers-without-loosing-data – Eimsas Jun 06 '17 at 07:59
  • @KIKOSoftware I don't think that jsonSerializable is a solution: this class allows you only to choose what must be serialized, not how. – Alberto Pastore Jun 06 '17 at 12:59
  • Ok it looks like I have to write my very own json encoder. I was hoping I could avoid this. Come on, how can in 2017 a language have not built in support for unsigned 64 bit integers???? – Alberto Pastore Jun 06 '17 at 13:00

1 Answers1

1

I found a workaround to the problem, not exactly "elegant", but practical (and it works).

I've included in my project a custom pure-PHP json encoding library found on GitHub here.

I've declared this interface:

interface JSONSerializer
{
    public function toJSON();
}

I've implemented the interface in this simple class:

class UInt64Raw implements JSONSerializer {

    private $value;

    public function __construct($number) {
        $this->value = $number;
    }

    public function getValue() { return $this->value; }


    public function toJSON() {
        if (is_null($this->value)) return "null";
        if ($this->value) return (string)$this->value;
        return "0";
    }

} 

I've changed a little bit the default behavior of the JSON encoder library in order to invoke the interface method whenever the object to be encoded is implementing that interface.

I can then retrieve unsigned 64 bit integers as strings from PDO, wrap them inside a UInt64Raw object instance and eventually pass them unquoted (as numbers) through custom JSON encoding.

Alberto Pastore
  • 193
  • 2
  • 14