Commit e4de647
authored
Major lljson rework to make round-trippable serialization easier (#61)
Of note, there's now a notion of replacer and reviver callbacks as in
JS' `JSON` APIs. An example of their use is in the tests folder as
`lljson_typedjson.lua`.
We went with this form since it allows constructing a different representation
of the object before serializing without requiring you to construct an entire,
serializable copy before calling `lljson.encode()`. That allows you to save memory,
since the serializable version of each object only need to be alive as long
as we're still traversing the object.
Additionally, an empty table is now encoded as `[]` by default. This is
probably the most common meaning for an empty table, but you can also
apply `object_mt` as a metatable or add `__jsonhint="object"` to your
own metatable to force serialization as an object. Similarly, `array_mt`
or `__jsonhint="array"` will act as a hint to treat your object as an array.
`__len` should no longer be used as a hint that the object should be treated
as an array, that's what `__jsonhint` is for.
Also added a new options table format to `lljson.encode()` and friends. The
table now allows you to specify that `__tojson` hooks should be skipped, so
you can manually invoke them at your leisure in your replacer hooks.
Sparse tables are better-handled because of these changes, and
having object-like tables with numeric or `uuid` keys is now allowed,
though in the numeric case you must set that `object_mt` metatable
so the serializer knows not to treat it as a sparse table.1 parent 7368193 commit e4de647
File tree
10 files changed
+2287
-924
lines changed- VM
- include
- src
- cjson
- tests
- conformance
10 files changed
+2287
-924
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
| 7 | + | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3034 | 3034 | | |
3035 | 3035 | | |
3036 | 3036 | | |
| 3037 | + | |
| 3038 | + | |
| 3039 | + | |
| 3040 | + | |
| 3041 | + | |
3037 | 3042 | | |
3038 | 3043 | | |
3039 | 3044 | | |
| |||
0 commit comments