| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Json; |
| 23 |
|
| 24 |
use InvalidArgumentException; |
| 25 |
use JsonException; |
| 26 |
use JsonSerializable; |
| 27 |
use Psr\Container\ContainerInterface; |
| 28 |
use ReflectionClass; |
| 29 |
use stdClass; |
| 30 |
use Wikimedia\Assert\Assert; |
| 31 |
use Wikimedia\JsonCodec\JsonClassCodec; |
| 32 |
use Wikimedia\JsonCodec\JsonCodecable; |
| 33 |
use Wikimedia\Parsoid\Core\PageBundle; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class JsonCodec |
| 43 |
extends \Wikimedia\JsonCodec\JsonCodec |
| 44 |
implements JsonDeserializer, JsonSerializer |
| 45 |
{ |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private bool $backCompat = true; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( ?ContainerInterface $services = null ) { |
| 58 |
parent::__construct( $services ); |
| 59 |
|
| 60 |
|
| 61 |
$this->addCodecFor( PageBundle::class, new class implements JsonClassCodec { |
| 62 |
|
| 63 |
public function toJsonArray( $obj ): array { |
| 64 |
'@phan-var PageBundle $obj'; |
| 65 |
return [ |
| 66 |
'html' => $obj->html, |
| 67 |
'parsoid' => $obj->parsoid, |
| 68 |
'mw' => $obj->mw, |
| 69 |
'version' => $obj->version, |
| 70 |
'headers' => $obj->headers, |
| 71 |
'contentmodel' => $obj->contentmodel, |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
public function newFromJsonArray( string $className, array $json ) { |
| 77 |
|
| 78 |
return new PageBundle( |
| 79 |
$json['html'] ?? '', |
| 80 |
$json['parsoid'] ?? null, |
| 81 |
$json['mw'] ?? null, |
| 82 |
$json['version'] ?? null, |
| 83 |
$json['headers'] ?? null, |
| 84 |
$json['contentmodel'] ?? null |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
public function jsonClassHintFor( string $className, string $keyName ) { |
| 90 |
return null; |
| 91 |
} |
| 92 |
} ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
protected function codecFor( string $className ): ?JsonClassCodec { |
| 102 |
static $deserializableCodec = null; |
| 103 |
static $serializableCodec = null; |
| 104 |
$codec = parent::codecFor( $className ); |
| 105 |
if ( $codec !== null ) { |
| 106 |
return $codec; |
| 107 |
} |
| 108 |
|
| 109 |
$className = ( new ReflectionClass( $className ) )->getName(); |
| 110 |
|
| 111 |
if ( is_a( $className, JsonDeserializable::class, true ) ) { |
| 112 |
if ( $deserializableCodec === null ) { |
| 113 |
$deserializableCodec = new JsonDeserializableCodec( $this ); |
| 114 |
} |
| 115 |
$codec = $deserializableCodec; |
| 116 |
$this->addCodecFor( $className, $codec ); |
| 117 |
return $codec; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
if ( is_a( $className, JsonSerializable::class, true ) ) { |
| 122 |
$codec = JsonSerializableCodec::getInstance(); |
| 123 |
$this->addCodecFor( $className, $codec ); |
| 124 |
return $codec; |
| 125 |
} |
| 126 |
return null; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
protected function markArray( array &$value, string $className, ?string $classHint ): void { |
| 131 |
parent::markArray( $value, $className, $classHint ); |
| 132 |
|
| 133 |
if ( $this->backCompat ) { |
| 134 |
$value[JsonConstants::COMPLEX_ANNOTATION] = true; |
| 135 |
if ( ( $value[JsonConstants::TYPE_ANNOTATION] ?? null ) === 'array' ) { |
| 136 |
unset( $value[JsonConstants::TYPE_ANNOTATION] ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
protected function isArrayMarked( array $value ): bool { |
| 143 |
|
| 144 |
if ( $this->backCompat && array_key_exists( JsonConstants::COMPLEX_ANNOTATION, $value ) ) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
if ( ( $value['_type_'] ?? null ) === 'string' ) { |
| 148 |
|
| 149 |
return false; |
| 150 |
} |
| 151 |
return parent::isArrayMarked( $value ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
protected function unmarkArray( array &$value, ?string $classHint ): string { |
| 156 |
|
| 157 |
|
| 158 |
if ( |
| 159 |
$this->backCompat && |
| 160 |
$classHint === null && |
| 161 |
array_key_exists( JsonConstants::COMPLEX_ANNOTATION, $value ) |
| 162 |
) { |
| 163 |
$classHint = 'array'; |
| 164 |
} |
| 165 |
|
| 166 |
$className = parent::unmarkArray( $value, $classHint ); |
| 167 |
|
| 168 |
if ( $this->backCompat ) { |
| 169 |
unset( $value[JsonConstants::COMPLEX_ANNOTATION] ); |
| 170 |
} |
| 171 |
return $className; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
public function unserialize( $json, ?string $expectedClass = null ) { |
| 176 |
return $this->deserialize( $json, $expectedClass ); |
| 177 |
} |
| 178 |
|
| 179 |
public function deserialize( $json, ?string $expectedClass = null ) { |
| 180 |
Assert::parameterType( [ 'stdClass', 'array', 'string' ], $json, '$json' ); |
| 181 |
Assert::precondition( |
| 182 |
!$expectedClass || |
| 183 |
is_subclass_of( $expectedClass, JsonDeserializable::class ) || |
| 184 |
is_subclass_of( $expectedClass, JsonCodecable::class ), |
| 185 |
'$expectedClass parameter must be subclass of JsonDeserializable or JsonCodecable, got ' . $expectedClass |
| 186 |
); |
| 187 |
if ( is_string( $json ) ) { |
| 188 |
$jsonStatus = FormatJson::parse( $json, FormatJson::FORCE_ASSOC ); |
| 189 |
if ( !$jsonStatus->isGood() ) { |
| 190 |
throw new JsonException( "Bad JSON: {$jsonStatus}" ); |
| 191 |
} |
| 192 |
$json = $jsonStatus->getValue(); |
| 193 |
} |
| 194 |
|
| 195 |
if ( $json instanceof stdClass ) { |
| 196 |
$json = (array)$json; |
| 197 |
} |
| 198 |
|
| 199 |
if ( $expectedClass !== null ) { |
| 200 |
|
| 201 |
$jsonCopy = $json; |
| 202 |
if ( is_array( $jsonCopy ) && $this->isArrayMarked( $jsonCopy ) ) { |
| 203 |
$got = $this->unmarkArray( $jsonCopy, $expectedClass ); |
| 204 |
|
| 205 |
|
| 206 |
if ( is_a( $got, $expectedClass, true ) ) { |
| 207 |
|
| 208 |
} else { |
| 209 |
throw new JsonException( "Expected {$expectedClass} got {$got}" ); |
| 210 |
} |
| 211 |
} else { |
| 212 |
$got = get_debug_type( $json ); |
| 213 |
throw new JsonException( "Expected {$expectedClass} got {$got}" ); |
| 214 |
} |
| 215 |
} |
| 216 |
try { |
| 217 |
$result = is_array( $json ) ? $this->newFromJsonArray( $json ) : $json; |
| 218 |
} catch ( InvalidArgumentException $e ) { |
| 219 |
throw new JsonException( $e->getMessage() ); |
| 220 |
} |
| 221 |
if ( $expectedClass && !is_a( $result, $expectedClass, false ) ) { |
| 222 |
throw new JsonException( "Unexpected class: {$expectedClass}" ); |
| 223 |
} |
| 224 |
return $result; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
public function unserializeArray( array $array ): array { |
| 229 |
return $this->deserializeArray( $array ); |
| 230 |
} |
| 231 |
|
| 232 |
public function deserializeArray( array $array ): array { |
| 233 |
try { |
| 234 |
|
| 235 |
|
| 236 |
return $this->newFromJsonArray( $array, 'array' ); |
| 237 |
} catch ( InvalidArgumentException $e ) { |
| 238 |
throw new JsonException( $e->getMessage() ); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
public function serialize( $value ) { |
| 243 |
|
| 244 |
|
| 245 |
try { |
| 246 |
$value = $this->toJsonArray( $value ); |
| 247 |
} catch ( InvalidArgumentException $e ) { |
| 248 |
throw new JsonException( $e->getMessage() ); |
| 249 |
} |
| 250 |
|
| 251 |
$json = FormatJson::encode( $value, false, FormatJson::ALL_OK ); |
| 252 |
if ( !$json ) { |
| 253 |
try { |
| 254 |
|
| 255 |
$details = $this->detectNonSerializableData( $value ); |
| 256 |
} catch ( \Throwable $t ) { |
| 257 |
$details = $t->getMessage(); |
| 258 |
} |
| 259 |
throw new JsonException( |
| 260 |
'Failed to encode JSON. ' . |
| 261 |
'Error: ' . json_last_error_msg() . '. ' . |
| 262 |
'Details: ' . $details |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
return $json; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
private function detectNonSerializableDataInternal( |
| 284 |
$value, |
| 285 |
bool $expectDeserialize, |
| 286 |
string $accumulatedPath, |
| 287 |
bool $exhaustive = false |
| 288 |
): ?string { |
| 289 |
if ( |
| 290 |
( is_array( $value ) && $this->isArrayMarked( $value ) ) || |
| 291 |
( $value instanceof stdClass && $this->isArrayMarked( (array)$value ) ) |
| 292 |
) { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
return $accumulatedPath . ': conflicting use of protected property'; |
| 297 |
} |
| 298 |
if ( is_object( $value ) ) { |
| 299 |
if ( get_class( $value ) === stdClass::class ) { |
| 300 |
$value = (array)$value; |
| 301 |
} elseif ( $value instanceof JsonCodecable ) { |
| 302 |
if ( $exhaustive ) { |
| 303 |
|
| 304 |
|
| 305 |
$codec = $this->codecFor( get_class( $value ) ); |
| 306 |
$value = $codec->toJsonArray( $value ); |
| 307 |
} else { |
| 308 |
|
| 309 |
|
| 310 |
return null; |
| 311 |
} |
| 312 |
} elseif ( |
| 313 |
$expectDeserialize ? |
| 314 |
$value instanceof JsonDeserializable : |
| 315 |
$value instanceof JsonSerializable |
| 316 |
) { |
| 317 |
if ( $exhaustive ) { |
| 318 |
|
| 319 |
|
| 320 |
'@phan-var JsonSerializable $value'; |
| 321 |
$value = $value->jsonSerialize(); |
| 322 |
if ( !is_array( $value ) ) { |
| 323 |
return $accumulatedPath . ": jsonSerialize didn't return array"; |
| 324 |
} |
| 325 |
} else { |
| 326 |
|
| 327 |
|
| 328 |
return null; |
| 329 |
} |
| 330 |
} else { |
| 331 |
|
| 332 |
return $accumulatedPath . ': ' . get_debug_type( $value ); |
| 333 |
} |
| 334 |
} |
| 335 |
if ( is_array( $value ) ) { |
| 336 |
foreach ( $value as $key => $propValue ) { |
| 337 |
$propValueNonSerializablePath = $this->detectNonSerializableDataInternal( |
| 338 |
$propValue, |
| 339 |
$expectDeserialize, |
| 340 |
$accumulatedPath . '.' . $key, |
| 341 |
$exhaustive |
| 342 |
); |
| 343 |
if ( $propValueNonSerializablePath !== null ) { |
| 344 |
return $propValueNonSerializablePath; |
| 345 |
} |
| 346 |
} |
| 347 |
} elseif ( !is_scalar( $value ) && $value !== null ) { |
| 348 |
return $accumulatedPath . ': nonscalar ' . get_debug_type( $value ); |
| 349 |
} |
| 350 |
return null; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function detectNonSerializableData( $value, bool $expectDeserialize = false ): ?string { |
| 364 |
return $this->detectNonSerializableDataInternal( $value, $expectDeserialize, '$', true ); |
| 365 |
} |
| 366 |
} |