Closed mrjjwright closed 3 years ago
@mrjjwright circular references are handled quite gracefully actually. You just need to define the target type you want them to be serialized with.
const stringify = sjs({
a: attr('boolean'),
b: attr('number'),
circular: attr('string'), // <= Target type
});
const obj = {
a: true,
b: 42,
}
obj.circular = obj;
const str = stringify(obj);
// => {"a":true,"b":42,"circular":"[object Object]"}
But you can also customize how the serialization is handled in the first place.
const stringify = sjs({
a: attr('boolean'),
b: attr('number'),
circular: attr('string', () => undefined),
});
const str = stringify(obj);
// => {"a":true,"b":42}
const stringify = sjs({
a: attr('boolean'),
b: attr('number'),
circular: attr('string', () => '<circular>'),
});
const str = stringify(obj);
// => {"a":true,"b":42,"circular":"<circular>"}
Freaking awesome, thanks!!
Can I serialize recursive schemas/structures?
What is the difference between a circular structure and a recursive structure? Could you provide an example?
I just need to serialize a tree like structure that I don't know the exact shape of but I do know that it repeats and nests smaller structures that have a known schema.
@mrjjwright In that case the solution would be a bit more involved. A possible approach I can see is:
const serializer1 = sjs({ ... })
const serializer2 = sjs({ ... })
const mainSchema = sjs({
unknownNested: attr('null', (value) => {
if (type of value === 'type1') {
return serializer1(value)
}
return serializer2(value)
})
})
Note that this approach can keep nesting indefinitely. As any schema can keep referring to other schemas.
Also, I used the null
value in the attr
helper as this approach was not thought as out of the box.
I opened an issue for discussing optimizations for serializing Objects with unknow shapes here https://github.com/lucagez/slow-json-stringify/issues/28. Feel free to leave suggestions there if you have ideas!
This makes sense to me and should work great, thanks!
thank you!