1111*
1212* Author: Raghuveer Dendukuri
1313*
14- * Version: 1.0.3
14+ * Version: 1.0.2
1515*
1616* Description: This is to do json_decode operation only on valid json string and in a highly performing way.
1717*
@@ -28,85 +28,44 @@ class JSONDecode {
2828 public function isJson ($ value ) {
2929 try {
3030
31- /**
32- * A non-string value is never a Valid JSON string.
33- */
34- if ($ this ->isInputString ($ value ) === false ) {
35-
36- return false ;
37-
38- }
39-
40- /**
41- * A numeric string in double quotes that is encapsulated in single quotes is a valid JSON string. Anything else is never a Valid JSON string.
42- */
43- if ($ this ->isInputNumber ($ value ) === false ) {
44-
31+ if (!is_string ($ value )) {
32+ // A non-string value is never a Valid JSON string.
4533 return false ;
4634
47- }
48-
49- /**
50- * Any non-numeric JSON string must be longer than 2 characters, or else, it cannot be considered for next level of JSON string validation checks.
51- */
52- if ($ this ->isInputLength ($ value ) === false ) {
35+ } else if (is_numeric ($ value )) {
36+ // A numeric string in double quotes that is encapsulated in single quotes is a valid JSON string. Anything else is never a Valid JSON string.
37+ return false ;
5338
39+ } else if (mb_strlen ($ value ) < 2 ) {
40+ // Any non-numeric JSON string must be longer than 2 characters, or else, it cannot be considered for next level of JSON string validation checks.
5441 return false ;
5542
56- }
57-
58- /**
59- * "null" is never a valid JSON string.
60- */
61- if ($ this ->isInputNull ($ value ) === false ) {
62-
43+ } else if ('null ' === $ value ) {
44+ // "null" is never a valid JSON string.
6345 return false ;
6446
65- }
66-
67- /**
68- * "true" is never a valid JSON string.
69- */
70- if ($ this ->isInputTextTrue ($ value ) === false ) {
71-
47+ } else if ('true ' === $ value ) {
48+ // "true" is never a valid JSON string.
7249 return false ;
7350
74- }
75-
76- /**
77- * "false" is never a valid JSON string.
78- */
79- if ($ this ->isInputTextFalse ($ value ) === false ) {
80-
51+ } else if ('false ' === $ value ) {
52+ // "false" is never a valid JSON string.
8153 return false ;
8254
83- }
84-
85- /**
86- * Every JSON string has to be wrapped in {}, [] or "", if it is not, then that is never a Valid JSON string.
87- */
88- if ($ this ->doesInputHasJsonSymbols ($ value ) === false ) {
89-
55+ } else if ( '{ ' != $ value [0 ] && '[ ' != $ value [0 ] && '" ' != $ value [0 ] ) {
56+ // Every JSON string has to be wrapped in {}, [] or "", if it is not, then that is never a Valid JSON string.
9057 return false ;
9158
92- }
93-
94- /**
95- * Actual JSON Decode operation using php's inbuilt json_decode function.
96- */
97- $ json = $ this ->jsonDecodeData ($ value , false );
98-
99- if (($ json !== false ) && ($ json !== null ) && ($ value != $ json )) {
100-
101- return true ;
59+ } else {
60+ //Actual JSON Decode operation using php's inbuilt json_decode function.
61+ $ json = json_decode ($ value );
62+ return $ json !== false && !is_null ($ json ) && $ value != $ json ;
10263
10364 }
10465
105- return false ;
106-
10766 } catch (JSONDecodeException $ e ) {
10867
109- echo htmlspecialchars ( "\n JSONDecodeException - " , $ e ->getMessage (), (int )$ e ->getCode (), ENT_QUOTES );
68+ echo "\n JSONDecodeException - " , $ e ->getMessage (), (int )$ e ->getCode ();
11069
11170 }
11271
@@ -120,180 +79,90 @@ public function getJsonDecodedData($value, bool $asArray) {
12079 try {
12180
12281 $ response = [];
123-
124- $ response [ " is_json " ] = false ;
125- $ response [ " content " ] = "" ;
82+
83+ if (! is_string ( $ value )) {
84+ // A non-string value is never a Valid JSON string.
12685
127- /**
128- * A non-string value is never a Valid JSON string.
129- */
130- if ($ this ->isInputString ($ value ) === false ) {
86+ $ response ["is_json " ] = false ;
87+ $ response ["content " ] = "" ;
13188
13289 return $ response ;
13390
134- }
135-
136- /**
137- * A numeric string in double quotes that is encapsulated in single quotes is a valid JSON string. Anything else is never a Valid JSON string.
138- */
139- if ($ this ->isInputNumber ($ value ) === false ) {
91+ } else if (is_numeric ($ value )) {
92+ // A numeric string in double quotes that is encapsulated in single quotes is a valid JSON string. Anything else is never a Valid JSON string.
93+ $ response ["is_json " ] = false ;
94+ $ response ["content " ] = "" ;
14095
14196 return $ response ;
14297
143- }
144-
145- /**
146- * Any non-numeric JSON string must be longer than 2 characters, or else, it cannot be considered for next level of JSON string validation checks.
147- */
148- if ($ this ->isInputLength ($ value ) === false ) {
98+ } else if (mb_strlen ($ value ) < 2 ) {
99+ // Any non-numeric JSON string must be longer than 2 characters, or else, it cannot be considered for next level of JSON string validation checks.
100+ $ response ["is_json " ] = false ;
101+ $ response ["content " ] = "" ;
149102
150103 return $ response ;
151104
152- }
153-
154- /**
155- * "null" is never a valid JSON string.
156- */
157- if ($ this ->isInputNull ($ value ) === false ) {
105+ } else if ('null ' === $ value ) {
106+ // "null" is never a valid JSON string.
107+ $ response ["is_json " ] = false ;
108+ $ response ["content " ] = "" ;
158109
159110 return $ response ;
160111
161- }
162-
163- /**
164- * "true" is never a valid JSON string.
165- */
166- if ($ this ->isInputTextTrue ($ value ) === false ) {
112+ } else if ('true ' === $ value ) {
113+ // "true" is never a valid JSON string.
114+ $ response ["is_json " ] = false ;
115+ $ response ["content " ] = "" ;
167116
168117 return $ response ;
169118
170- }
171-
172- /**
173- * "false" is never a valid JSON string.
174- */
175- if ($ this ->isInputTextFalse ($ value ) === false ) {
176-
177- return $ response ;
178-
179- }
180-
181- /**
182- * Every JSON string has to be wrapped in {}, [] or "", if it is not, then that is never a Valid JSON string.
183- */
184- if ($ this ->doesInputHasJsonSymbols ($ value ) === false ) {
119+ } else if ('false ' === $ value ) {
120+ // "false" is never a valid JSON string.
121+ $ response ["is_json " ] = false ;
122+ $ response ["content " ] = "" ;
185123
186124 return $ response ;
187125
188- }
189-
190- /**
191- * Actual JSON Decode operation using php's inbuilt json_decode function.
192- */
193- $ json = $ this ->jsonDecodeData ($ value , $ asArray );
194-
195- if (($ json !== false ) && ($ json !== null ) && ($ value != $ json )) {
196-
197- $ response ["is_json " ] = true ;
198- $ response ["content " ] = $ json ;
199-
200- return $ response ;
201-
202- }
203-
204- return $ response ;
205-
206- } catch (JSONDecodeException $ e ) {
207-
208- echo htmlspecialchars ("\n JSONDecodeException - " , $ e ->getMessage (), (int )$ e ->getCode (), ENT_QUOTES );
209-
210- }
211-
212- }
213-
214- /**
215- * Checks if input value is a string
216- */
217- private function isInputString ($ jsonString ) : bool
218- {
219- return is_string ($ jsonString ) ? true : false ;
220- }
221-
222- /**
223- * Checks if input value is a Number/Numeric String
224- */
225- private function isInputNumber ($ jsonString ) : bool
226- {
227- return is_numeric ($ jsonString ) ? true : false ;
228- }
229-
230- /**
231- * Checks if input value length
232- */
233- private function isInputLength ($ jsonString ) : bool
234- {
235- return mb_strlen ($ jsonString ) < 2 ? true : false ;
236- }
237-
238- /**
239- * Checks if input value is NULL
240- */
241- private function isInputNull ($ jsonString ) : bool
242- {
243- return 'null ' === $ $ jsonString ? true : false ;
244- }
245-
246- /**
247- * Checks if input value is string true
248- */
249- private function isInputTextTrue ($ jsonString ) : bool
250- {
251- return 'true ' === $ jsonString ? true : false ;
252- }
253-
254- /**
255- * Checks if input value is string false
256- */
257- private function isInputTextFalse ($ jsonString ) : bool
258- {
259- return 'false ' === $ jsonString ? true : false ;
260- }
261-
262-
263- /**
264- * Checks if input value has JSON Starting and Ending Symbols
265- */
266- private function doesInputHasJsonSymbols ($ jsonString ) : bool
267- {
268- return '{ ' != $ jsonString [0 ] && '[ ' != $ jsonString [0 ] && '" ' != $ jsonString [0 ] ? true : false ;
269- }
270-
271- /**
272- * Returns JSON Decoded data
273- */
274- private function jsonDecodeData ($ jsonString , bool $ asArray ) {
275-
276- try {
277-
278- if ($ asArray === true ) {
279-
280- $ response = json_decode ($ jsonString , true );
281-
282- return $ response ;
283-
284- }
285-
286- if ($ asArray === false ) {
287-
288- $ response = json_decode ($ jsonString );
126+ } else if ( '{ ' != $ value [0 ] && '[ ' != $ value [0 ] && '" ' != $ value [0 ] ) {
127+ // Every JSON string has to be wrapped in {}, [] or "", if it is not, then that is never a Valid JSON string.
128+ $ response ["is_json " ] = false ;
129+ $ response ["content " ] = "" ;
289130
290131 return $ response ;
291132
133+ } else {
134+ //Actual JSON Decode operation using php's inbuilt json_decode function.
135+
136+ if ($ asArray === true ) {
137+
138+ $ json = json_decode ($ value , true );
139+
140+ } else {
141+
142+ $ json = json_decode ($ value );
143+
144+ }
145+
146+ if (($ json !== false ) && (!is_null ($ json )) && ($ value != $ json )) {
147+
148+ $ response ["is_json " ] = true ;
149+ $ response ["content " ] = $ json ;
150+
151+ return $ response ;
152+
153+ } else {
154+
155+ $ response ["is_json " ] = false ;
156+ $ response ["content " ] = "" ;
157+
158+ return $ response ;
159+
160+ }
292161 }
293162
294163 } catch (JSONDecodeException $ e ) {
295164
296- echo htmlspecialchars ( "\n JSONDecodeException - " , $ e ->getMessage (), (int )$ e ->getCode (), ENT_QUOTES );
165+ echo "\n JSONDecodeException - " , $ e ->getMessage (), (int )$ e ->getCode ();
297166
298167 }
299168
0 commit comments