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
Prev Previous commit
Next Next commit
resolve conflict
  • Loading branch information
nonanerz committed Nov 11, 2018
commit 6d75878ec37a0a54bec887d7af20e7678e2118a3
3 changes: 2 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ public function decode($data, $format, array $context = array())
$associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE];
$recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH];
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
$propertyPath = $context[JsonEncoder::JSON_PROPERTY_PATH] ?? $this->defaultContext[JsonEncoder::JSON_PROPERTY_PATH];

$decodedData = json_decode($data, $associative, $recursionDepth, $options);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new NotEncodableValueException(json_last_error_msg());
}

if ($propertyPath = $context[JsonEncoder::JSON_PROPERTY_PATH]) {
if ($propertyPath) {
if ($this->propertyAccessor->isReadable($decodedData, $propertyPath)) {
$decodedData = $this->propertyAccessor->getValue($decodedData, $propertyPath);
} else {
Expand Down
27 changes: 21 additions & 6 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class JsonEncode implements EncoderInterface
private $propertyAccessor;
private $defaultContext = array(
self::OPTIONS => 0,
JsonEncoder::JSON_PROPERTY_PATH => null
JsonEncoder::JSON_PROPERTY_PATH => null,
);

/**
Expand All @@ -52,22 +52,37 @@ public function __construct($defaultContext = array())
public function encode($data, $format, array $context = array())
{
$jsonEncodeOptions = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
$encodedJson = json_encode($data, $jsonEncodeOptions);
$propertyPath = $context[JsonEncoder::JSON_PROPERTY_PATH] ?? $this->defaultContext[JsonEncoder::JSON_PROPERTY_PATH];

if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($jsonEncodeOptions & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
if ($propertyPath = $context[JsonEncoder::JSON_PROPERTY_PATH]) {
if ($propertyPath) {
$data = $this->wrapEncodableData($propertyPath, $data);
}

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

if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($context['json_encode_options'] & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($jsonEncodeOptions & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
throw new NotEncodableValueException(json_last_error_msg());
}

return $encodedJson;
}

/**
* Wrap data before encoding.
*
* @param string $propertyPath
* @param mixed $data
*
* @return array
*/
private function wrapEncodableData($propertyPath, $data)
{
$wrappedData = array();
$this->propertyAccessor->setValue($wrappedData, $propertyPath, $data);

return $wrappedData;
}

/**
* {@inheritdoc}
*/
Expand Down
0