Update json.format.php

This commit is contained in:
Namhyeon Go 2020-01-27 17:04:12 +09:00 committed by GitHub
parent c8b0448d4b
commit 93e8922c4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
<?php <?php
// @date 2019-10-13 // @created_on 2019-10-13
// @updated_on 2020-01-27
// @author Go Namhyeon <gnh1201@gmail.com> // @author Go Namhyeon <gnh1201@gmail.com>
if(!check_function_exists("json_decode_ex")) { if(!check_function_exists("json_decode_ex")) {
@ -13,13 +14,15 @@ if(!check_function_exists("json_decode_ex")) {
"NO_FUNCTION_JSON_LAST_ERROR" => "json_last_error", "NO_FUNCTION_JSON_LAST_ERROR" => "json_last_error",
); );
$error = check_invalid_function($invalid_fn); $error = check_invalid_function($invalid_fn);
if($error == JSON_ERROR_NONE) {
if($error < 0) {
if($is_assoc) { if($is_assoc) {
$result = json_decode($data, true); $result = json_decode($data, true);
} else { } else {
$result = json_decode($data); $result = json_decode($data);
} }
} else {
$result = new stdClass();
$result->error = $error;
} }
return $result; return $result;
@ -31,7 +34,14 @@ if(!check_function_exists("json_encode_ex")) {
$result = false; $result = false;
$is_adaptive = array_key_equals("adaptive", $options, true); $is_adaptive = array_key_equals("adaptive", $options, true);
$is_pretty = array_key_equals("pretty", $options, true);
$invalid_fn = array(
"NO_FUNCTION_JSON_ENCODE" => "json_decode",
"NO_FUNCTION_JSON_LAST_ERROR" => "json_last_error",
);
$error = check_invalid_function($invalid_fn);
if($error == JSON_ERROR_NONE) {
if($is_adaptive) { if($is_adaptive) {
// 2018-06-01: Adaptive JSON is always quotes without escape non-ascii characters // 2018-06-01: Adaptive JSON is always quotes without escape non-ascii characters
$lines = array(); $lines = array();
@ -43,9 +53,16 @@ if(!check_function_exists("json_encode_ex")) {
} }
} }
$result = "{" . implode(",", $lines) . "}"; $result = "{" . implode(",", $lines) . "}";
} else {
if($is_pretty) {
$result = json_encode($data, JSON_PRETTY_PRINT);
} else { } else {
$result = json_encode($data); $result = json_encode($data);
} }
}
} else {
$result = sprintf("{\"error\": \"%s\"}", $error);
}
return $result; return $result;
} }