Skip to content

Instantly share code, notes, and snippets.

@oscarcs
Created January 21, 2019 02:22
Show Gist options
  • Save oscarcs/32ddaaa10a24cf38ba1290bfca61a30e to your computer and use it in GitHub Desktop.
Save oscarcs/32ddaaa10a24cf38ba1290bfca61a30e to your computer and use it in GitHub Desktop.
function customJSONStringify(value) {
function writeJSON(value) {
switch (toType(value)) {
case "object":
return writeObject(value);
case "array":
return writeArray(value);
//...
}
}
function writeObject(value) {
result = '{';
for (var k in value.keys()) {
result += '"' + k + '": ' + writeJson(value[k]) + ', '
}
result += "}";
return result;
}
function writeArray(value) {
result = '[';
result += ']';
return result;
}
function writeString(value) {
}
function writeNumber(value) {
}
function writeBoolean(value) {
}
function writeNull(value) {
}
// https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
function toType(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}
return writeJSON(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment