Class
JsonParser
Description [src]
class Json.Parser : GObject.Object {
/* No available fields */
}
JsonParser provides an object for parsing a JSON data stream, either
inside a file or inside a static buffer.
Using JsonParser
The JsonParser API is fairly simple:
gboolean
parse_json (const char *filename)
{
g_autoptr(JsonParser) parser = json_parser_new ();
g_autoptr(GError) error = NULL
json_parser_load_from_file (parser, filename, &error);
if (error != NULL)
{
g_critical ("Unable to parse '%s': %s", filename, error->message);
return FALSE;
}
g_autoptr(JsonNode) root = json_parser_get_root (parser);
// manipulate the object tree from the root node
return TRUE
}
By default, the entire process of loading the data and parsing it is
synchronous; the json_parser_load_from_stream_async() API will
load the data asynchronously, but parse it in the main context as the
signals of the parser must be emitted in the same thread. If you do
not use signals, and you wish to also parse the JSON data without blocking,
you should use a GTask and the synchronous JsonParser API inside the
task itself.
Constructors
json_parser_new
Creates a new JSON parser.
json_parser_new_immutable
Creates a new parser instance with its JsonParser:immutable
property set to TRUE to create immutable output trees.
Available since: 1.2
Instance methods
json_parser_get_current_line
Retrieves the line currently parsed, starting from 1.
json_parser_get_current_pos
Retrieves the current position inside the current line, starting from 0.
json_parser_get_root
Retrieves the top level node from the parsed JSON stream.
json_parser_get_strict
Retrieves whether the parser is operating in strict mode.
Available since: 1.10
json_parser_has_assignment
A JSON data stream might sometimes contain an assignment, like:
Available since: 0.4
json_parser_load_from_data
Loads a JSON stream from a buffer and parses it.
json_parser_load_from_file
Loads a JSON stream from the content of filename and parses it.
json_parser_load_from_mapped_file
Loads a JSON stream from the content of filename and parses it.
Available since: 1.6
json_parser_load_from_stream
Loads the contents of an input stream and parses them.
Available since: 0.12
json_parser_load_from_stream_async
Asynchronously reads the contents of a stream.
Available since: 0.12
json_parser_load_from_stream_finish
Finishes an asynchronous stream loading started with
json_parser_load_from_stream_async().
Available since: 0.12
Properties
Json.Parser:immutable
Whether the tree built by the parser should be immutable when created.
Available since: 1.2
Json.Parser:strict
Whether the parser should be strictly conforming to the JSON format, or allow custom extensions like comments.
Available since: 1.10
Signals
Json.Parser::array-element
The ::array-element signal is emitted each time a parser
has successfully parsed a single element of a JSON array.
Deprecated since: 1.10
Json.Parser::array-end
The ::array-end signal is emitted each time a parser
has successfully parsed an entire JSON array.
Deprecated since: 1.10
Json.Parser::array-start
The ::array-start signal is emitted each time a parser
starts parsing a JSON array.
Deprecated since: 1.10
Json.Parser::error
The ::error signal is emitted each time a parser encounters
an error in a JSON stream.
Deprecated since: 1.10
Json.Parser::object-end
The ::object-end signal is emitted each time a parser
has successfully parsed an entire JSON object.
Deprecated since: 1.10
Json.Parser::object-member
The ::object-member signal is emitted each time a parser
has successfully parsed a single member of a JSON object.
Deprecated since: 1.10
Json.Parser::object-start
This signal is emitted each time a parser starts parsing a JSON object.
Deprecated since: 1.10
Json.Parser::parse-end
This signal is emitted when a parser successfully finished parsing a JSON data stream.
Deprecated since: 1.10
Json.Parser::parse-start
This signal is emitted when a parser starts parsing a JSON data stream.
Deprecated since: 1.10
Signals inherited from GObject (1)
GObject.Object::notify
The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.
Class structure
struct JsonParserClass {
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
);
void (* _json_reserved1) (
void
);
void (* _json_reserved2) (
void
);
void (* _json_reserved3) (
void
);
void (* _json_reserved4) (
void
);
void (* _json_reserved5) (
void
);
void (* _json_reserved6) (
void
);
void (* _json_reserved7) (
void
);
void (* _json_reserved8) (
void
);
}
Class members
parse_start |
|
Class handler for the JsonParser::parse-start signal. |
|
object_start |
|
Class handler for the JsonParser::object-start signal. |
|
object_member |
|
Class handler for the JsonParser::object-member signal. |
|
object_end |
|
Class handler for the JsonParser::object-end signal. |
|
array_start |
|
Class handler for the JsonParser::array-start signal. |
|
array_element |
|
Class handler for the JsonParser::array-element signal. |
|
array_end |
|
Class handler for the JsonParser::array-end signal. |
|
parse_end |
|
Class handler for the JsonParser::parse-end signal. |
|
error |
|
Class handler for the JsonParser::error signal. |
|
_json_reserved1 |
|
| No description available. | |
_json_reserved2 |
|
| No description available. | |
_json_reserved3 |
|
| No description available. | |
_json_reserved4 |
|
| No description available. | |
_json_reserved5 |
|
| No description available. | |
_json_reserved6 |
|
| No description available. | |
_json_reserved7 |
|
| No description available. | |
_json_reserved8 |
|
| No description available. |
Virtual methods
Json.ParserClass.array_element
Class handler for the JsonParser::array-element signal.
Json.ParserClass.array_end
Class handler for the JsonParser::array-end signal.
Json.ParserClass.array_start
Class handler for the JsonParser::array-start signal.
Json.ParserClass.error
Class handler for the JsonParser::error signal.
Json.ParserClass.object_end
Class handler for the JsonParser::object-end signal.
Json.ParserClass.object_member
Class handler for the JsonParser::object-member signal.
Json.ParserClass.object_start
Class handler for the JsonParser::object-start signal.
Json.ParserClass.parse_end
Class handler for the JsonParser::parse-end signal.
Json.ParserClass.parse_start
Class handler for the JsonParser::parse-start signal.