Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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