ported from json gem - pure ruby implementation