Skip to content

Instantly share code, notes, and snippets.

@matthieubulte
Created December 26, 2014 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthieubulte/1a4a69538fcb91a703d0 to your computer and use it in GitHub Desktop.
Save matthieubulte/1a4a69538fcb91a703d0 to your computer and use it in GitHub Desktop.
var foo = {
"bar": {
"x": "test",
"y": 3
},
"list": [1, 2, 3, 4],
"obj_list": [
{
"x": "",
"y": ""
},
{
"x": "test",
"y": 2
},
{
"x": "test",
"y": 1
}
],
"nested_list": [
[1, 2, 3],
[4, 5, 6]
]
};
function id(x) { return x; }
function extract_level(str, nester, popper) {
var nesters = ['{', '['];
var poppers = ['}', ']'];
var index = 0;
var deep_level = 0;
var current = "";
var content = [];
if(str === "") {
return [];
}
do {
var c = str[index++];
if(c === undefined) {
throw new Error('Unexpected end of input');
}
if(nesters.indexOf(c) !== -1) {
deep_level++;
}
if(poppers.indexOf(c) !== -1) {
deep_level--;
}
// skip first nester
if (c === nester && deep_level === 1) {
continue;
}
// skip last popper and push last content
if (c === popper && deep_level === 0) {
if(current) {
content.push(current);
}
continue;
}
// push if comma and on base level
if (c === ',' && deep_level===1) {
content.push(current);
current = "";
}
// append to current word else
else {
current += c;
}
} while (deep_level >= 1);
return content;
}
// could be replaced by typed (de)serializer like Int/String/... doing convertion and
// type checking and convertion while deserializing
function atom() {
return {
serialize: id,
deserialize: id
};
}
function model(mod) {
return {
serialize: function(obj) {
var result = []
for(var key in obj) {
var val = obj[key];
var serializer = mod[key].serialize;
result.push(serializer(val));
}
return '{' + result.join(',') + '}';
},
deserialize: function(str) {
var content = extract_level(str, '{', '}');
var index = 0;
var ret = {};
for(key in mod) {
var value = content[index++];
var deserializer = mod[key].deserialize;
ret[key] = deserializer(value);
}
return ret;
}
};
}
function list(mod) {
var serializer = mod.serialize;
var deserializer = mod.deserialize;
return {
serialize: function(arr) {
var result = []
for(var i=0; i<arr.length; i++) {
result.push(serializer(arr[i]));
}
return '[' + result.join(',') + ']';
},
deserialize: function(str) {
var content = extract_level(str, '[', ']');
var index = 0;
var ret = [];
for(var i=0; i<content.length; i++) {
ret.push(mod.deserialize(content[i]));
}
return ret;
}
};
}
var communication_data = model({});
var address = model({
"address": atom(),
"postalCode": atom(),
"city": atom(),
"countryName": atom()
});
var specialization = model({
"name": atom(),
"classification": atom(),
"weight": atom()
});
var prefix_academic_title = model({
"name": atom(),
"type": atom()
});
var person = model({
"_id": atom(),
"addresses": list(address),
"communicationData": list(communication_data),
"customerNumber": atom(),
"firstName": atom(),
"gender": atom(),
"lastName": atom(),
"lastTwelveMonthsActivityCount": atom(),
"personTypes": list(atom()),
"prefixAcademicTitle": prefix_academic_title,
"specializations": list(specialization)
});
var da_person = {
"_id" : "00000e55-416e-4b85-8a95-bddb0de3e976",
"addresses" : [
{
"address" : "Robert-Koch-Str. 2-12",
"postalCode" : "16515",
"city" : "Oranienburg",
"countryName" : "Deutschland"
}
],
"communicationData" : [ ],
"customerNumber" : "1275619",
"firstName" : "Melanie",
"gender" : "FEMALE",
"lastName" : "Kott",
"lastTwelveMonthsActivityCount" : 0,
"personTypes" : ["Klinikarzt"],
"prefixAcademicTitle" : {
"name" : "Dr. med.",
"type" : "PREFIX"
},
"specializations" : [
{
"name" : "Innere Medizin",
"classification" : "HF",
"weight" : 1
}
]
};
var bar_model = model({
"x": atom(),
"y": atom()
});
var foo_model = model({
"a": atom(),
"bar": bar_model,
"list": list(atom()),
"obj_list": list(bar_model),
"nested_list": list(list(atom()))
});
//console.log('serialized: ' + foo_model.serialize(foo));
//console.log('deserialized: ' + JSON.stringify(foo_model.deserialize(foo_model.serialize(foo)), null, ' '));
console.log('serialized: ' + person.serialize(da_person));
console.log('deserialized: ' + JSON.stringify(person.deserialize(person.serialize(da_person)), null, ' '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment