shakilsiraj / json-object-mapper

A TypeScript library to serialize and deserialize object graph from/to JSON in a fast and non-recursive way
MIT License
58 stars 18 forks source link

Repeating deserialisation twice per object #80

Open dr86ansixavier opened 1 year ago

dr86ansixavier commented 1 year ago

Hi Team,

Please look at the below code segment from index.ts file. It's doing deserialisation twice per object because we are assigning the first element for deserialisation first and then the popped element again.

`const runDeserialization = (conversionFunctionStructures: ConversionFunctionStructure[]): void => {

    const converstionFunctionsArray: Array<ConversionFunctionStructure> = [];
    conversionFunctionStructures.forEach((struct: ConversionFunctionStructure) => {
        converstionFunctionsArray.push(struct);
    });

    **let conversionFunctionStructure: ConversionFunctionStructure = converstionFunctionsArray[0];** //This line needs to be replaced with **let conversionFunctionStructure: ConversionFunctionStructure = (converstionFunctionsArray.length > 0)? converstionFunctionsArray.pop() : undefined;**

    // tslint:disable-next-line:triple-equals
    while (conversionFunctionStructure != undefined) {
        const stackEntries: Array<ConversionFunctionStructure> = conversionFunctions[conversionFunctionStructure.functionName](
            conversionFunctionStructure.instance, conversionFunctionStructure.instanceKey,
            conversionFunctionStructure.type, conversionFunctionStructure.json,
            conversionFunctionStructure.jsonKey);
        stackEntries.forEach((structure: ConversionFunctionStructure) => {
            converstionFunctionsArray.push(structure);
        });
        conversionFunctionStructure = converstionFunctionsArray.pop();
    }
};`
dr86ansixavier commented 1 year ago

This is the sample JSON which I'm trying to deserialise. When we try to deserialise complex nested objects, it's recursively calling again and again. But if we add the above condition which I mentioned, it will solve the problem.

{ "id" : 55, "references" : [ ], "groupingTarget" : false, "metaTarget" : true, "relativeDataTargets" : [ { "id" : 56, "references" : [ ], "groupingTarget" : false, "metaTarget" : false, "name" : "meta.tracked_statement_website_key", "operator" : { "regEx" : "(([0-9]{1,2})[-/\\.]([0-9]{1,2})[-/\\.]([0-9]{2,4}+))", "groupCount" : -1 }, "xPath" : "./parent::td/parent::tr/child::td[count(//th[contains(text(),'Invoice Date')]/preceding-sibling::th)+1]/text()", "variableSubstitution" : false, "reinitializeVariable" : false, "index" : 0 } ], "xPath" : ".//text()", "alternateXPaths" : null, "variableSubstitution" : false, "reinitializeVariable" : false, "index" : 0 };

dr86ansixavier commented 1 year ago

@shakilsiraj I'm not sure who should I reach out. Will this be taken care? Or should I raise any PR by forking this repo? Can you guide me the next steps?