Basic JSON Parser

by Andrew Gerst

JavaScript

const data = '{"bool-true":true,"bool-false":false,"null-key":null,"number-key":1234567890,"string-key":"string","key2":"value2","advancedkey":"\'01!@#$%^&*(){}[]/?+=_-\|!~`<>,.:;`","number-key2":0987654321,"float-key":3.14159,"elog+":5.3e+5,"elog-":13.2E-6,"negative-number":-365,"bool-ending":true}';

let output = {};
let previousKey = '';
let tokenValue = '';
let tokenType = '';
const primitiveMap = {
	'true': true,
  'false': false,
  'null': null
};

for (let i = 0; i < data.length; i++) {
  if (tokenType === 'primitive') {
    if (data[i] === ',' || data[i] === '}') {
      output[previousKey] = primitiveMap[tokenValue];
      previousKey = '';
      tokenType = '';
      tokenValue = '';
    } else {
    	continue;
    }
  }
  if (tokenType === '' && (data[i] === '{' || data[i] === '}' || data[i] === ',')) {
    continue;
  }
  if (tokenType === '' || tokenType === 'primitive') {
  	if (tokenType === 'primitive') { continue; }
  	if (data[i] === 't' || data[i] === 'f' || data[i] === 'n') {
			tokenType = 'primitive';
      if (data[i] === 't' && data[i + 1] === 'r' && data[i + 2] === 'u' && data[i + 3] === 'e') {
      	tokenValue = 'true';
      }
      if (data[i] === 'f' && data[i + 1] === 'a' && data[i + 2] === 'l' && data[i + 3] === 's' && data[i + 4] === 'e') {
      	tokenValue = 'false';
      }
      if (data[i] === 'n' && data[i + 1] === 'u' && data[i + 2] === 'l' && data[i + 3] === 'l') {
      	tokenValue = 'null';
      }
      continue;
    }
  }
  if (tokenType === 'number' && !isNumberChar(data[i])) {
  	output[previousKey] = parseFloat(tokenValue);
    previousKey = '';
    tokenValue = '';
    tokenType = '';
    continue;
  }
  if ((tokenType === '' || tokenType === 'number') && isNumberChar(data[i])) {
	  tokenType = 'number';
    tokenValue += data[i];
    continue;
  }
  if (data[i] === '"') {
  	if (data[i - 1] !== ':' && tokenType !== 'string-key' && tokenType !== 'string-value') {
    	tokenType = 'string-key';
    } else if (data[i...