This PR nominally implements load() and dump(), but the much bigger change is to the unit tests:
Read all test cases from JSON, so that this implementation and other implementations can share all the same test cases. Previously some tests were described in JSON, while many others weren't. The only non-language-agnostic tests now are those that deal with things specific to the python implementation, like the arguments to dumps().
Include information about expected errors (e.g. line number, message, etc.) in the JSON tests. This will ensure that every implementation produces consistent error messages.
Add some helper scripts to help organize and manage the JSON test cases.
Separate the test data from the test code (except for a few simple tests). This makes the tests easier to read, more consistent, and more thorough. It also makes it much easier to do things like test all of the test cases with all of the various ways of calling the load() and loads() functions, for example.
Add tests for load() and dump().
Note that I actually envision the tests/official_tests directory being broken out into it's own repository, then included back here as a git submodule. You'd have to be the one to create this repository, though.
The load() and dump() implementations are pretty simple, they just make sure to accept both paths and open files (or more precisely, anything that would be accepted by open() or that has a read()/write() method). I was careful to write the functions such to use duck-typing as much as possible, and to produce clear errors when given invalid input. I didn't implement the nifty idea of loading lines one the fly to save memory (although I imagine that the best way to do that would be to make a loadi() function that accepts an iterable of lines, then to have load() and loads() call that function with an open file object and str.splitlines(), respectively). To me, load() and dump() are important because it's far more common to want to read from a file than from a string, and it's obnoxious to have to type a whole with-block to do the most common thing. Plus, load() and dump() are universal components of these file format APIs (e.g. json, yaml, toml).
This PR nominally implements
load()
anddump()
, but the much bigger change is to the unit tests:Read all test cases from JSON, so that this implementation and other implementations can share all the same test cases. Previously some tests were described in JSON, while many others weren't. The only non-language-agnostic tests now are those that deal with things specific to the python implementation, like the arguments to
dumps()
.Include information about expected errors (e.g. line number, message, etc.) in the JSON tests. This will ensure that every implementation produces consistent error messages.
Add some helper scripts to help organize and manage the JSON test cases.
Separate the test data from the test code (except for a few simple tests). This makes the tests easier to read, more consistent, and more thorough. It also makes it much easier to do things like test all of the test cases with all of the various ways of calling the
load()
andloads()
functions, for example.Add tests for
load()
anddump()
.Note that I actually envision the
tests/official_tests
directory being broken out into it's own repository, then included back here as a git submodule. You'd have to be the one to create this repository, though.The
load()
anddump()
implementations are pretty simple, they just make sure to accept both paths and open files (or more precisely, anything that would be accepted byopen()
or that has aread()/write()
method). I was careful to write the functions such to use duck-typing as much as possible, and to produce clear errors when given invalid input. I didn't implement the nifty idea of loading lines one the fly to save memory (although I imagine that the best way to do that would be to make aloadi()
function that accepts an iterable of lines, then to haveload()
andloads()
call that function with an open file object andstr.splitlines()
, respectively). To me,load()
anddump()
are important because it's far more common to want to read from a file than from a string, and it's obnoxious to have to type a whole with-block to do the most common thing. Plus,load()
anddump()
are universal components of these file format APIs (e.g.json
,yaml
,toml
).