diff options
author | C. Scott Ananian <cscott@cscott.net> | 2023-11-17 13:31:19 -0500 |
---|---|---|
committer | James D. Forrester <jforrester@wikimedia.org> | 2024-10-15 20:09:51 -0400 |
commit | 3bc172d0e4e8048a415b6992af3b6db84929cc02 (patch) | |
tree | b3785adc7fd7e9acf0817480c2d4e250702dcae3 /includes/json/JsonSerializableCodec.php | |
parent | 1b65e8ba83db8158a5a946b9dd006718520d4f68 (diff) | |
download | mediawikicore-3bc172d0e4e8048a415b6992af3b6db84929cc02.tar.gz mediawikicore-3bc172d0e4e8048a415b6992af3b6db84929cc02.zip |
[JsonCodec] Use wikimedia/json-codec to implement JsonCodec
This adds support for serializing/deserializing objects which
implement the JsonCodecable interface from the wikimedia/json-codec
library used by Parsoid. JsonCodecable allows customizing the encoding
of objects of a given class using a class-specific codec object, and
JsonCodecable is an interface which is defined and can be used outside
mediawiki core.
In addition json-codec supports deserialization in the presence of
aliased class names, fixing T353883.
Backward and forward compatibility established via the mechanism
described in
https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility
Test data generated by this patch was added in
I109640b510cef9b3b870a8c188f3b4f086d75d06 to ensure forward
compatibility with the output after this patch is merged.
Benchmarks:
PHP 7.4.33 PHP 8.2.19 PHP 8.3.6
BEFORE AFTER BEFORE AFTER BEFORE AFTER
Serialize: 926.7/s 1424.8/s 978.5/s 1542.4/s 1023.5/s 1488.6/s
Serialize (assoc): 930.2/s 1378.6/s 974.6/s 1541.9/s 1022.4/s 1463.4/s
Deserialize: 1942.7/s 1961.3/s 2118.8/s 2175.9/s 2129.8/s 2063.5/s
Deserialize (assoc): 1952.0/s 1905.7/s 2107.5/s 2192.1/s 2153.3/s 2011.1/s
These numbers definitely do not have as many significant digits as
written here. But they should be sufficient to demonstrate that
performance is not impaired by this patch and in fact serialization
speed improves slightly.
Bug: T273540
Bug: T327439
Bug: T346829
Bug: T353883
Depends-On: If1d70ba18712839615c1f4fea236843ffebc8645
Change-Id: Ia1017dcef462f3ac1ff5112106f7df81f5cc384f
Diffstat (limited to 'includes/json/JsonSerializableCodec.php')
-rw-r--r-- | includes/json/JsonSerializableCodec.php | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/includes/json/JsonSerializableCodec.php b/includes/json/JsonSerializableCodec.php new file mode 100644 index 000000000000..aeb5a3542852 --- /dev/null +++ b/includes/json/JsonSerializableCodec.php @@ -0,0 +1,61 @@ +<?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; + +use JsonException; +use JsonSerializable; +use Wikimedia\JsonCodec\JsonClassCodec; + +/** + * A JsonClassCodec for objects implementing the JsonSerializable interface. + * + * NOTE that this is for compatibility only and does NOT deserialize! + * + * @see JsonSerializable + * @see JsonClassCodec + * @since 1.43 + */ +class JsonSerializableCodec implements JsonClassCodec { + + /** @inheritDoc */ + public function toJsonArray( $obj ): array { + return $obj->jsonSerialize(); + } + + /** @return never */ + public function newFromJsonArray( string $className, array $json ) { + throw new JsonException( "Cannot deserialize: {$className}" ); + } + + /** @inheritDoc */ + public function jsonClassHintFor( string $className, string $keyName ) { + return null; + } + + public static function getInstance(): JsonSerializableCodec { + static $instance = null; + if ( $instance === null ) { + $instance = new JsonSerializableCodec(); + } + return $instance; + } +} |