Skip to content

Commit

Permalink
toJSON may serialize a "pos" object for a custom complex property fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Feb 1, 2024
1 parent e63882f commit 6410ea8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/jsonobject.ts
Expand Up @@ -1763,9 +1763,7 @@ export class JsonObject {
}
private removePosOnValueToJson(property: JsonObjectProperty, value: any): any {
if(!property.isCustom || !value) return value;
if (!!value[JsonObject.positionPropertyName]) {
delete value[JsonObject.positionPropertyName];
}
this.removePosFromObj(value);
return value;
}
private removePos(property: JsonObjectProperty, value: any): void {
Expand All @@ -1780,9 +1778,13 @@ export class JsonObject {
this.removePosFromObj(obj[i]);
}
}
if(typeof obj !== "object") return;
if (!!obj[JsonObject.positionPropertyName]) {
delete obj[JsonObject.positionPropertyName];
}
for(let key in obj) {
this.removePosFromObj(obj[key]);
}
}
private isValueArray(value: any): boolean {
return value && Array.isArray(value);
Expand Down
52 changes: 52 additions & 0 deletions tests/jsonobjecttests.ts
Expand Up @@ -849,6 +849,58 @@ QUnit.test(
);
}
);
QUnit.test("Custom object deserialization, remove pos", function (assert) {
Serializer.addClass("optionstype",
[
{ name: "itemValue" },
{ name: "colorId" },
{ name: "label:text", isLocalizable: true },
], undefined);
Serializer.addProperty("car", { name: "options:optionstype" });
Serializer.addProperty("car", { name: "optionsarray:optionstype[]" });
const json: any = {
options: {
pos: { start: 1, end: 5 },
itemValue: "abcdef",
label: {
pos: { start: 10, end: 15 },
default: "default text",
en: "en text",
de: "de text",
},
colorId: "black"
},
optionsarray: [{
pos: { start: 20, end: 25 },
itemValue: "abcdef",
label: {
pos: { start: 30, end: 35 },
default: "default text",
en: "en text",
de: "de text",
},
colorId: "black"
}]
};
const car: any = new Car();
car.fromJSON(json);

assert.equal(car.options.itemValue, "abcdef", "check itemValue");
assert.equal(car.options.pos.start, 1, "pos1");
assert.equal(car.options.label.pos.start, 10, "pos2");
assert.equal(car.optionsarray[0].pos.start, 20, "pos3");
assert.equal(car.optionsarray[0].label.pos.start, 30, "pos4");

const car_json = car.toJSON();
assert.notOk(car_json.options["pos"], "no pos1");
assert.notOk(car_json.options.label["pos"], "no pos2");
assert.notOk(car_json.optionsarray[0]["pos"], "no pos3");
assert.notOk(car_json.optionsarray[0].label["pos"], "no pos4");

Serializer.removeProperty("car", "options");
Serializer.removeProperty("car", "optionsarray");
Serializer.removeClass("optionstype");
});
QUnit.test(
"ItemValue and settings.itemValueAlwaysSerializeAsObject = true",
function (assert) {
Expand Down

0 comments on commit 6410ea8

Please sign in to comment.