I need to parse a json file into a javascript object. I'm using GCC (Closure Compiler) which might mangle my object names, so I need to refer to the json keys as strings when importing or exporting the json file.
i.e. importing:
var jsonobj = JSON.parse(json_object_text);
myobj.member1 = jsonobj['Key 1'];
myobj.member2 = jsonobj['Key 2'];
and exporting:
jsonobj['Key 1'] = myobj.member1;
jsonobj['Key 2'] = myobj.member2;
var json_object_text = JSON.stringify(jsonobj);
But doing that is tedious. And I would like to only define the keys as text strings once.
So I tried to define a constant object which is kind of mapping from javascript object to the json schema.
// Schema definition for import/export of confc.json.
/** @const */ var dbs = {
$schema: '$schema',
$seq: '$seq',
channel: {
key: 'chan',
id: 'id',
name: 'txt',
value: 'val'
}
}
But then I still had to do a tedious import/export function:
export_db: function (db) {
return db && {
[dbs.$schema]: db.$schema,
[dbs.$seq]: db.$seq,
[dbs.channel.key]: db.channel.map(function (e, i) {
var s = dbs.channel; // schema object
return {
[s.id]: util.toInt(e.id),
[s.name]: e.name,
[s.value]: e.value
};
}),
import_db: function (js) {
js = js || {};
// wrapper for .map() function which works if array is undefined.
function dbmap(arr_in, func) {
arr_in = arr_in || [];
return arr_in.map(func);
}
return {
$schema: js[dbs.$schema],
$seq: js[dbs.$seq],
channel: dbmap(js[dbs.channel.key], function (e, i) {
var s = dbs.channel;
return {
id: e[s.id],
name: e[s.name],
value: e[s.value]
};
}),
This seems to work OK, but would someone please show me a cleaner way to do this?
I would like to use an object prototype/constructor like in the examples given here. But I don't see any examples that take into account how GCC might mangle the objects member names. Also I have nested objects (no functions, just text and numbers).