|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ahc\Json; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Attempts to fix truncated JSON by padding contextual counterparts at the end. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Jitendra Adhikari <[email protected]> |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
* |
|
11
|
|
|
* @internal |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/adhocore/php-json-fixer |
|
14
|
|
|
*/ |
|
15
|
|
|
trait PadsJson |
|
16
|
|
|
{ |
|
17
|
|
|
public function pad($tmpJson) |
|
18
|
|
|
{ |
|
19
|
|
|
if (!$this->inStr) { |
|
20
|
|
|
$tmpJson = \rtrim($tmpJson, ','); |
|
21
|
|
|
while ($this->lastToken() === ',') { |
|
|
|
|
|
|
22
|
|
|
\array_pop($this->stack); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$tmpJson = $this->padLiteral($tmpJson); |
|
27
|
|
|
$tmpJson = $this->padObject($tmpJson); |
|
28
|
|
|
|
|
29
|
|
|
return $this->padStack($tmpJson); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function padLiteral($tmpJson) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->inStr) { |
|
35
|
|
|
return $tmpJson; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$match = \preg_match('/(tr?u?e?|fa?l?s?e|nu?l?l?)$/', $tmpJson, $matches); |
|
39
|
|
|
|
|
40
|
|
|
if (!$match || null === $literal = $this->maybeLiteral($matches[1])) { |
|
|
|
|
|
|
41
|
|
|
return $tmpJson; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return \substr($tmpJson, 0, 0 - \strlen($matches[1])) . $literal; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function padStack($tmpJson) |
|
48
|
|
|
{ |
|
49
|
|
|
foreach (\array_reverse($this->stack, true) as $index => $token) { |
|
50
|
|
|
if (isset($this->pairs[$token])) { |
|
51
|
|
|
$tmpJson .= $this->pairs[$token]; |
|
52
|
|
|
} elseif (\in_array($token, [':', ','])) { |
|
53
|
|
|
$tmpJson .= $this->padValue($tmpJson, $token, $index); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $tmpJson; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function padObject($tmpJson) |
|
61
|
|
|
{ |
|
62
|
|
|
$empty = \substr($tmpJson, -1) == '{' && !$this->inStr; |
|
63
|
|
|
|
|
64
|
|
|
if ($empty || $this->arrayPos > $this->objectPos) { |
|
65
|
|
|
return $tmpJson; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$part = \substr($tmpJson, $this->objectPos + 1); |
|
69
|
|
|
|
|
70
|
|
|
if (\preg_match('/(,)?(\"[^"]+\"(\s*:\s*)?[^,]*)+$/', $part, $matches)) { |
|
71
|
|
|
return $tmpJson; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$tmpJson .= $this->inStr ? '":' : ':'; |
|
75
|
|
|
$tmpJson .= $this->missingValue; |
|
76
|
|
|
if ($this->lastToken() === '"') { |
|
77
|
|
|
\array_pop($this->stack); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $tmpJson; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function padValue($tmpJson, $token, $index) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
if ($token === ':' && !$this->inStr && \substr($tmpJson, -1) === ':') { |
|
86
|
|
|
return $this->missingValue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return ''; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function padString($string) |
|
93
|
|
|
{ |
|
94
|
|
|
$last = \substr($string, -1); |
|
95
|
|
|
$last2 = \substr($string, -2); |
|
96
|
|
|
|
|
97
|
|
|
if ($last2 === '\"' || $last !== '"') { |
|
98
|
|
|
return $string . '"'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|