Skip to content

Commit 07ca1e8

Browse files
committed
Simplify internal JSON-RPC protocol by avoiding unneeded escapes
1 parent 8d7ecd0 commit 07ca1e8

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

res/sqlite-worker.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
//
55
// Communication happens via newline-delimited JSON-RPC messages, see:
66
// $ php res/sqlite-worker.php
7-
// < {"id":0,"method":"open","params":["test.db"]}
8-
// > {"id":0,"result":true}
7+
// < {"id":1,"method":"open","params":["examples/users.db",null]}
8+
// > {"id":1,"result":true}
9+
// < {"id":2,"method":"query","params":["SELECT 42",[]]}
10+
// > {"id":2,"result":{"columns":["42"],"rows":[{"42":42}],"insertId":0,"changed":0}}
11+
// < {"id":3,"method":"query","params":["SELECT ? AS name",["Alice"]]}
12+
// > {"id":3,"result":{"columns":["name"],"rows":[{"name":"Alice"}],"insertId":0,"changed":0}}
913
//
1014
// Or via socket connection (used for Windows, which does not support non-blocking process pipe I/O)
1115
// $ nc localhost 8080
@@ -45,11 +49,11 @@
4549
});
4650

4751
$in = new Decoder($through);
48-
$out = new Encoder($stream);
52+
$out = new Encoder($stream, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | (\PHP_VERSION_ID >= 50606 ? \JSON_PRESERVE_ZERO_FRACTION : 0));
4953
} else {
5054
// no socket address given, use process I/O pipes
5155
$in = new Decoder(new ReadableResourceStream(\STDIN, $loop));
52-
$out = new Encoder(new WritableResourceStream(\STDOUT, $loop));
56+
$out = new Encoder(new WritableResourceStream(\STDOUT, $loop), \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | (\PHP_VERSION_ID >= 50606 ? \JSON_PRESERVE_ZERO_FRACTION : 0));
5357
}
5458

5559
// report error when input is invalid NDJSON
@@ -135,7 +139,7 @@
135139
foreach ($row as &$value) {
136140
if (\is_string($value) && \preg_match('/[\x00-\x08\x11\x12\x14-\x1f\x7f]/u', $value) !== 0) {
137141
$value = ['base64' => \base64_encode($value)];
138-
} elseif (\is_float($value)) {
142+
} elseif (\is_float($value) && \PHP_VERSION_ID < 50606) {
139143
$value = ['float' => $value];
140144
}
141145
}

src/Io/ProcessIoDatabase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function query($sql, array $params = array())
8080
foreach ($params as &$value) {
8181
if (\is_string($value) && \preg_match('/[\x00-\x08\x11\x12\x14-\x1f\x7f]/u', $value) !== 0) {
8282
$value = ['base64' => \base64_encode($value)];
83-
} elseif (\is_float($value)) {
83+
} elseif (\is_float($value) && \PHP_VERSION_ID < 50606) {
8484
$value = ['float' => $value];
8585
}
8686
}
@@ -155,7 +155,7 @@ public function send($method, array $params)
155155
'id' => $id,
156156
'method' => $method,
157157
'params' => $params
158-
), \JSON_UNESCAPED_SLASHES) . "\n");
158+
), \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | (\PHP_VERSION_ID >= 50606 ? \JSON_PRESERVE_ZERO_FRACTION : 0)) . "\n");
159159

160160
$deferred = new Deferred();
161161
$this->pending[$id] = $deferred;

0 commit comments

Comments
 (0)