Skip to content

Commit f6be0ab

Browse files
authored
Merge pull request #3288 from devorama/devorama-formatter-json-patch1
add JSON_NUMERIC_CHECK to json encode options
2 parents 31c7d41 + 6330be5 commit f6be0ab

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

app/Config/Format.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,21 @@ class Format extends BaseConfig
3939
'application/xml' => \CodeIgniter\Format\XMLFormatter::class,
4040
'text/xml' => \CodeIgniter\Format\XMLFormatter::class,
4141
];
42-
42+
43+
/*
44+
|--------------------------------------------------------------------------
45+
| Formatters Options
46+
|--------------------------------------------------------------------------
47+
|
48+
| Additional Options to adjust default formatters behaviour.
49+
| For each mime type, list the additional options that should be used.
50+
|
51+
*/
52+
public $formatterOptions = [
53+
'application/json' => JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
54+
'application/xml' => 0,
55+
'text/xml' => 0,
56+
];
4357
//--------------------------------------------------------------------
4458

4559
/**

system/Format/JSONFormatter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
namespace CodeIgniter\Format;
4141

4242
use CodeIgniter\Format\Exceptions\FormatException;
43+
use Config\Format;
4344

4445
/**
4546
* JSON data formatter
@@ -56,7 +57,10 @@ class JSONFormatter implements FormatterInterface
5657
*/
5758
public function format($data)
5859
{
59-
$options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR;
60+
$config = new Format();
61+
62+
$options = $config->formatterOptions['application/json'] ?? JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
63+
$options = $options | JSON_PARTIAL_OUTPUT_ON_ERROR;
6064

6165
$options = ENVIRONMENT === 'production' ? $options : $options | JSON_PRETTY_PRINT;
6266

system/Format/XMLFormatter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
namespace CodeIgniter\Format;
4141

4242
use CodeIgniter\Format\Exceptions\FormatException;
43+
use Config\Format;
4344

4445
/**
4546
* XML data formatter
@@ -56,6 +57,8 @@ class XMLFormatter implements FormatterInterface
5657
*/
5758
public function format($data)
5859
{
60+
$config = new Format();
61+
5962
// SimpleXML is installed but default
6063
// but best to check, and then provide a fallback.
6164
if (! extension_loaded('simplexml'))
@@ -66,7 +69,8 @@ public function format($data)
6669
// @codeCoverageIgnoreEnd
6770
}
6871

69-
$output = new \SimpleXMLElement('<?xml version="1.0"?><response></response>');
72+
$options = $config->formatterOptions['application/xml'] ?? 0;
73+
$output = new \SimpleXMLElement('<?xml version="1.0"?><response></response>', $options);
7074

7175
$this->arrayToXML((array)$data, $output);
7276

0 commit comments

Comments
 (0)