Refactor json_pack()

* Implement a "scanner" that reads the format string, maintaining state

* Split json_vnpack() to three separate functions for packing objects,
  arrays and simple values. This makes it more clear what is being
  packed, and the object and array structures become more evident.

* Make the skipping of ignored character simpler, i.e. skip ':' and
  ',' independent of their context

This patch shaves around 80 lines of code from the original
implementation.
This commit is contained in:
Petri Lehtinen
2011-01-23 21:14:19 +02:00
parent 53383860e8
commit 7f3018a4fb
4 changed files with 176 additions and 253 deletions

View File

@@ -191,7 +191,7 @@ int main()
if(json_pack(&error, "{ s: {}, s:[ii{} }", "foo", "bar", 12, 13))
fail("json_pack failed to catch missing ]");
if(error.line != 1 || error.column != 13)
if(error.line != 1 || error.column != 19)
fail("json_pack didn't get the error coordinates right!");
if(json_pack(&error, "[[[[[ [[[[[ [[[[ }]]]] ]]]] ]]]]]"))