Skip to content

Instantly share code, notes, and snippets.

@kylebakerio
Last active August 29, 2015 14:25
Show Gist options
  • Save kylebakerio/74c3c5a7b52cc18deec0 to your computer and use it in GitHub Desktop.
Save kylebakerio/74c3c5a7b52cc18deec0 to your computer and use it in GitHub Desktop.
function stringifyJSON(subject) {
var output = '';
var type = typeof subject;
var index = 0;
output =
(type === 'number') || (type === 'boolean') ?
subject.toString() :
subject === null ?
'null' :
type === 'string' ?
('"' + (subject) + '"') :
Object.prototype.toString.call(subject) === '[object Array]' ?(
index = subject.length,
output += '[',
subject.forEach(function(val){
output =
(typeof val === "number") ?
output += val :
output+= stringifyJSON(val) ;
index--;
if (index > 0) {output += ","}
}),
output += ']' ):
Object.prototype.toString.call(subject) === '[object Object]' ?(
index = Object.keys(subject).length,
output += '{',
Object.keys(subject).forEach(function(val){
output =
(val === 'functions') || (val === "undefined") ?(
index --,
output ):
output += (stringifyJSON(val) + ":" + stringifyJSON(subject[val])) ;
index--;
if (index > 0) {output = output.concat(",")}
}),
output += '}' ):
console.log('failed all conditions');
return output;
}
///test values
var stringifiableObjects = [
9,
null,
true,
false,
"Hello world",
[],
[8],
["hi"],
[8, "hi"],
[1, 0, -1, -0.3, 0.3, 1343.32, 3345, 0.00011999999999999999],
[8, [[],3,4]],
[[[["foo"]]]],
{},
{"a": "apple"},
{"foo": true, "bar": false, "baz": null},
{"boolean, true": true, "boolean, false": false, "null": null },
// basic nesting
{"a":{"b":"c"}},
{"a":["b", "c"]},
[{"a":"b"}, {"c":"d"}],
{"a":[],"c": {}, "b": true}
];
var unstringifiableValues = [
{
'functions': function(){},
'undefined': undefined
}
];
//
// start tests
//
var passed = 0, failed = 0;
unstringifiableValues.concat(stringifiableObjects).reverse().forEach(function(val){
JSON.stringify(val) === stringifyJSON(val) ?
passed++ : (failed++,
console.log("what type is it? " + typeof val),
console.log(Object.prototype.toString.call(val)),
// console.log("which statement did it hit? " + hit);
console.log("original input to be stringified:"),
console.dir(val),
console.log("which should be turned into:"),
console.dir(JSON.stringify(val)),
console.log("this is what my script currently turns it into:"),
console.dir(stringifyJSON(val)),
console.log("---- \n")
)
})
console.log("Passed " + passed + " out of " + (passed + failed) + "; failed " + failed)
@kylebakerio
Copy link
Author

Why does this fail so utterly?

A) what is my syntax mistake for the object and array section?
B) why, even when I comment those out, do none of the conditions ever result in a 'true' proposition...?

@kylebakerio
Copy link
Author

Okay, learned lots of syntax rules, got everything that's not an array or an object to pass I think.

Also made several things much more efficient, not the least of which was redundant testing code.

Now to apply those lessons in the array/object chunk...

@kylebakerio
Copy link
Author

done.

@kylebakerio
Copy link
Author

removed function blocks. It makes it slightly less robust, I think, though (they're nice for creating a scoped environment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment