From 379bed000d22304b2a363d08a36c4a656ddc155c Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Fri, 26 Feb 2016 14:58:42 +0900 Subject: [PATCH 1/2] Fix JSON encoding to be fully XE-compatible --- classes/display/JSONDisplayHandler.php | 37 +++++++++++++++++++------- common/js/xml_handler.js | 6 +---- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/classes/display/JSONDisplayHandler.php b/classes/display/JSONDisplayHandler.php index 069b6464e..3be23d3d7 100644 --- a/classes/display/JSONDisplayHandler.php +++ b/classes/display/JSONDisplayHandler.php @@ -15,20 +15,37 @@ class JSONDisplayHandler $variables['error'] = $oModule->getError(); $variables['message'] = $oModule->getMessage(); - $temp = array(); - foreach ($variables as $key => $value) + self::_convertCompat($variables, Context::getRequestMethod()); + return json_encode($variables); + } + + /** + * Convert arrays in a format that is compatible with XE. + * + * @param array $array + * @param string $compat_type + * @return array + */ + protected static function _convertCompat(&$array, $compat_type = 'JSON') + { + foreach ($array as $key => &$value) { - if (self::_isNumericArray($value)) + if (is_array($value)) { - $temp[$key] = array_values($value); - } - else - { - $temp[$key] = $value; + self::_convertCompat($value, $compat_type); + if (self::_isNumericArray($value)) + { + if ($compat_type === 'XMLRPC') + { + $value = array('item' => array_values($value)); + } + else + { + $value = array_values($value); + } + } } } - - return json_encode($temp); } /** diff --git a/common/js/xml_handler.js b/common/js/xml_handler.js index c5a54d6d5..0b4f03775 100644 --- a/common/js/xml_handler.js +++ b/common/js/xml_handler.js @@ -69,11 +69,7 @@ var result = {}; $.each(data, function(key, val) { if ($.inArray(key, ["error", "message", "act", "redirect_url"]) >= 0 || $.inArray(key, return_fields) >= 0) { - if ($.isArray(val)) { - result[key] = { item: val }; - } else { - result[key] = val; - } + result[key] = val; } }); From 705ad34c49bb4f3efa18bbb7fc47780bc2d35046 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sat, 27 Feb 2016 14:27:46 +0900 Subject: [PATCH 2/2] Also fix JSON encoding when the result contains objects not arrays --- classes/display/JSONDisplayHandler.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classes/display/JSONDisplayHandler.php b/classes/display/JSONDisplayHandler.php index 3be23d3d7..7e7fc61a1 100644 --- a/classes/display/JSONDisplayHandler.php +++ b/classes/display/JSONDisplayHandler.php @@ -30,6 +30,10 @@ class JSONDisplayHandler { foreach ($array as $key => &$value) { + if (is_object($value)) + { + $value = get_object_vars($value); + } if (is_array($value)) { self::_convertCompat($value, $compat_type);