Skip to content

Instantly share code, notes, and snippets.

@juniorbird
Last active January 16, 2016 07:16
Show Gist options
  • Save juniorbird/193f7b6460d1676f291a to your computer and use it in GitHub Desktop.
Save juniorbird/193f7b6460d1676f291a to your computer and use it in GitHub Desktop.
JSON parser (objects are broken)
/* --------------------------------------------------------------------------- */
// Utility Functions
function dequote(str) {
// We need to remove excess quotes
// This seems hacky but I've run through a bunch of test cases and it appears to be what's needed
var newstring;
// Replace any escaped quotes
newstring = str.replace(/\\"/g, '"');
// The whole string will be wrapped in quotes. Unwrap.
newstring = newstring.replace(/"(.*)"/, '$1');
return newstring;
}
/* --------------------------------------------------------------------------- */
// Content type detectors
function isArray(str) {
return str.charAt(0) === '[';
}
function isObject(str) {
return str.charAt(0) === '{';
}
function isNumber(str) {
return !isNaN(Number(str));
}
/* --------------------------------------------------------------------------- */
// If we see our content types, then we can stop parsing and return what we get
function returnTerminalDatatype(str) {
var isItSpecial = ['true', 'false', 'null'].indexOf(str);
// console.log('isItSpecial=' + isItSpecial);
switch (isItSpecial) {
// First check for the specials because we don't want to coerce them to something else
case 0:
// it's true
output = true;
break;
case 1:
// it's false
output = false;
break;
case 2:
// it's a null
output = null;
break;
default:
// check for number after true/false because those will automatically coerce to number
if (isNumber(str)) {
output = Number(str);
} else if (isArray(str)) {
output = buildArray(str);
} else if (isObject(str)) {
output = buildObject(str);
} else {
// it's a lowly string
output = dequote(str);
}
break;
}
console.log('TerminalDataype=' + output);
return output;
}
/* --------------------------------------------------------------------------- */
// Build array or object
// This is the fun part!
function buildArray(str) {
var newArr = [];
var newVal;
var elstart = 1;
var elend = 1;
for (var i = 1; i < str.length; i++) {
if (str[i] === ',' || str[i] === ']') {
elend = i;
if (elend - elstart > 0) {
newVal = returnTerminalDatatype(str.substring(elstart,elend));
newArr.push(newVal);
}
elstart = i+1;
}
}
return newArr;
}
function buildObject(str) {
var newObj = {};
var newKey, newVal;
var elstart = 1;
var elend = 1;
// All this for 0-length strings
var contentsStr = str.substring(1,str.length-1);
contentsStr = contentsStr.trim();
console.log(contentsStr);
var contentsLength = contentsStr.length;
console.log(contentsLength);
if (contentsLength === 0) {
console.log('broke out');
return newObj;
}
for (var i = 0; i < str.length; i++) {
if (str[i] === ':') {
elend = i;
if (elend - elstart > 0) {
newKey = str.substring(elstart,elend).trim();
newKey = returnTerminalDatatype(newKey);
}
elstart = i+1;
}
if (str[i] === '}' || str[i] === ',') {
elend = i;
if (elend - elstart > 0) {
newVal = str.substring(elstart,elend).trim();
newVal = returnTerminalDatatype(newVal);
}
elstart = i+1;
}
newObj[newKey] = newVal;
}
return newObj;
}
/* --------------------------------------------------------------------------- */
// Now actually bring it all together
function JSONParser(string) {
return returnTerminalDatatype(string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment