10000 [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
Merge remote-tracking branch 'upstream/master' into add_option_to_jso…
…n_decoder

# Conflicts:
#	src/Symfony/Component/Serializer/Encoder/JsonDecode.php
#	src/Symfony/Component/Serializer/Encoder/JsonEncode.php
  • Loading branch information
nonanerz committed Nov 11, 2018
commit 18666ca63a6e4dbf454e44a7d8f8e7597d23875a
51 changes: 29 additions & 22 deletions src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,44 @@
class JsonDecode implements DecoderInterface
{
protected $serializer;

private $associative;
private $recursionDepth;
private $propertyAccessor;

/**
* True to return the result as an associative array, false for a nested stdClass hierarchy.
*/
const ASSOCIATIVE = 'json_decode_associative';

const OPTIONS = 'json_decode_options';

/**
* Specifies the recursion depth.
*/
const RECURSION_DEPTH = 'json_decode_recursion_depth';

private $defaultContext = array(
self::ASSOCIATIVE => false,
self::OPTIONS => 0,
self::RECURSION_DEPTH => 512,
JsonEncoder::JSON_PROPERTY_PATH => null,
);

/**
* Constructs a new JsonDecode instance.
*
* @param array $defaultContext
*/
public function __construct($defaultContext = array(), int $depth = 512)
{
$this->associative = $associative;
$this->recursionDepth = $depth;
if (!\is_array($defaultContext)) {
@trigger_error(sprintf('Using constructor parameters that are not a default context is deprecated since Symfony 4.2, use the "%s" and "%s" keys of the context instead.', self::ASSOCIATIVE, self::RECURSION_DEPTH), E_USER_DEPRECATED);

$defaultContext = array(
self::ASSOCIATIVE => (bool) $defaultContext,
self::RECURSION_DEPTH => $depth,
);
}

$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}

Expand Down Expand Up @@ -99,21 +123,4 @@ public function supportsDecoding($format)
{
return JsonEncoder::FORMAT === $format;
}

/**
* Merges the default options of the Json Decoder with the passed context.
*
* @return array
*/
private function resolveContext(array $context)
{
$defaultOptions = array(
'json_decode_associative' => $this->associative,
'json_decode_recursion_depth' => $this->recursionDepth,
'json_decode_options' => 0,
JsonEncoder::JSON_PROPERTY_PATH => null,
);

return array_merge($defaultOptions, $context);
}
}
41 changes: 11 additions & 30 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@
*/
class JsonEncode implements EncoderInterface
{
private $options;
private $propertyAccessor;
const OPTIONS = 'json_encode_options';

private $propertyAccessor;
private $defaultContext = array(
self::OPTIONS => 0,
JsonEncoder::JSON_PROPERTY_PATH => null
);

/**
* @param array $defaultContext
*/
public function __construct($defaultContext = array())
{
$this->options = $bitmask;
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
if (!\is_array($defaultContext)) {
@trigger_error(sprintf('Passing an integer as first parameter of the "%s()" method is deprecated since Symfony 4.2, use the "json_encode_options" key of the context instead.', __METHOD__), E_USER_DEPRECATED);

$this->defaultContext[self::OPTIONS] = (int) $defaultContext;
} else {
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
}

/**
Expand All @@ -47,6 +54,7 @@ public function encode($data, $format, array $context = array())
$jsonEncodeOptions = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
$encodedJson = json_encode($data, $jsonEncodeOptions);

if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($jsonEncodeOptions & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
if ($propertyPath = $context[JsonEncoder::JSON_PROPERTY_PATH]) {
$data = $this->wrapEncodableData($propertyPath, $data);
}
Expand All @@ -67,31 +75,4 @@ public function supportsEncoding($format)
{
return JsonEncoder::FORMAT === $format;
}

/**
* Merge default json encode options with context.
*
* @return array
*/
private function resolveContext(array $context = array())
{
return array_merge(array('json_encode_options' => $this->options, JsonEncoder::JSON_PROPERTY_PATH => null), $context);
}

/**
* 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;
}
}
You are viewing a condensed version of this merge commit. You can view the full changes here.
0