Skip to content

Commit

Permalink
Allow to create a custom survey serialization fixed #3395
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Sep 3, 2022
1 parent 0a1c947 commit 4eb5939
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ export class JsonMetadata {
private childrenClasses: HashTable<Array<JsonMetadataClass>> = {};
private classProperties: HashTable<Array<JsonObjectProperty>> = {};
private classHashProperties: HashTable<HashTable<JsonObjectProperty>> = {};
public onSerializingProperty: ((obj: Base, prop: JsonObjectProperty, value: any, json: any) => boolean) | undefined;
public getObjPropertyValue(obj: any, name: string): any {
if (this.isObjWrapper(obj)) {
var orignalObj = obj.getOriginalObj();
Expand Down Expand Up @@ -1519,7 +1520,7 @@ export class JsonObject {
result: any,
property: JsonObjectProperty,
storeDefaults = false
) {
): void {
if (
property.isSerializable === false ||
(property.isLightSerializable === false && this.lightSerializing)
Expand All @@ -1540,7 +1541,9 @@ export class JsonObject {
typeof obj["getPropertyValue"] === "function" &&
obj["getPropertyValue"](property.name, null) !== null;
if ((storeDefaults && hasValue) || !property.isDefaultValue(value)) {
result[property.name] = value;
if(!Serializer.onSerializingProperty || !Serializer.onSerializingProperty(obj, property, value, result)) {
result[property.name] = value;
}
}
}
public valueToObj(value: any, obj: any, property: JsonObjectProperty) {
Expand Down
48 changes: 48 additions & 0 deletions tests/jsonobjecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Serializer,
JsonUnknownPropertyError,
property,
JsonObjectProperty,
} from "../src/jsonobject";
import { ItemValue } from "../src/itemvalue";
import { Base } from "../src/base";
Expand Down Expand Up @@ -2834,3 +2835,50 @@ QUnit.test("Find out are properties addingin in addClass are custom or not", fun
assert.equal(Serializer.findProperty("testcar", "name").isCustom, true, "testcar properties are custom");
assert.equal(Serializer.findProperty("car", "name").isCustom, false, "car properties are not custom");
});
QUnit.test("Custom survey serialization, onSerializingProperty", function (assert) {
const mapping : any = {
base: { elements: "Questions", visible: { name: "IsHidden", converter: (obj: Base, value: any): any => { return !value; } } },
question: { choices: "AnswerOptions" },
survey: { title: "Name", pages: "sections" }
};
const getMappedObj = function(type: string, name: string): string {
const typeMap = mapping[type];
return !!typeMap ? typeMap[name] : undefined;
};
Serializer.onSerializingProperty = (obj: Base, prop: JsonObjectProperty, value: any, json: any): boolean => {
let typesList = new Array<string>();
typesList.push(obj.getType());
if(obj.isDescendantOf("question")) {
typesList.push("question");
}
typesList.push("base");

for(var i = 0; i < typesList.length; i ++) {
let mapObj = <any>getMappedObj(typesList[i], prop.name);
if(!mapObj) continue;
const name = typeof mapObj === "string" ? mapObj : mapObj.name;
if(typeof mapObj !== "string" && mapObj.converter) {
value = mapObj.converter(obj, value);
}
json[name] = value;
return true;
}
return false;
};
const checkJSON = function(origionalJSON: any, expectedJSON: any, attempt: string) {
const survey = new SurveyModel(origionalJSON);
assert.deepEqual(survey.toJSON(), expectedJSON, "Failed attempt: " + attempt);
};
checkJSON({ title: "Your Questionnaire" }, { Name: "Your Questionnaire" }, "survey title => survey Name");
checkJSON({ pages: [{ name: "page1" }] }, { sections: [{ name: "page1" }] }, "survey pages => survey sections");
checkJSON(
{ pages: [{ name: "page1", elements: [{ type: "text", name: "q1" }] }] },
{ sections: [{ name: "page1", Questions: [{ type: "text", name: "q1" }] }] }, "page elements => page Questions");
checkJSON(
{ pages: [{ name: "page1", elements: [{ type: "checkbox", name: "q1", choices: [1, 2] }] }] },
{ sections: [{ name: "page1", Questions: [{ type: "checkbox", name: "q1", AnswerOptions: [1, 2] }] }] }, "choices => page AnswerOptions");
checkJSON(
{ pages: [{ name: "page1", elements: [{ type: "text", name: "q1", visible: false }] }] },
{ sections: [{ name: "page1", Questions: [{ type: "text", name: "q1", IsHidden: true }] }] }, "visible => isHidden (opposite)");
Serializer.onSerializingProperty = undefined;
});

0 comments on commit 4eb5939

Please sign in to comment.