Skip to content

Commit 9a00a55

Browse files
committed
Add Json renderer option: jsonEncodeFlags
// this option is currently only for the Json renderer. // it controls how the output JSON is formatted. // see availabe options on https://www.php.net/manual/en/function.json-encode.php 'jsonEncodeFlags' => \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE, Signed-off-by: Jack Cherng <[email protected]>
1 parent cb54261 commit 9a00a55

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ $rendererOptions = [
108108
// internally, ops (tags) are all int type but this is not good for human reading.
109109
// set this to "true" to convert them into string form before outputting.
110110
'outputTagAsString' => false,
111+
// this option is currently only for the Json renderer.
112+
// it controls how the output JSON is formatted.
113+
// see availabe options on https://www.php.net/manual/en/function.json-encode.php
114+
'jsonEncodeFlags' => \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE,
111115
// change this value to a string as the returned diff if the two input strings are identical
112116
'resultForIdenticals' => null,
113117
// extra HTML classes added to the DOM of the diff container

Diff for: example/demo.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
// internally, ops (tags) are all int type but this is not good for human reading.
7777
// set this to "true" to convert them into string form before outputting.
7878
'outputTagAsString' => false,
79+
// this option is currently only for the Json renderer.
80+
// it controls how the output JSON is formatted.
81+
// see availabe options on https://www.php.net/manual/en/function.json-encode.php
82+
'jsonEncodeFlags' => \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE,
7983
// change this value to a string as the returned diff if the two input strings are identical
8084
'resultForIdenticals' => null,
8185
// extra HTML classes added to the DOM of the diff container
@@ -237,15 +241,17 @@
237241
$newFile,
238242
'Json',
239243
$diffOptions,
240-
['outputTagAsString' => true] + $rendererOptions
241-
);
242-
243-
$beautified = \json_encode(
244-
\json_decode($jsonResult, true),
245-
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT
244+
[
245+
'outputTagAsString' => true,
246+
'jsonEncodeFlags' => (
247+
\JSON_PRETTY_PRINT |
248+
\JSON_UNESCAPED_SLASHES |
249+
\JSON_UNESCAPED_UNICODE
250+
),
251+
] + $rendererOptions
246252
);
247253

248-
echo $beautified;
254+
echo $jsonResult;
249255

250256
?></code></pre>
251257

Diff for: src/Renderer/AbstractRenderer.php

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ abstract class AbstractRenderer implements RendererInterface
6767
// internally, ops (tags) are all int type but this is not good for human reading.
6868
// set this to "true" to convert them into string form before outputting.
6969
'outputTagAsString' => false,
70+
// this option is currently only for the Json renderer.
71+
// it controls how the output JSON is formatted.
72+
// see availabe options on https://www.php.net/manual/en/function.json-encode.php
73+
'jsonEncodeFlags' => \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE,
7074
// change this value to a string as the returned diff if the two input strings are identical
7175
'resultForIdenticals' => null,
7276
// extra HTML classes added to the DOM of the diff container

Diff for: src/Renderer/Html/Json.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ protected function redererChanges(array $changes): string
4141
$this->convertTagToString($changes);
4242
}
4343

44-
return \json_encode(
45-
$changes,
46-
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES
47-
);
44+
return \json_encode($changes, $this->options['jsonEncodeFlags']);
4845
}
4946

5047
/**

0 commit comments

Comments
 (0)