Skip to content

Commit b30227c

Browse files
committed
adding 'php' port
1 parent a6a6c31 commit b30227c

3 files changed

Lines changed: 131 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# JSON-minify
22

3-
A port of the `JSON-minify` utility to the [target-language-name] language.
3+
A port of the `JSON-minify` utility to the PHP language.
44

55
## Overview
66

77
`JSON-minify` minifies blocks of JSON-like content into valid JSON by removing all whitespace *and* JS-style comments (single-line `//` and multiline `/* .. */`).
88

99
With `JSON-minify`, you can maintain developer-friendly JSON documents, but minify them before parsing or transmitting them over-the-wire.
1010

11-
## Porting
12-
13-
Please see [PORTING.md](PORTING.md) for instructions.
14-
1511
## Testing
1612

17-
Please see [TESTING.md](TESTING.md) for example tests.
13+
To run the tests:
14+
15+
```
16+
php tests.php
17+
```
1818

1919
## License
2020

minify.json.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*! JSON.minify()
4+
v0.1 (c) Kyle Simpson
5+
MIT License
6+
*/
7+
8+
function json_minify($json) {
9+
$tokenizer = "/\"|(\/\*)|(\*\/)|(\/\/)|\n|\r/";
10+
$in_string = false;
11+
$in_multiline_comment = false;
12+
$in_singleline_comment = false;
13+
$tmp; $tmp2; $new_str = array(); $ns = 0; $from = 0; $lc; $rc; $lastIndex = 0;
14+
15+
while (preg_match($tokenizer,$json,$tmp,PREG_OFFSET_CAPTURE,$lastIndex)) {
16+
$tmp = $tmp[0];
17+
$lastIndex = $tmp[1] + strlen($tmp[0]);
18+
$lc = substr($json,0,$lastIndex - strlen($tmp[0]));
19+
$rc = substr($json,$lastIndex);
20+
if (!$in_multiline_comment && !$in_singleline_comment) {
21+
$tmp2 = substr($lc,$from);
22+
if (!$in_string) {
23+
$tmp2 = preg_replace("/(\n|\r|\s)*/","",$tmp2);
24+
}
25+
$new_str[] = $tmp2;
26+
}
27+
$from = $lastIndex;
28+
29+
if ($tmp[0] == "\"" && !$in_multiline_comment && !$in_singleline_comment) {
30+
preg_match("/(\\\\)*$/",$lc,$tmp2);
31+
if (!$in_string || !$tmp2 || (strlen($tmp2[0]) % 2) == 0) { // start of string with ", or unescaped " character found to end string
32+
$in_string = !$in_string;
33+
}
34+
$from--; // include " character in next catch
35+
$rc = substr($json,$from);
36+
}
37+
else if ($tmp[0] == "/*" && !$in_string && !$in_multiline_comment && !$in_singleline_comment) {
38+
$in_multiline_comment = true;
39+
}
40+
else if ($tmp[0] == "*/" && !$in_string && $in_multiline_comment && !$in_singleline_comment) {
41+
$in_multiline_comment = false;
42+
}
43+
else if ($tmp[0] == "//" && !$in_string && !$in_multiline_comment && !$in_singleline_comment) {
44+
$in_singleline_comment = true;
45+
}
46+
else if (($tmp[0] == "\n" || $tmp[0] == "\r") && !$in_string && !$in_multiline_comment && $in_singleline_comment) {
47+
$in_singleline_comment = false;
48+
}
49+
else if (!$in_multiline_comment && !$in_singleline_comment && !(preg_match("/\n|\r|\s/",$tmp[0]))) {
50+
$new_str[] = $tmp[0];
51+
}
52+
}
53+
$new_str[] = $rc;
54+
return implode("",$new_str);
55+
}
56+
57+
?>

tests.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
require_once("./minify.json.php");
4+
5+
$tests = array(
6+
array(
7+
"source" => "
8+
// this is a JSON file with comments
9+
{
10+
\"foo\": \"bar\", // this is cool
11+
\"bar\": [
12+
\"baz\", \"bum\", \"zam\"
13+
],
14+
/* the rest of this document is just fluff
15+
in case you are interested. */
16+
\"something\": 10,
17+
\"else\": 20
18+
}
19+
20+
/* NOTE: You can easily strip the whitespace and comments
21+
from such a file with the JSON.minify() project hosted
22+
here on github at http://github.com/getify/JSON.minify
23+
*/\n",
24+
"assert" => "{\"foo\":\"bar\",\"bar\":[\"baz\",\"bum\",\"zam\"],\"something\":10,\"else\":20}"
25+
),
26+
array(
27+
"source" => "
28+
\n
29+
{\"/*\":\"*/\",\"//\":\"\",/*\"//\"*/\"/*/\"://
30+
\"//\"}
31+
\n",
32+
"assert" => "{\"/*\":\"*/\",\"//\":\"\",\"/*/\":\"//\"}"
33+
),
34+
array(
35+
"source" => "
36+
/*
37+
this is a
38+
multi line comment */{
39+
\n
40+
\"foo\"
41+
:
42+
\"bar/*\"// something
43+
, \"b\\\"az\":/*
44+
something else */\"blah\"
45+
\n
46+
}\n",
47+
"assert" => "{\"foo\":\"bar/*\",\"b\\\"az\":\"blah\"}"
48+
),
49+
array(
50+
"source" => "
51+
{\"foo\": \"ba\\\"r//\", \"bar\\\\\": \"b\\\\\\\"a/*z\",
52+
\"baz\\\\\\\\\": /* yay */ \"fo\\\\\\\\\\\"*/o\"
53+
}\n",
54+
"assert" => "{\"foo\":\"ba\\\"r//\",\"bar\\\\\":\"b\\\\\\\"a/*z\",\"baz\\\\\\\\\":\"fo\\\\\\\\\\\"*/o\"}"
55+
)
56+
);
57+
58+
foreach ($tests as $idx => $test) {
59+
$res = json_minify($test["source"]);
60+
if ($test["assert"] !== $res) {
61+
throw new Exception("Test (" . ($idx + 1) . ") failed:\n " . $res);
62+
}
63+
echo "Test " . ($idx + 1) . " passed\n";
64+
}
65+
66+
echo "Done.\n";
67+
68+
?>

0 commit comments

Comments
 (0)