diff options
Diffstat (limited to 'MLEB/Translate/src/Utilities/Json/JsonUnserializableTrait.php')
-rw-r--r-- | MLEB/Translate/src/Utilities/Json/JsonUnserializableTrait.php | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/MLEB/Translate/src/Utilities/Json/JsonUnserializableTrait.php b/MLEB/Translate/src/Utilities/Json/JsonUnserializableTrait.php new file mode 100644 index 00000000..2db96ca1 --- /dev/null +++ b/MLEB/Translate/src/Utilities/Json/JsonUnserializableTrait.php @@ -0,0 +1,37 @@ +<?php +declare( strict_types = 1 ); + +namespace MediaWiki\Extension\Translate\Utilities\Json; + +use FormatJson; + +/** + * Can be used by classes that want to serialize / deserialize + * Remove once we need to support only MW >= 1.36 + * See Change-Id: I5433090ae8e2b3f2a4590cc404baf838025546ce + * + * @license GPL-2.0-or-later + * @since 2020.12 + */ +trait JsonUnserializableTrait { + public function jsonSerialize() { + return $this->annotateJsonForDeserialization( + $this->toJsonArray() + ); + } + + /** Annotate the $json array with class metadata. */ + private function annotateJsonForDeserialization( array $json ): string { + $json[JsonCodec::TYPE_ANNOTATION] = get_class( $this ); + return FormatJson::encode( $json, false, FormatJson::ALL_OK ); + } + + /** + * Prepare this object for JSON serialization. + * The returned array will be passed to self::newFromJsonArray + * upon JSON deserialization. + */ + abstract protected function toJsonArray(): array; +} + +class_alias( JsonUnserializableTrait::class, '\MediaWiki\Extensions\Translate\JsonUnserializableTrait' ); |