diff --git a/static_core/plugins/ets/sdk/api/@ohos.buffer.ets b/static_core/plugins/ets/sdk/api/@ohos.buffer.ets index da2ed0024104b975ce5fa290e2c3e3c9886f803f..ef81cbe66a502dacb335931407ffa4e1b0931c84 100644 --- a/static_core/plugins/ets/sdk/api/@ohos.buffer.ets +++ b/static_core/plugins/ets/sdk/api/@ohos.buffer.ets @@ -669,7 +669,7 @@ export default namespace buffer { * A class representing a fixed-length sequence of bytes. * Provides methods for reading and manipulating binary data with various encodings. */ - export class Buffer implements JsonElementDeserializable { + export class Buffer implements jsonx.JsonElementDeserializable { /** The underlying ArrayBuffer storing the binary data */ private bufferData: ArrayBuffer; /** The offset into the buffer where this Buffer instance starts */ diff --git a/static_core/plugins/ets/stdlib/escompat/json.ets b/static_core/plugins/ets/stdlib/escompat/json.ets index 77438bfaa99ad122008ad5cda058aed430c008ff..69a44847851d2626e0a796fc6417cec3ed3233dd 100644 --- a/static_core/plugins/ets/stdlib/escompat/json.ets +++ b/static_core/plugins/ets/stdlib/escompat/json.ets @@ -544,6 +544,26 @@ export class JSON { return s.toString() } + /** + * Converts a JsonElementDeserializable to a JSON string. + * @param elem - The JsonElementDeserializable to stringify + * @returns The JSON string representation + */ + public static stringifyJsonElement(elem: jsonx.JsonElementDeserializable): string { + return JSON.stringifyJsonElement(elem.toJSON()) + } + + /** + * Converts a JsonElementDeserializable to a JSON string with optional formatting. + * @param elem - The JsonElementDeserializable to stringify + * @param replacer - Array of keys to include (currently not implemented) + * @param space - String or number of spaces for indentation + * @returns The JSON string representation + */ + public static stringifyJsonElement(elem: jsonx.JsonElementDeserializable, replacer?: (double | string)[], space?: int | string): string { + return JSON.stringifyJsonElement(elem.toJSON(), replacer, space) + } + /** * Converts a JsonElement to a JSON string. * @param elem - The JsonElement to stringify @@ -665,14 +685,6 @@ export class JSON { } } -export interface JsonElementDeserializable { - /** - * Serializes an object to a JsonElement. - * @returns {JsonElement} The JSON representation of the object - */ - toJSON(): jsonx.JsonElement -} - class JSONWriter { // NOTE(cheezzario) replace with Type.for() when it will be implemented private static readonly STD_CORE_INTEROP_JSVALUE_TYPE = (Type.of([] as FixedArray) as ArrayType).getElementType() @@ -743,8 +755,8 @@ class JSONWriter { this.writeESCompatRecord(temp) } else if (obj instanceof ESValue) { this.writeInteropESValue(obj) - } else if (obj instanceof JsonElementDeserializable) { - this.buffer.append(JSON.stringifyJsonElement((obj as JsonElementDeserializable).toJSON())) + } else if (obj instanceof jsonx.JsonElementDeserializable) { + this.buffer.append(JSON.stringifyJsonElement(obj)) } else if (obj instanceof ArrayBuffer) { this.buffer.append(JSONObject.EMPTY) } else if (this.writeValueTypeWrapper(obj)) { @@ -753,8 +765,6 @@ class JSONWriter { const objType = Type.of(obj) if (objType.getName() == "escompat.Date") { this.buffer.append("\"").append((obj as Date).toISOString()).append("\"") - // } else if (objType.getName() == "@ohos.buffer.buffer.Buffer") { - // this.buffer.append(JSON.stringifyJsonElement((obj as JsonElementDeserializable).toJSON())) } else if (objType.getName() == JSONWriter.STD_CORE_INTEROP_JSVALUE_TYPE.getName()) { this.buffer.append(JSON.stringify(obj as JSValue)) } else if (objType instanceof ArrayType) { diff --git a/static_core/plugins/ets/stdlib/std/core/Json.ets b/static_core/plugins/ets/stdlib/std/core/Json.ets index 7a2c37010a263a7740ecb72acef33148f89a02a2..3f73768e2eca718a877e30e62c070744180821e5 100644 --- a/static_core/plugins/ets/stdlib/std/core/Json.ets +++ b/static_core/plugins/ets/stdlib/std/core/Json.ets @@ -776,24 +776,24 @@ export namespace jsonx { * Interface for types that can be deserialized from JSON. * Classes implementing this interface can be converted from a JsonElement. */ - interface JsonElementSerializable { + export interface JsonElementSerializable { /** * Deserializes an object from a JsonElement. * @param {JsonElement} jsonElem - The JSON element to deserialize from */ - fromJson(jsonElem: JsonElement) + fromJSON(jsonElem: JsonElement) } /** * Interface for types that can be serialized to JSON. * Classes implementing this interface can be converted to a JsonElement. */ - interface JsonElementDeserializable { + export interface JsonElementDeserializable { /** * Serializes an object to a JsonElement. * @returns {JsonElement} The JSON representation of the object */ - toJson(): JsonElement + toJSON(): JsonElement } } diff --git a/static_core/plugins/ets/stdlib/std/core/String.ets b/static_core/plugins/ets/stdlib/std/core/String.ets index 17b4dc4a0121d59e09f75a7440ffe4937d5c1a15..2d6af5ad51a4b8f798cfae212dc0d93737b399ae 100644 --- a/static_core/plugins/ets/stdlib/std/core/String.ets +++ b/static_core/plugins/ets/stdlib/std/core/String.ets @@ -1587,7 +1587,7 @@ export final class String extends Object implements Comparable, Iterable } let preceding = this.substring(0, position) let following = this.substring(position + searchLength) - let replacement = replacer(searchValue, [new Double(position as double) as Object, this as Object] as Object[]) + let replacement = replacer(searchValue, [position.toDouble() as Object, this as Object] as Object[]) return preceding + replacement + following } diff --git a/static_core/plugins/ets/tests/ets_func_tests/std/core/json/JsonElementFromJsonTest.ets b/static_core/plugins/ets/tests/ets_func_tests/std/core/json/JsonElementFromJsonTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..deac9377e854fea259ddef7364bd8023a92825dc --- /dev/null +++ b/static_core/plugins/ets/tests/ets_func_tests/std/core/json/JsonElementFromJsonTest.ets @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { jsonx } from "std/core" + +function main() { + const suite = new arktest.ArkTestsuite("jsonx.fromJson") + + suite.addTest("Test fromJson", testFromJson) +} + +function testFromJson() { + let jsonStr = ` + { + "foo": 42, + "bar": "myBar", + "t2": { + "newName": false + } + } + ` + const parsed = JSON.parseJsonElement(jsonStr) + let t1 = new T1 + t1.fromJSON(parsed) + arktest.assertEQ(t1.foo, 42) + arktest.assertEQ(t1.bar, "myBar") + arktest.assertFalse(t1.t2.biz) +} + +class T2 implements jsonx.JsonElementSerializable { + biz: boolean + + fromJSON(jsonElem: jsonx.JsonElement) { + this.biz = jsonElem.getBoolean("newName") + } +} + +class T1 implements jsonx.JsonElementSerializable { + foo: int + bar: string + t2: T2 + + constructor() { + this.foo = 1 + this.bar = "bar" + this.t2 = new T2 + } + + fromJSON(jsonElem: jsonx.JsonElement) { + this.foo = jsonElem.getInteger("foo") + this.bar = jsonElem.getString("bar") + let myT2 = new T2 + myT2.fromJSON(jsonElem.getElement("t2")) + this.t2 = myT2 + } +} diff --git a/static_core/plugins/ets/tests/ets_func_tests/std/core/json/JsonElementToJsonTest.ets b/static_core/plugins/ets/tests/ets_func_tests/std/core/json/JsonElementToJsonTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..3bcdb9955083fc37b978f912ff6333ed01efafe9 --- /dev/null +++ b/static_core/plugins/ets/tests/ets_func_tests/std/core/json/JsonElementToJsonTest.ets @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { jsonx } from "std/core" + +function main() { + const suite = new arktest.ArkTestsuite("jsonx.toJson") + + suite.addTest("Test toJson", testToJson) +} + +function testToJson() { + let t1 = new T1 + let jsonedT1 = t1.toJSON() + arktest.assertEQ(jsonedT1.getString("newBar"), "34") + arktest.assertEQ(jsonedT1.getInteger("foo"), 5) + let jsonedT2 = jsonedT1.getElement("t2") + arktest.assertTrue(jsonedT2.getBoolean("biz")) +} + +class T2 implements jsonx.JsonElementDeserializable { + biz: boolean = true + + toJSON(): jsonx.JsonElement { + let val = jsonx.JsonElement.createBoolean(this.biz) + let m: Record = { + "biz": val, + } + return jsonx.JsonElement.createObject(m) + } +} + +class T1 implements jsonx.JsonElementDeserializable { + foo: int = 5 + bar: string = "34" + t2: T2 = new T2 + + toJSON(): jsonx.JsonElement { + let jFoo = jsonx.JsonElement.createInteger(this.foo) + let jBar = jsonx.JsonElement.createString(this.bar) + let jT2 = this.t2.toJSON() + + let m: Record = { + "foo": jFoo, + "newBar": jBar, + "t2": jT2, + } + return jsonx.JsonElement.createObject(m) + } +} diff --git a/static_core/plugins/ets/tests/test-lists/declgenets2ts/ets-func-tests/declgen-ets2ts-func-tests-ignored.txt b/static_core/plugins/ets/tests/test-lists/declgenets2ts/ets-func-tests/declgen-ets2ts-func-tests-ignored.txt index dafbeafc62151015cf04912122142d75d04dfcdf..876a5fa8625f0835dda03c8efd20f8ba03770451 100644 --- a/static_core/plugins/ets/tests/test-lists/declgenets2ts/ets-func-tests/declgen-ets2ts-func-tests-ignored.txt +++ b/static_core/plugins/ets/tests/test-lists/declgenets2ts/ets-func-tests/declgen-ets2ts-func-tests-ignored.txt @@ -181,6 +181,8 @@ regression/23623.ets regression/23735.ets std/core/DeepCopyTest.ets std/core/json/JsonElementParseTest.ets +std/core/json/JsonElementFromJsonTest.ets +std/core/json/JsonElementToJsonTest.ets std/core/std_core_char_static_Char_charsToCodePoint.ets std/core/std_core_char_static_Char_codeUnitsToEncode.ets std/core/std_core_char_static_Char_getHighSurrogate.ets