2020-11-18 00:26:53 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Json
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Json;
|
|
|
|
|
|
2023-11-17 16:24:32 +00:00
|
|
|
use JsonException;
|
2020-11-18 00:26:53 +00:00
|
|
|
use JsonSerializable;
|
2023-12-20 19:58:45 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2022-07-07 20:32:08 +00:00
|
|
|
use stdClass;
|
2020-11-18 00:26:53 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-04 08:58:57 +00:00
|
|
|
* Helper class to serialize/deserialize things to/from JSON.
|
2020-11-18 00:26:53 +00:00
|
|
|
*
|
|
|
|
|
* @stable to type
|
|
|
|
|
* @since 1.36
|
|
|
|
|
* @package MediaWiki\Json
|
|
|
|
|
*/
|
2024-05-04 08:58:57 +00:00
|
|
|
class JsonCodec implements JsonDeserializer, JsonSerializer {
|
2020-11-18 00:26:53 +00:00
|
|
|
|
2024-05-04 08:58:57 +00:00
|
|
|
/** @deprecated since 1.43; use ::deserialize() */
|
|
|
|
|
public function unserialize( $json, ?string $expectedClass = null ) {
|
|
|
|
|
return $this->deserialize( $json, $expectedClass );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function deserialize( $json, string $expectedClass = null ) {
|
2023-01-26 12:29:00 +00:00
|
|
|
Assert::parameterType( [ 'stdClass', 'array', 'string' ], $json, '$json' );
|
2020-11-18 00:26:53 +00:00
|
|
|
Assert::precondition(
|
2024-05-04 08:58:57 +00:00
|
|
|
!$expectedClass || is_subclass_of( $expectedClass, JsonDeserializable::class ),
|
|
|
|
|
'$expectedClass parameter must be subclass of JsonDeserializable, got ' . $expectedClass
|
2020-11-18 00:26:53 +00:00
|
|
|
);
|
|
|
|
|
if ( is_string( $json ) ) {
|
|
|
|
|
$jsonStatus = FormatJson::parse( $json, FormatJson::FORCE_ASSOC );
|
|
|
|
|
if ( !$jsonStatus->isGood() ) {
|
2023-11-17 16:24:32 +00:00
|
|
|
throw new JsonException( "Bad JSON: {$jsonStatus}" );
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
$json = $jsonStatus->getValue();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 12:29:00 +00:00
|
|
|
if ( $json instanceof stdClass ) {
|
2020-11-18 00:26:53 +00:00
|
|
|
$json = (array)$json;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 20:32:08 +00:00
|
|
|
if ( $this->containsComplexValue( $json ) ) {
|
2024-05-04 08:58:57 +00:00
|
|
|
// Recursively deserialize the array values before deserializing
|
2022-07-07 20:32:08 +00:00
|
|
|
// the array itself.
|
2024-05-04 08:58:57 +00:00
|
|
|
$json = $this->deserializeArray( $json );
|
2022-07-07 20:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-18 00:26:53 +00:00
|
|
|
if ( !$this->canMakeNewFromValue( $json ) ) {
|
|
|
|
|
if ( $expectedClass ) {
|
2023-11-17 16:24:32 +00:00
|
|
|
throw new JsonException( 'JSON did not have ' . JsonConstants::TYPE_ANNOTATION );
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
return $json;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$class = $json[JsonConstants::TYPE_ANNOTATION];
|
2023-12-20 19:58:45 +00:00
|
|
|
if ( $class == "ParserOutput" || $class == "MediaWiki\\Parser\\ParserOutput" ) {
|
|
|
|
|
$class = ParserOutput::class; // T353835
|
|
|
|
|
} elseif ( $class !== stdClass::class &&
|
2024-05-04 08:58:57 +00:00
|
|
|
!( class_exists( $class ) && is_subclass_of( $class, JsonDeserializable::class ) )
|
2022-07-07 20:32:08 +00:00
|
|
|
) {
|
2023-11-17 16:24:32 +00:00
|
|
|
throw new JsonException( "Invalid target class {$class}" );
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 04:15:31 +00:00
|
|
|
if ( $expectedClass && $class !== $expectedClass && !is_subclass_of( $class, $expectedClass ) ) {
|
2023-11-17 16:24:32 +00:00
|
|
|
throw new JsonException(
|
2024-05-04 08:58:57 +00:00
|
|
|
"Refusing to deserialize: expected $expectedClass, got $class"
|
2021-06-09 04:15:31 +00:00
|
|
|
);
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
2024-05-15 02:02:07 +00:00
|
|
|
|
|
|
|
|
unset( $json[JsonConstants::TYPE_ANNOTATION] );
|
2022-07-07 20:32:08 +00:00
|
|
|
if ( $class === stdClass::class ) {
|
|
|
|
|
return (object)$json;
|
|
|
|
|
}
|
2021-06-09 04:15:31 +00:00
|
|
|
return $class::newFromJsonArray( $this, $json );
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-04 08:58:57 +00:00
|
|
|
/** @deprecated since 1.43; use ::deserializeArray() */
|
2021-07-22 03:11:47 +00:00
|
|
|
public function unserializeArray( array $array ): array {
|
2024-05-04 08:58:57 +00:00
|
|
|
return $this->deserializeArray( $array );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function deserializeArray( array $array ): array {
|
|
|
|
|
$deserializedExtensionData = [];
|
2020-11-18 00:26:53 +00:00
|
|
|
foreach ( $array as $key => $value ) {
|
2022-07-07 20:32:08 +00:00
|
|
|
if ( $key === JsonConstants::COMPLEX_ANNOTATION ) {
|
|
|
|
|
/* don't include this in the result */
|
|
|
|
|
} elseif (
|
|
|
|
|
$this->canMakeNewFromValue( $value ) ||
|
|
|
|
|
$this->containsComplexValue( $value )
|
|
|
|
|
) {
|
2024-05-04 08:58:57 +00:00
|
|
|
$deserializedExtensionData[$key] = $this->deserialize( $value );
|
2020-11-18 00:26:53 +00:00
|
|
|
} else {
|
2024-05-04 08:58:57 +00:00
|
|
|
$deserializedExtensionData[$key] = $value;
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-04 08:58:57 +00:00
|
|
|
return $deserializedExtensionData;
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-07 20:32:08 +00:00
|
|
|
private function serializeOne( &$value ) {
|
2020-11-18 00:26:53 +00:00
|
|
|
if ( $value instanceof JsonSerializable ) {
|
|
|
|
|
$value = $value->jsonSerialize();
|
2024-05-15 21:01:08 +00:00
|
|
|
if ( !is_array( $value ) ) {
|
|
|
|
|
// Although JsonSerializable doesn't /require/ the result to be
|
2024-05-04 08:58:57 +00:00
|
|
|
// an array, JsonCodec and JsonDeserializableTrait do.
|
2024-05-15 21:01:08 +00:00
|
|
|
throw new JsonException( "jsonSerialize didn't return array" );
|
|
|
|
|
}
|
2022-07-07 20:32:08 +00:00
|
|
|
$value[JsonConstants::COMPLEX_ANNOTATION] = true;
|
|
|
|
|
// The returned array may still have instance of JsonSerializable,
|
|
|
|
|
// stdClass, or array, so fall through to recursively handle these.
|
|
|
|
|
} elseif ( is_object( $value ) && get_class( $value ) === stdClass::class ) {
|
|
|
|
|
// T312589: if $value is stdObject, mark the type
|
2024-05-04 08:58:57 +00:00
|
|
|
// so we deserialize as stdObject as well.
|
2022-07-07 20:32:08 +00:00
|
|
|
$value = (array)$value;
|
|
|
|
|
$value[JsonConstants::TYPE_ANNOTATION] = stdClass::class;
|
|
|
|
|
$value[JsonConstants::COMPLEX_ANNOTATION] = true;
|
|
|
|
|
// Fall through to handle the property values
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
2022-07-07 20:32:08 +00:00
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
$is_complex = false;
|
|
|
|
|
// Recursively convert array values to serializable form
|
2023-02-04 21:16:31 +00:00
|
|
|
foreach ( $value as &$v ) {
|
2022-07-07 20:32:08 +00:00
|
|
|
if ( is_object( $v ) || is_array( $v ) ) {
|
|
|
|
|
$v = $this->serializeOne( $v );
|
|
|
|
|
if ( isset( $v[JsonConstants::COMPLEX_ANNOTATION] ) ) {
|
|
|
|
|
$is_complex = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $is_complex ) {
|
|
|
|
|
$value[JsonConstants::COMPLEX_ANNOTATION] = true;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( !is_scalar( $value ) && $value !== null ) {
|
2024-07-31 16:47:01 +00:00
|
|
|
$details = get_debug_type( $value );
|
2024-05-15 21:01:08 +00:00
|
|
|
throw new JsonException(
|
|
|
|
|
'Unable to serialize JSON: ' . $details
|
|
|
|
|
);
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
2022-07-07 20:32:08 +00:00
|
|
|
return $value;
|
|
|
|
|
}
|
2020-11-18 00:26:53 +00:00
|
|
|
|
2022-07-07 20:32:08 +00:00
|
|
|
public function serialize( $value ) {
|
2020-11-18 00:26:53 +00:00
|
|
|
// Detect if the array contained any properties non-serializable
|
2022-07-07 20:32:08 +00:00
|
|
|
// to json.
|
2020-11-18 00:26:53 +00:00
|
|
|
// TODO: make detectNonSerializableData not choke on cyclic structures.
|
2024-05-04 08:58:57 +00:00
|
|
|
$deserializablePath = $this->detectNonSerializableDataInternal(
|
2024-05-15 21:01:08 +00:00
|
|
|
$value, false, '$', false
|
2022-07-07 20:32:08 +00:00
|
|
|
);
|
2024-05-04 08:58:57 +00:00
|
|
|
if ( $deserializablePath ) {
|
2023-11-17 16:24:32 +00:00
|
|
|
throw new JsonException(
|
2024-05-04 08:58:57 +00:00
|
|
|
"Non-deserializable property set at {$deserializablePath}"
|
2020-11-18 00:26:53 +00:00
|
|
|
);
|
|
|
|
|
}
|
2022-07-07 20:32:08 +00:00
|
|
|
// Recursively convert stdClass and JsonSerializable
|
|
|
|
|
// to serializable arrays
|
|
|
|
|
$value = $this->serializeOne( $value );
|
|
|
|
|
// Format as JSON
|
|
|
|
|
$json = FormatJson::encode( $value, false, FormatJson::ALL_OK );
|
|
|
|
|
if ( !$json ) {
|
2024-05-15 21:01:08 +00:00
|
|
|
try {
|
|
|
|
|
// Try to collect more information on the failure.
|
|
|
|
|
$details = $this->detectNonSerializableData( $value );
|
|
|
|
|
} catch ( \Throwable $t ) {
|
|
|
|
|
$details = $t->getMessage();
|
|
|
|
|
}
|
2023-11-17 16:24:32 +00:00
|
|
|
throw new JsonException(
|
2024-05-15 21:01:08 +00:00
|
|
|
'Failed to encode JSON. ' .
|
|
|
|
|
'Error: ' . json_last_error_msg() . '. ' .
|
|
|
|
|
'Details: ' . $details
|
2022-07-07 20:32:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
2020-11-18 00:26:53 +00:00
|
|
|
|
|
|
|
|
return $json;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is it likely possible to make a new instance from $json serialization?
|
|
|
|
|
* @param mixed $json
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
private function canMakeNewFromValue( $json ): bool {
|
2020-11-18 00:26:53 +00:00
|
|
|
$classAnnotation = JsonConstants::TYPE_ANNOTATION;
|
|
|
|
|
if ( is_array( $json ) ) {
|
2022-07-07 20:32:08 +00:00
|
|
|
return array_key_exists( $classAnnotation, $json ) &&
|
|
|
|
|
# T313818: conflict with ParserOutput::detectAndEncodeBinary()
|
|
|
|
|
$json[$classAnnotation] !== 'string';
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_object( $json ) ) {
|
2022-07-07 20:32:08 +00:00
|
|
|
return property_exists( $json, $classAnnotation );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does this serialized array contain a complex value (a serialized class
|
|
|
|
|
* or an array which itself contains a serialized class)?
|
|
|
|
|
* @param mixed $json
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function containsComplexValue( $json ): bool {
|
|
|
|
|
if ( is_array( $json ) ) {
|
|
|
|
|
return array_key_exists( JsonConstants::COMPLEX_ANNOTATION, $json );
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recursive check for ability to serialize $value to JSON via FormatJson::encode().
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $value
|
2024-05-04 08:58:57 +00:00
|
|
|
* @param bool $expectDeserialize
|
2020-11-18 00:26:53 +00:00
|
|
|
* @param string $accumulatedPath
|
2024-05-15 21:01:08 +00:00
|
|
|
* @param bool $exhaustive Whether to (slowly) completely traverse the
|
|
|
|
|
* $value in order to find the precise location of a problem
|
2020-11-18 00:26:53 +00:00
|
|
|
* @return string|null JSON path to first encountered non-serializable property or null.
|
|
|
|
|
*/
|
|
|
|
|
private function detectNonSerializableDataInternal(
|
|
|
|
|
$value,
|
2024-05-04 08:58:57 +00:00
|
|
|
bool $expectDeserialize,
|
2024-05-15 21:01:08 +00:00
|
|
|
string $accumulatedPath,
|
|
|
|
|
bool $exhaustive = false
|
2021-07-22 03:11:47 +00:00
|
|
|
): ?string {
|
2022-07-07 20:32:08 +00:00
|
|
|
if (
|
|
|
|
|
$this->canMakeNewFromValue( $value ) ||
|
|
|
|
|
$this->containsComplexValue( $value )
|
|
|
|
|
) {
|
|
|
|
|
// Contains a conflicting use of JsonConstants::TYPE_ANNOTATION or
|
|
|
|
|
// JsonConstants::COMPLEX_ANNOTATION; in the future we might use
|
|
|
|
|
// an alternative encoding for these objects to allow them.
|
2024-05-15 21:01:08 +00:00
|
|
|
return $accumulatedPath . ': conflicting use of protected property';
|
|
|
|
|
}
|
|
|
|
|
if ( is_object( $value ) ) {
|
|
|
|
|
if ( get_class( $value ) === stdClass::class ) {
|
|
|
|
|
$value = (array)$value;
|
|
|
|
|
} elseif (
|
2024-05-04 08:58:57 +00:00
|
|
|
$expectDeserialize ?
|
|
|
|
|
$value instanceof JsonDeserializable :
|
2024-05-15 21:01:08 +00:00
|
|
|
$value instanceof JsonSerializable
|
|
|
|
|
) {
|
|
|
|
|
if ( $exhaustive ) {
|
|
|
|
|
// Call the appropriate serialization method and recurse to
|
|
|
|
|
// ensure contents are also serializable.
|
|
|
|
|
$value = $value->jsonSerialize();
|
|
|
|
|
if ( !is_array( $value ) ) {
|
|
|
|
|
return $accumulatedPath . ": jsonSerialize didn't return array";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Assume that serializable objects contain 100%
|
|
|
|
|
// serializable contents in their representation.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Instances of classes other the \stdClass or JsonSerializable can not be serialized to JSON.
|
|
|
|
|
return $accumulatedPath . ': ' . get_class( $value );
|
|
|
|
|
}
|
2022-07-07 20:32:08 +00:00
|
|
|
}
|
2024-05-15 21:01:08 +00:00
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
foreach ( $value as $key => $propValue ) {
|
2020-11-18 00:26:53 +00:00
|
|
|
$propValueNonSerializablePath = $this->detectNonSerializableDataInternal(
|
|
|
|
|
$propValue,
|
2024-05-04 08:58:57 +00:00
|
|
|
$expectDeserialize,
|
2024-05-15 21:01:08 +00:00
|
|
|
$accumulatedPath . '.' . $key,
|
|
|
|
|
$exhaustive
|
2020-11-18 00:26:53 +00:00
|
|
|
);
|
2024-05-15 21:01:08 +00:00
|
|
|
if ( $propValueNonSerializablePath !== null ) {
|
2020-11-18 00:26:53 +00:00
|
|
|
return $propValueNonSerializablePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ( !is_scalar( $value ) && $value !== null ) {
|
2024-07-31 16:56:55 +00:00
|
|
|
return $accumulatedPath . ': nonscalar ' . get_debug_type( $value );
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the $value is JSON-serializable (contains only scalar values)
|
|
|
|
|
* and returns a JSON-path to the first non-serializable property encountered.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $value
|
2024-05-04 08:58:57 +00:00
|
|
|
* @param bool $expectDeserialize whether to expect the $value to be deserializable with JsonDeserializer.
|
2020-11-18 00:26:53 +00:00
|
|
|
* @return string|null JSON path to first encountered non-serializable property or null.
|
2024-05-04 08:58:57 +00:00
|
|
|
* @see JsonDeserializer
|
2020-11-18 00:26:53 +00:00
|
|
|
* @since 1.36
|
|
|
|
|
*/
|
2024-05-04 08:58:57 +00:00
|
|
|
public function detectNonSerializableData( $value, bool $expectDeserialize = false ): ?string {
|
|
|
|
|
return $this->detectNonSerializableDataInternal( $value, $expectDeserialize, '$', true );
|
2020-11-18 00:26:53 +00:00
|
|
|
}
|
|
|
|
|
}
|