Uhhh

JSON.stringify, what the HECK did you just do?

> c.values[0]
{ char: '10001',
pinyin: '',
english: [],
code: '00031',
cjkdecomp: [ 'ra', '㇑' ] }

> JSON.stringify(c.values[0])
'{"char":"10001","pinyin":"","english":"[\\n\\n]","code":"00031","cjkdecomp":"[\\n\\"ra\\",\\n\\"㇑\\"\\n]"}'

Did you just turn array-valued entities inside an object into string-valued entities....?

Is that... is that in the spec, somehow?

> c.values[20000]
{ char: '壜',
pinyin: 'tan2',
english: [ 'variant of 罈|坛[tan2]' ],
code: '058DC',
cjkdecomp: [ 'a', '土', '曇' ],
ids: [ '⿰土曇' ],
analysis: '⿰土曇',
kind: 's' }

>
JSON.stringify(c.values[20000])

'{"char":"壜","pinyin":"tan2","english":"[\\n\\"variant of 罈|坛[tan2]\\"\\n]","code":"058DC","cjkdecomp":"[\\n\\"a\\",\\n\\"土\\",\\n\\"曇\\"\\n]","ids":"[\\n\\"⿰土曇\\"\\n]","analysis":"⿰土曇","kind":"s"}'

How... how the heck am I supposed to parse that up again correctly....?

Surely any sane person asking 'convert this JSON to a string' would expect it to convert it to a string that resembles the actual JSON...?

But this seems to be recursing over objects and turning all subproperties, themselves, into strings.....?

Who ordered that?

Why is this the default 'to JSON' routine built into Node?

What's the rational explanation that I'm missing here?

Node 8.11.1, by the way. The current LTS. It's not an ancient one.

And yep, that output certainly does NOT JSON.parse() back as what went in.

> JSON.parse(x)
{ char: '壜',
pinyin: 'tan2',
english: '[\n"variant of 罈|坛[tan2]"\n]',
code: '058DC',
cjkdecomp: '[\n"a",\n"土",\n"曇"\n]',
ids: '[\n"⿰土曇"\n]',
analysis: '⿰土曇',
kind: 's' }
>

It turns out what I was missing from my JSON.stringify adventure was that evil mad science has its drawbacks.

Protip: although adding your own custom methods to Array.prototype may look neat and give you an exhilarating feeling of ABSOLUTE POWER, JSON.stringify does NOT like this and will turn all your arrays into strings if you do.

I don't know why it does this, probably out of sheer spite.

@natecull Why haven’t I heard of JavaScript prototypes being used as a malware vector?
@natecull How are you feeding this back in? It seems to be trying to escape newlines as well.

@Tablesaw @[email protected] ah, I think I know what happened.

My evil fed back on me.

I added custom methods to Array.prototype, and I guess that made JSON.stringify think my arrays had to be stringed.

sigh

Oh well.

@natecull @[email protected] I suppose you could try using eval() instead of JSON.parse().

@Tablesaw @[email protected]

and yep, removing my prototype extensions and putting them back as functions, stringify is good again

Sigh.

Well, it was clearly marked as evil, so I have nobody to blame but myself.

@natecull JSON.parse(JSON.stringify(x)) should give you a clone of x if it's a simple object like that

@natecull is this node or a browser?

if node make sure your locale is set properly. you can see if your terminal supports utf8 with:

echo -e '\xe2\x82\xac'

that should output "€"

if not you'll need to set LANG
export LANG=en_US.UTF-8

if browser add <meta charset=utf-8> to the <head> (before the <title> tag)

@natecull you can see your current locale with locale

@natecull oh it's not an encoding issue w

disregard

@natecull Rational?

THIS. IS. JAVASCRIPT!