![]() |
![]() |
![]() |
JSON-GLib 0.6.2 Reference Manual | ![]() |
---|---|---|---|---|
enum JsonParserError; JsonParser; JsonParserClass; JsonParser* json_parser_new (void); gboolean json_parser_load_from_file (JsonParser *parser, const gchar *filename, GError **error); gboolean json_parser_load_from_data (JsonParser *parser, const gchar *data, gssize length, GError **error); JsonNode* json_parser_get_root (JsonParser *parser); guint json_parser_get_current_line (JsonParser *parser); guint json_parser_get_current_pos (JsonParser *parser); gboolean json_parser_has_assignment (JsonParser *parser, gchar **variable_name);
"array-element" : Run Last "array-end" : Run Last "array-start" : Run Last "error" : Run Last "object-end" : Run Last "object-member" : Run Last "object-start" : Run Last "parse-end" : Run Last "parse-start" : Run Last
JsonParser provides an object for parsing a JSON data stream, either inside a file or inside a static buffer.
typedef enum { JSON_PARSER_ERROR_PARSE, JSON_PARSER_ERROR_UNKNOWN } JsonParserError;
Error enumeration for JsonParser
typedef struct _JsonParser JsonParser;
JSON data streams parser. The contents of the JsonParser structure are private and should only be accessed via the provided API.
typedef struct { void (* parse_start) (JsonParser *parser); void (* object_start) (JsonParser *parser); void (* object_member) (JsonParser *parser, JsonObject *object, const gchar *member_name); void (* object_end) (JsonParser *parser, JsonObject *object); void (* array_start) (JsonParser *parser); void (* array_element) (JsonParser *parser, JsonArray *array, gint index_); void (* array_end) (JsonParser *parser, JsonArray *array); void (* parse_end) (JsonParser *parser); void (* error) (JsonParser *parser, const GError *error); } JsonParserClass;
JsonParser class.
|
class handler for the JsonParser::parse-start signal |
|
class handler for the JsonParser::object-start signal |
|
class handler for the JsonParser::object-member signal |
|
class handler for the JsonParser::object-end signal |
|
class handler for the JsonParser::array-start signal |
|
class handler for the JsonParser::array-element signal |
|
class handler for the JsonParser::array-end signal |
|
class handler for the JsonParser::parse-end signal |
|
class handler for the JsonParser::error signal |
JsonParser* json_parser_new (void);
Creates a new JsonParser instance. You can use the JsonParser to load a JSON stream from either a file or a buffer and then walk the hierarchy using the data types API.
Returns : |
the newly created JsonParser. Use g_object_unref()
to release all the memory it allocates.
|
gboolean json_parser_load_from_file (JsonParser *parser, const gchar *filename, GError **error);
Loads a JSON stream from the content of filename
and parses it. See
json_parser_load_from_data()
.
|
a JsonParser |
|
the path for the file to parse |
|
return location for a GError, or NULL
|
Returns : |
TRUE if the file was successfully loaded and parsed.
In case of error, error is set accordingly and FALSE is returned
|
gboolean json_parser_load_from_data (JsonParser *parser, const gchar *data, gssize length, GError **error);
Loads a JSON stream from a buffer and parses it. You can call this function multiple times with the same JsonParser object, but the contents of the parser will be destroyed each time.
|
a JsonParser |
|
the buffer to parse |
|
the length of the buffer, or -1 |
|
return location for a GError, or NULL
|
Returns : |
TRUE if the buffer was succesfully parser. In case
of error, error is set accordingly and FALSE is returned
|
JsonNode* json_parser_get_root (JsonParser *parser);
Retrieves the top level node from the parsed JSON stream.
|
a JsonParser |
Returns : |
the root JsonNode . The returned node is owned by the JsonParser and should never be modified or freed. |
guint json_parser_get_current_line (JsonParser *parser);
Retrieves the line currently parsed, starting from 1.
This function has defined behaviour only while parsing; calling this function from outside the signal handlers emitted by JsonParser will yield 0.
|
a JsonParser |
Returns : |
the currently parsed line, or 0. |
guint json_parser_get_current_pos (JsonParser *parser);
Retrieves the current position inside the current line, starting from 0.
This function has defined behaviour only while parsing; calling this function from outside the signal handlers emitted by JsonParser will yield 0.
|
a JsonParser |
Returns : |
the position in the current line, or 0. |
gboolean json_parser_has_assignment (JsonParser *parser, gchar **variable_name);
A JSON data stream might sometimes contain an assignment, even though it would technically constitute a violation of the RFC. JsonParser will ignore the left hand identifier and parse the right hand value of the assignment. JsonParser will record, though, the existence of the assignment in the data stream and the variable name used.
|
a JsonParser |
|
return location for the variable name, or NULL
|
Returns : |
TRUE if there was an assignment, FALSE otherwise. If
variable_name is not NULL it will be set to the name of the variable
used in the assignment. The string is owned by JsonParser and should
never be modified or freed.
|
Since 0.4
"array-element"
signalvoid user_function (JsonParser *parser, JsonArray *array, gint index_, gpointer user_data) : Run Last
The ::array-element signal is emitted each time the JsonParser has successfully parsed a single element of a JsonArray. The array and element index are passed to the signal handlers.
|
the JsonParser that received the signal |
|
a JsonArray |
|
the index of the newly parsed element |
|
user data set when the signal handler was connected. |
"array-end"
signalvoid user_function (JsonParser *parser, JsonArray *array, gpointer user_data) : Run Last
The ::array-end signal is emitted each time the JsonParser has successfully parsed an entire JsonArray
|
the JsonParser that received the signal |
|
the parsed JsonArrary |
|
user data set when the signal handler was connected. |
"array-start"
signalvoid user_function (JsonParser *parser, gpointer user_data) : Run Last
The ::array-start signal is emitted each time the JsonParser starts parsing a JsonArray
|
the JsonParser that received the signal |
|
user data set when the signal handler was connected. |
"error"
signalvoid user_function (JsonParser *parser, gpointer error, gpointer user_data) : Run Last
The ::error signal is emitted each time a JsonParser encounters an error in a JSON stream.
|
the parser instance that received the signal |
|
a pointer to the GError |
|
user data set when the signal handler was connected. |
"object-end"
signalvoid user_function (JsonParser *parser, JsonObject *object, gpointer user_data) : Run Last
The ::object-end signal is emitted each time the JsonParser has successfully parsed an entire JsonObject.
|
the JsonParser that received the signal |
|
the parsed JsonObject |
|
user data set when the signal handler was connected. |
"object-member"
signalvoid user_function (JsonParser *parser, JsonObject *object, gchar *member_name, gpointer user_data) : Run Last
The ::object-member signal is emitted each time the JsonParser has successfully parsed a single member of a JsonObject. The object and member are passed to the signal handlers.
|
the JsonParser that received the signal |
|
a JsonObject |
|
the name of the newly parsed member |
|
user data set when the signal handler was connected. |
"object-start"
signalvoid user_function (JsonParser *parser, gpointer user_data) : Run Last
The ::object-start signal is emitted each time the JsonParser starts parsing a JsonObject.
|
the JsonParser that received the signal |
|
user data set when the signal handler was connected. |
"parse-end"
signalvoid user_function (JsonParser *parser, gpointer user_data) : Run Last
The ::parse-end signal is emitted when the parser successfully finished parsing a JSON data stream
|
the JsonParser that received the signal |
|
user data set when the signal handler was connected. |
"parse-start"
signalvoid user_function (JsonParser *parser, gpointer user_data) : Run Last
The ::parse-start signal is emitted when the parser began parsing a JSON data stream.
|
the JsonParser that received the signal |
|
user data set when the signal handler was connected. |