public convert()

in GatewayPluginExample/Ux/gulps/gulp-resjson/resjson-convert.ts [63:101]


    public convert(content: string) {
        let root = {};
        // Remove comments, /* multilinecomment*/ and // one line comment and "//": "JSON element comment"
        content = content.replace(/(\/\*([^*]|[\n]|(\*+([^*/]|[\n])))*\*\/+)|( +\/\/.*)|(  +\"\/\/\".*)/g, '');
        let data: any = JSON.parse(content);
        let itemKeys = Object.keys(data);

        // build a data tree.
        for (let itemKey of itemKeys) {
            // remove localization comments
            if (itemKey.startsWith('//') || (itemKey.startsWith('_') && itemKey.endsWith('.comment'))) {
                continue;
            }

            let current = root;
            let itemValue = data[itemKey];
            const keys = itemKey.split('_');
            let count = keys.length;
            for (let key of keys) {
                count--;
                if (count > 0) {
                    if (!current.hasOwnProperty(key)) {
                        current[key] = {};
                    }

                    current = current[key];

                    if (typeof current !== 'object') {
                        throw new Error('Resource key already exists: ' + itemKey);
                    }
                } else {
                    current[key] = itemValue;
                }
            }
        }

        this.contentReset();
        this.traverse([{ name: 'Strings', value: root }], 0);
    }