8000 [Serializer] Add options to JsonDecode and JsonEncode to wrap/unwrap json data by nonanerz · Pull Request #28887 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add options to JsonDecode and JsonEncode to wrap/unwrap json data #28887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add options to JsonDecode and JsonEncode to wrap/unwrap json data
Add options to JsonDecode and JsonEncode to wrap/unwrap json data

Add options to JsonDecode and JsonEncode to wrap/unwrap json data

Add options to JsonDecode and JsonEncode to wrap/unwrap json data

Add options to JsonDecode and JsonEncode to wrap/unwrap json data

Add options to JsonDecode and JsonEncode to wrap/unwrap json data
  • Loading branch information
nonanerz committed Oct 16, 2018
commit 1887e34ccbd8dc8542edcd9dd2f2b1ca837e3b6d
16 changes: 15 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(bool $associative = false, int $depth = 512)
*
* @param string $data The encoded JSON string to decode
* @param string $format Must be set to JsonEncoder::FORMAT
* @param array $context An optional set of options for the JSON decoder; see below
* @param array $context an optional set of options for the JSON decoder; see below
*
* The $context array is a simple key=>value array, with the following supported keys:
*
Expand All @@ -58,6 +58,9 @@ public function __construct(bool $associative = false, int $depth = 512)
* json_decode_options: integer
* Specifies additional options as per documentation for json_decode.
*
* json_root_key: string
* Specifies root key and allow to unwrap data
*
* @return mixed
*
* @throws NotEncodableValueException
Expand All @@ -78,6 +81,16 @@ public function decode($data, $format, array $context = array())
throw new NotEncodableValueException(json_last_error_msg());
}

if ($rootKey = $context['json_root_key']) {
if (\is_array($decodedData) && array_key_exists($rootKey, $decodedData)) {
$decodedData = $decodedData[$rootKey];
} elseif (\is_object($decodedData) && property_exists($decodedData, $rootKey)) {
$decodedData = $decodedData->$rootKey;
} else {
$decodedData = null;
}
}

return $decodedData;
}

Expand All @@ -100,6 +113,7 @@ private function resolveContext(array $context)
'json_decode_associative' => $this->associative,
'json_decode_recursion_depth' => $this->recursionDepth,
'json_decode_options' => 0,
'json_root_key' => null,
);

return array_merge($defaultOptions, $context);
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function encode($data, $format, array $context = array())
{
$context = $this->resolveContext($context);

if (($rootKey = $context['json_root_key'])) {
$data = array($rootKey => $data);
}

$encodedJson = json_encode($data, $context['json_encode_options']);

if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
Expand All @@ -60,6 +64,6 @@ public function supportsEncoding($format)
*/
private function resolveContext(array $context = array())
{
return array_merge(array('json_encode_options' => $this->options), $context);
return array_merge(array('json_encode_options' => $this->options, 'json_root_key' => null), $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function decodeProvider()
return array(
array('{"foo": "bar"}', $stdClass, array()),
array('{"foo": "bar"}', $assoc, array('json_decode_associative' => true)),
array('{"baz": {"foo": "bar"}}', $stdClass, array('json_root_key' => 'baz')),
array('{"baz": {"foo": "bar"}}', $assoc, array('json_root_key' => 'baz', 'json_decode_associative' => true)),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function encodeProvider()
return array(
array(array(), '[]', array()),
array(array(), '{}', array('json_encode_options' => JSON_FORCE_OBJECT)),
array(array('bar' => 'foo'), '{"baz":{"bar":"foo"}}', array('json_root_key' => 'baz')),
);
}

Expand Down
0