-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathJsonParser.php
More file actions
121 lines (109 loc) · 3.68 KB
/
JsonParser.php
File metadata and controls
121 lines (109 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Incenteev\ParameterHandler\Parser;
class JsonParser implements ParserInterface
{
/**
* Parses a JSON string to a PHP value.
*
* @param string $value A JSON string
* @param int $flags Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)
* @param bool $assoc When TRUE, returned objects will be converted into associative arrays.
* @param int $depth User specified recursion depth.
*
* @return mixed A PHP value
*/
public function parse($value, $flags = 0, $assoc = true, $depth = 512)
{
if ('' === $value) {
$result = array();
} else {
$result = json_decode($value, $assoc, $depth);
if (null === $result) {
$result = false;
}
}
return $result;
}
/**
* Dumps a given array of data to JSON format.
*
* @param array $data Data to dump.
*
* @return string Dumped JSON data
*/
public function dump(array $data)
{
return $this->prettyPrint(json_encode($data));
}
/**
* Returns pretty printed JSON string.
* Custom implementation for compatibility with PHP < 5.4 and custom spacing.
*
* @param string $json JSON data to be pretty printed
* @param string $spacer Spacer used e.g. ' ' or "\t"
* @param int $spacing Multiplicand for spacer (count)
* @param bool $newLineAtEof Whether to write a nl at end of file or not
*
* @return string Pretty printed JSON data
*/
protected function prettyPrint($json, $spacer = ' ', $spacing = 2, $newLineAtEof = true)
{
$result = '';
$level = 0;
$in_quotes = false;
$in_escape = false;
$ends_line_level = null;
$json_length = strlen($json);
for ($i = 0; $i < $json_length; ++$i) {
$char = $json[$i];
$new_line_level = null;
$post = '';
if ($ends_line_level !== null) {
$new_line_level = $ends_line_level;
$ends_line_level = null;
}
if ($in_escape) {
$in_escape = false;
} elseif ($char === '"') {
$in_quotes = !$in_quotes;
} elseif (!$in_quotes) {
switch ($char) {
case '}':
case ']':
$level--;
$ends_line_level = null;
$new_line_level = $level;
break;
case '{':
case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = ' ';
break;
case ' ':
case "\t":
case "\n":
case "\r":
$char = '';
$ends_line_level = $new_line_level;
$new_line_level = null;
break;
}
} elseif ($char === '\\') {
$in_escape = true;
}
if ($new_line_level !== null) {
$result .= "\n".str_repeat(str_repeat($spacer, $spacing), $new_line_level);
}
$result .= $char.$post;
}
$result = trim($result);
if (true === $newLineAtEof) {
$result .= "\n";
}
return $result;
}
}