Skip to content

Instantly share code, notes, and snippets.

@rom1504
Created June 14, 2016 12:51
Show Gist options
  • Save rom1504/88645e54071cad8d0ec0fccc28494e14 to your computer and use it in GitHub Desktop.
Save rom1504/88645e54071cad8d0ec0fccc28494e14 to your computer and use it in GitHub Desktop.
simple stringify
// model : https://github.com/rom1504/JsonConv/blob/master/source/json_to_json.ml
const p=require('./protocol.json');
const json_string_of_jarray=a => `[${(a.map(json_string_of_jvalue)).join(",")}]`;
const json_string_of_jobject=o => `{${Object.keys(o).map(k => '"'+k+'":'+json_string_of_jvalue(o[k])).join(",")}}`;
const json_string_of_jvalue=v => {
if(v===null)
return "NULL";
if(v.constructor===Array)
return json_string_of_jarray(v);
if(typeof v === 'object')
return json_string_of_jobject(v);
if(typeof v === 'string')
return '"'+v+'"';
if(typeof v === 'boolean')
return v ? "true" : "false";
if(typeof v === 'number')
return v;
throw Error("unexpected type !");
}
console.log(json_string_of_jvalue(p));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment