From 477bb0dc0c979b4b44d25af623eeb640afe253ee Mon Sep 17 00:00:00 2001 From: Hauke Henningsen Date: Mon, 16 Mar 2015 03:22:56 +0100 Subject: [PATCH 1/2] Add option for exporting pretty-printed JSON This adds a single option to the JSON export plugin, which, if available (PHP >= 5.4.0) corresponds to passing `JSON_PRETTY_PRINT` to `json_encode`. Signed-off-by: Hauke Henningsen --- export.php | 1 + libraries/config.default.php | 7 +++++++ libraries/plugins/export/ExportJson.class.php | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/export.php b/export.php index 50ceb500ac..397b5ee5de 100644 --- a/export.php +++ b/export.php @@ -86,6 +86,7 @@ if (!defined('TESTSUITE')) { 'ods_structure_or_data', 'ods_columns', 'json_structure_or_data', + 'json_pretty_print', 'xml_structure_or_data', 'xml_export_events', 'xml_export_functions', diff --git a/libraries/config.default.php b/libraries/config.default.php index e2db43abde..a6c033eb55 100644 --- a/libraries/config.default.php +++ b/libraries/config.default.php @@ -1801,6 +1801,13 @@ $cfg['Export']['phparray_structure_or_data'] = 'data'; */ $cfg['Export']['json_structure_or_data'] = 'data'; +/** + * Export functions + * + * @global string $cfg['Export']['json_pretty_print'] + */ +$cfg['Export']['json_pretty_print'] = false; + /** * * diff --git a/libraries/plugins/export/ExportJson.class.php b/libraries/plugins/export/ExportJson.class.php index d659e3fef6..1ca7633bab 100644 --- a/libraries/plugins/export/ExportJson.class.php +++ b/libraries/plugins/export/ExportJson.class.php @@ -41,6 +41,7 @@ class ExportJson extends ExportPlugin include_once "$props/options/groups/OptionsPropertyRootGroup.class.php"; include_once "$props/options/groups/OptionsPropertyMainGroup.class.php"; include_once "$props/options/items/HiddenPropertyItem.class.php"; + include_once "$props/options/items/BoolPropertyItem.class.php"; $exportPluginProperties = new ExportPluginProperties(); $exportPluginProperties->setText('JSON'); @@ -61,6 +62,15 @@ class ExportJson extends ExportPlugin $leaf = new HiddenPropertyItem(); $leaf->setName("structure_or_data"); $generalOptions->addProperty($leaf); + + // JSON_PRETTY_PRINT is available since 5.4.0 + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $leaf = new BoolPropertyItem(); + $leaf->setName('pretty_print'); + $leaf->setText(__('Output pretty-printed JSON (Use human-readable formatting)')); + $generalOptions->addProperty($leaf); + } + // add the main group to the root group $exportSpecificOptions->addProperty($generalOptions); @@ -196,8 +206,14 @@ class ExportJson extends ExportPlugin for ($i = 0; $i < $columns_cnt; $i++) { $data[$columns[$i]] = $record[$i]; } + + if (isset($GLOBALS['json_pretty_print']) && $GLOBALS['json_pretty_print']) { + $encoded = json_encode($data, JSON_PRETTY_PRINT); + } else { + $encoded = json_encode($data); + } - if (! PMA_exportOutputHandler(json_encode($data))) { + if (! PMA_exportOutputHandler($encoded)) { return false; } } From b350af3c7f1b610ad131bed924a4f649429a2c7a Mon Sep 17 00:00:00 2001 From: Hauke Henningsen Date: Mon, 16 Mar 2015 03:28:32 +0100 Subject: [PATCH 2/2] Strip trailing spaces Signed-off-by: Hauke Henningsen --- libraries/plugins/export/ExportJson.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/plugins/export/ExportJson.class.php b/libraries/plugins/export/ExportJson.class.php index 1ca7633bab..68c386adbf 100644 --- a/libraries/plugins/export/ExportJson.class.php +++ b/libraries/plugins/export/ExportJson.class.php @@ -62,7 +62,7 @@ class ExportJson extends ExportPlugin $leaf = new HiddenPropertyItem(); $leaf->setName("structure_or_data"); $generalOptions->addProperty($leaf); - + // JSON_PRETTY_PRINT is available since 5.4.0 if (version_compare(PHP_VERSION, '5.4.0', '>=')) { $leaf = new BoolPropertyItem(); @@ -70,7 +70,7 @@ class ExportJson extends ExportPlugin $leaf->setText(__('Output pretty-printed JSON (Use human-readable formatting)')); $generalOptions->addProperty($leaf); } - + // add the main group to the root group $exportSpecificOptions->addProperty($generalOptions); @@ -206,7 +206,7 @@ class ExportJson extends ExportPlugin for ($i = 0; $i < $columns_cnt; $i++) { $data[$columns[$i]] = $record[$i]; } - + if (isset($GLOBALS['json_pretty_print']) && $GLOBALS['json_pretty_print']) { $encoded = json_encode($data, JSON_PRETTY_PRINT); } else {