Right. So how does one produce a flat structure from a tree?
function* toJSON(x) {
if (typeof x === 'string') {
yield '"';
yield escape(x);
yield '"';
return;
}
if (typeof x === 'number') {
... }
if (typeof x === 'boolean'
...
}
yield '{';
let first = true;
for (let k in x) {
if (!first) yield ',';
first = false;
yield '"' + k + '": ';
yield* toJSON(x[k]);
}
yield '}';
}
Straightforward streaming of keys, values, delimiters. No one is aware of anyone's parents or children.
If we were to flatten it first, then somewhere need to distinguish that they are part of objects, so the correct keys can be produced in the output.
function* toJSON(k, x) {
if (typeof x === 'object') {
for (let k1 in x) {
yield* toJSON(k? k + '.' + k1: k1, x[k1]);
}
return;
}
if (k) {
yield '"';
yield k;
yield '":';
}
if (typeof x === 'string') {
yield '"';
yield escape(x);
yield '"';
return;
}
if (typeof x === 'number') {
... }
if (typeof x === 'boolean'
...
}
}
no subject
Date: 2017-04-06 07:27 pm (UTC)Straightforward streaming of keys, values, delimiters. No one is aware of anyone's parents or children.
If we were to flatten it first, then somewhere need to distinguish that they are part of objects, so the correct keys can be produced in the output.