summaryrefslogtreecommitdiff
path: root/json-glib/json-parser.c
Commit message (Collapse)AuthorAgeFilesLines
* Abstract the loading code into its own functionerror-locationEmmanuele Bassi2008-11-281-72/+103
| | | | | | | | | | | | | | | | | | The load_from_file() method must set the is_filename/filename fields of the JsonParserPrivate structure, so that the error handler can use them to print out the file, as well as the line in case of error. Since load_from_data() needs to unset those two fields, to avoid printing invalid/stale information, we need to have a generic "load" function that can be invoked by both load_from_data() and load_from_file(), and leave the JsonParser object set up to those two methods. Hence, a private json_parser_load() has been added, moving most of the code out of json_parser_load_from_data(). This function does not perform type checks and requires that the length of the memory buffer containing the JSON data stream is already a positive integer.
* Display the filename inside error messagesEmmanuele Bassi2008-11-281-2/+26
| | | | | | | | | | | | | | | | | | | | Instead of just relaying the line number both when parsing files and memory buffers, JsonParser should also print out the file name in case it is available. The error message format should be make-like and emacs-friendly, that is: filename:line_number: error message so that editors and development environments can parse the errors easily. This commit adds the filename string, and a boolean flag for checking whether the filename is set, inside the JsonParser private data structure. The boolean flag is checked inside the JsonScanner error handler when populating the GError or when printing the warning directly on stderr.
* Update JsonParser to fix compilationEmmanuele Bassi2008-02-291-16/+17
| | | | JsonParser now uses JsonScanner, the internal copy of GScanner.
* Use JsonScanner API into JsonParserEmmanuele Bassi2008-02-291-83/+44
| | | | | | | Rename the GScanner function calls into JsonScanner, to see what we actually need to have public. Also, remove the configuration: JsonScanner will provide us with one.
* Include JsonScanner into JsonParserEmmanuele Bassi2008-02-291-0/+1
|
* Revert JsonParser::get_root semanticsEmmanuele Bassi2008-01-291-24/+2
| | | | | | | | | The get_root() method should not return a copy of the parsed node: it is up to the developer copying it, if it needs to be kept around across multiple parsing runs. This commit reverts the 0b6b09c0 commit, by removing the peek_root() method and restoring the previous get_root() method behaviour.
* Extract the parser state clearing into its own functionEmmanuele Bassi2008-01-271-21/+30
| | | | | | | | The JsonParser object clears its state upon starting the parsing sequence or during the instance destruction process. It's worth moving the free and unref calls into their own function to be called by the load_from_data() and dispose methods. As further optimisation, inlining them should be worth as well.
* Kill off a few indirectionsEmmanuele Bassi2007-12-251-11/+14
| | | | Use an intermediary pointer to avoid a lot of pointer dereferences
* Plug a leak when loading a new buffer with the same parserEmmanuele Bassi2007-12-251-0/+2
| | | | | | When the same JsonParser loads a different buffer it needs to clear out the current state; this means clearing the variable_name string it saves when the passed JSON stream is a JavaScript assignment.
* Use gssize for the buffer length parameterEmmanuele Bassi2007-12-251-5/+5
| | | | | | | Since we allow a negative value, meaning "take the whole string", for the length parameter, when need a signed size_t. This also fixes the bug where we implicitly always computed the buffer length and discarded the passed length parameter.
* Change json_parser_get_root() semanticsEmmanuele Bassi2007-11-211-2/+24
| | | | | | | | | | | | | The json_parser_get_root() returns a pointer to the root node. This does not conform to the API naming convention inherited from GLib, where functions returning an internal pointer are called "peek" and function returning a copy are called "get". Thus, json_parser_get_root() will now return a copy of the root node and it is left to the developer to free the returned JsonNode. A function returning the pointer has also been added, and it's called json_parser_peek_root().
* Fix variable shadowing for distcheckEmmanuele Bassi2007-11-131-2/+2
|
* When parsing a full assignment, also swallow any trailing semi-colonEmmanuele Bassi2007-11-101-1/+11
| | | | | | There's no end to web developers insanity when they can twist and turn a perfectly simple and elegant notation into something that allows lazyness and breakage.
* Add support for parsing assignmentsEmmanuele Bassi2007-11-101-2/+78
| | | | | | | | | | | | | | | | | Some JSON web APIs return a full JavaScript assignment instead of just the data structure (and I'm looking at you, Tumblr). This is done in clear violation of the grammar published in the RFC 4627, and it's evidently done because most web developers are too lazy for doing a var foo = eval('(' + text ')'); and want everything defined for them. JsonParser will blissfully ignore the left-hand part of the assignment but, in the interest of the developer who cares, will record: 1. that the parsed statement was in fact an assignment and 2. the name of the variable used. The function json_parser_has_assignment() can be used to query both the presence of an assignment and the variable name at the same time.
* More sanity checks for objectsEmmanuele Bassi2007-10-291-1/+7
| | | | Add a check for commas after complex data types inside object members.
* Add more sanity checks to the parserEmmanuele Bassi2007-10-291-0/+10
| | | | Catch missing commas after an array element or an object member.
* Backport fixes from Clutter trunkEmmanuele Bassi2007-10-291-20/+103
| | | | | | | | | | The copy of JSON-GLib in Clutter trunk has two fixes for the JsonParser object: * support for negative numbers (ints and floats); * correct parse error propagation which should make the JsonParser hopefully complete.
* Fix compilation errors with extra maintainer CFLAGSEmmanuele Bassi2007-10-161-2/+2
|
* Parse bare root valuesEmmanuele Bassi2007-10-101-50/+29
| | | | | | | | If the root node contains just a bare value (true, false, null, fundamental type) then it's still valid JSON. Also, use the commodity JsonNode API to avoid using values in the parser code.
* Use doubles when parsing, not floatsEmmanuele Bassi2007-10-051-4/+4
| | | | | | GScanner advertise the floating point values as floats, but it really uses doubles. Hence, we need to use G_TYPE_DOUBLE when saving the parsed constants into a GValue.
* Add line/position getters to JsonParserEmmanuele Bassi2007-10-051-3/+43
| | | | | | Add two methods to JsonParser to retrieve the currently parsed line and position within that line. These methods works only while parsing, so within the signal handlers and inside subclasses.
* Document the newly added signalsEmmanuele Bassi2007-10-051-0/+64
|
* Add more signals to the JsonParser classEmmanuele Bassi2007-10-051-3/+115
| | | | | | | JsonParser should emit signals in critical places, like: start/end of the parsing process; start and end of a JsonObject and a JsonArray; completion of every member and element of a JsonObject and a JsonArray. These signals require the addition of some custom marshaller.
* Move data types ctors and setters into the public headersEmmanuele Bassi2007-10-011-1/+0
| | | | | | | Now that we are providing a generator class we need to provide the constructors and setters for JsonNode, JsonObject and JsonArray. This also means that the json-private.h header is now useless, so we can remove it from the build and repository.
* Add licensing informations to the source codeEmmanuele Bassi2007-10-011-2/+26
|
* Swallow the comma earlier in the parserEmmanuele Bassi2007-10-011-9/+16
| | | | | The comma is used as a member and element separator, so it should be swallowed by the parser as soon as possible.
* Add JSON object parsingEmmanuele Bassi2007-10-011-15/+220
| | | | | | This commit completes the JsonParser class by adding the ability to parse JSON objects, either alone or inside arrays. JsonParser is now a JSON parser.
* Chain up nodes to their parentEmmanuele Bassi2007-10-011-1/+5
| | | | | When parsing a JSON node we need to correctly set up the parent node of the newly created ones.
* Add JsonNode, a generic container for JSON typesEmmanuele Bassi2007-10-011-23/+64
| | | | | | | | | | | | | | | | | This huge commit removes JsonData and adds JsonNode, the generic container for fundamental and complex data types extracted from a JSON stream. The contents of a JsonNode can be extracted from it in form of a GValue for fundamental types (integers, floats, strings, booleans) or in form of JsonObject and JsonArray objects. JsonObject and JsonArray now accept JsonNodes instead of GValues. The JsonParser object builds the data model tree when parsing a JSON stream; the tree can be recursed by getting the root node and walking it using the GValue API for the fundamental types and the objects/arrays API for complex types. The API has been updated and the tests now recurse through the generated data model tree.
* Skip the token check after parsing a nested arrayEmmanuele Bassi2007-09-211-0/+2
| | | | | | Since there cannot be any other token except for the comma, which we eat anyway, there's no point in going through the switch() check after we have finished parsing a nested array.
* Parse JSON arraysEmmanuele Bassi2007-09-211-17/+97
| | | | | | | Add the array parsing code. The parser identifies and builds nested levels of arrays, but the storage is not quite right. That is a problem of the parser object, though, so this can be considered a first raw pass at the problem.
* Top-levels in JSON can only be objects or arraysEmmanuele Bassi2007-09-211-10/+14
| | | | | JSON is an object serialisation format (thanks, RFC4627), so it can only express objects and/or arrays as top-levels.
* Add stubs to JsonParser for actually parsing a JSON streamEmmanuele Bassi2007-09-211-1/+265
| | | | | | | | | | | | | | | | | Initial commit for getting JsonParser to work. Because GScanner API is old and mostly sucks, we need to do some magic with signals. If json_parser_load_from_data() fails, the passed GError will be set with a JSON_PARSER_ERROR code and message; unfortunately, we can't get the nice error message out of GScanner. We can, however, override the default message handler and make it emit a signal on the JsonParser object. So, to make a long story short: the GError passed to the load_from_data() method is filled with a short error message; the *real* error message is passed to the ::error signal handlers so they can actually use it. GScanner should really get a way to retrieve the last error message.
* Initial import of JSON-GLibEmmanuele Bassi2007-09-201-0/+152
JSON-GLib is a JSON parser library written with GLib and GObject. JSON is the JavaScript Object Notation, and it's used to define objects and object hierarchies in a human-readable way.