Json.Parser¶
- Subclasses:
None
Methods¶
- Inherited:
- Structs:
class |
|
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Virtual Methods¶
- Inherited:
|
|
|
|
|
|
|
|
|
|
|
|
Properties¶
Name |
Type |
Flags |
Short Description |
|---|---|---|---|
r/w/co |
|||
r/w/en |
Signals¶
- Inherited:
Name |
Short Description |
|---|---|
The |
|
The |
|
The |
|
The |
|
The |
|
The |
|
This signal is emitted each time a parser starts parsing a JSON object. |
|
This signal is emitted when a parser successfully finished parsing a JSON data stream. |
|
This signal is emitted when a parser starts parsing a JSON data stream. |
Fields¶
- Inherited:
Name |
Type |
Access |
Description |
|---|---|---|---|
parent_instance |
r |
Class Details¶
- class Json.Parser(**kwargs)¶
- Bases:
- Abstract:
No
- Structure:
JsonParserprovides an object for parsing a JSON data stream, either inside a file or inside a static buffer.- Using
Json.Parser
The
JsonParserAPI is fairly simple:```c
boolparse_json (conststr*filename) { g_autoptr(Json.Parser) parser =Json.Parser.new(); g_autoptr(GLib.Error) error =NoneJson.Parser.load_from_file(parser, filename, &error); if (error !=None) { g_critical (“Unable to parse ‘%s’: %s”, filename, error->message); returnFalse; }g_autoptr(
Json.Node) 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 [method`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
GTaskand the synchronousJsonParserAPI inside the task itself.- classmethod new()¶
- Returns:
the newly created parser
- Return type:
Creates a new JSON parser.
You can use the
JsonParserto load a JSON stream from either a file or a buffer and then walk the hierarchy using the data types API.
- classmethod new_immutable()¶
- Returns:
the newly created parser
- Return type:
Creates a new parser instance with its [property`Json`.Parser:immutable] property set to
TRUEto create immutable output trees.New in version 1.2.
- get_current_line()¶
- Returns:
the currently parsed line, or 0.
- Return type:
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 the parser will yield 0.
- get_current_pos()¶
- Returns:
the position in the current line, or 0.
- Return type:
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 the parser will yield 0.
- get_root()¶
-
Retrieves the top level node from the parsed JSON stream.
If the parser input was an empty string, or if parsing failed, the root will be
NULL. It will also beNULLif it has been stolen using [method`Json`.Parser.steal_root].
- get_strict()¶
- Returns:
true if the parser is strict, and false otherwise
- Return type:
Retrieves whether the parser is operating in strict mode.
New in version 1.10.
- has_assignment()¶
- Returns:
TRUEif there was an assignment, andFALSEotherwise- variable_name:
the variable name
- Return type:
A JSON data stream might sometimes contain an assignment, like:
`` var _json_data = { “member_name” : [ … ``
even though it would technically constitute a violation of the RFC.
JsonParserwill ignore the left hand identifier and parse the right hand value of the assignment.JsonParserwill record, though, the existence of the assignment in the data stream and the variable name used.New in version 0.4.
- load_from_data(data, length)¶
- Parameters:
- Raises:
- Returns:
TRUEif the buffer was succesfully parsed- Return type:
Loads a JSON stream from a buffer and parses it.
You can call this function multiple times with the same parser, but the contents of the parser will be destroyed each time.
- load_from_file(filename)¶
- Parameters:
filename (
str) – the path for the file to parse- Raises:
- Returns:
TRUEif the file was successfully loaded and parsed.- Return type:
Loads a JSON stream from the content of
filenameand parses it.If the file is large or shared between processes, [method`Json`.Parser.load_from_mapped_file] may be a more efficient way to load it.
See also: [method`Json`.Parser.load_from_data]
- load_from_mapped_file(filename)¶
- Parameters:
filename (
str) – the path for the file to parse- Raises:
- Returns:
TRUEif the file was successfully loaded and parsed.- Return type:
Loads a JSON stream from the content of
filenameand parses it.Unlike [method`Json`.Parser.load_from_file],
filenamewill be memory mapped as read-only and parsed.filenamewill be unmapped before this function returns.If mapping or reading the file fails, a
G_FILE_ERRORwill be returned.New in version 1.6.
- load_from_stream(stream, cancellable)¶
- Parameters:
stream (
Gio.InputStream) – the input stream with the JSON datacancellable (
Gio.CancellableorNone) – aGio.Cancellable
- Raises:
- Returns:
TRUEif the data stream was successfully read and parsed, andFALSEotherwise- Return type:
Loads the contents of an input stream and parses them.
If
cancellableis notNULL, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled,G_IO_ERROR_CANCELLEDwill be set on the givenerror.New in version 0.12.
- load_from_stream_async(stream, cancellable, callback, *user_data)¶
- Parameters:
stream (
Gio.InputStream) – the input stream with the JSON datacancellable (
Gio.CancellableorNone) – aGio.Cancellablecallback (
Gio.AsyncReadyCallbackorNone) – the function to call when the request is satisfied
Asynchronously reads the contents of a stream.
For more details, see [method`Json`.Parser.load_from_stream], which is the synchronous version of this call.
When the operation is finished, callback will be called. You should then call [method`Json`.Parser.load_from_stream_finish] to get the result of the operation.
New in version 0.12.
- load_from_stream_finish(result)¶
- Parameters:
result (
Gio.AsyncResult) – the result of the asynchronous operation- Raises:
- Returns:
TRUEif the content of the stream was successfully retrieved and parsed, andFALSEotherwise- Return type:
Finishes an asynchronous stream loading started with [method`Json`.Parser.load_from_stream_async].
New in version 0.12.
- set_strict(strict)¶
- Parameters:
strict (
bool) – whether the parser should be strict
Sets whether the parser should operate in strict mode.
If strict is true,
JsonParserwill strictly conform to the JSON format.If strict is false,
JsonParserwill allow custom extensions to the JSON format, like comments.New in version 1.10.
- steal_root()¶
-
Steals the top level node from the parsed JSON stream.
This will be
NULLin the same situations as [method`Json`.Parser.get_root] returnNULL.New in version 1.4.
- do_array_element(array, index_) virtual¶
- Parameters:
array (
Json.Array) –index (
int) –
class handler for the
Json.Parser::array-elementsignal
- do_array_end(array) virtual¶
- Parameters:
array (
Json.Array) –
class handler for the
Json.Parser::array-endsignal
- do_array_start() virtual¶
class handler for the
Json.Parser::array-startsignal
- do_error(error) virtual¶
- Parameters:
error (
GLib.Error) –
class handler for the
Json.Parser::errorsignal
- do_object_end(object) virtual¶
- Parameters:
object (
Json.Object) –
class handler for the
Json.Parser::object-endsignal
- do_object_member(object, member_name) virtual¶
- Parameters:
object (
Json.Object) –member_name (
str) –
class handler for the
Json.Parser::object-membersignal
- do_object_start() virtual¶
class handler for the
Json.Parser::object-startsignal
- do_parse_end() virtual¶
class handler for the
Json.Parser::parse-endsignal
- do_parse_start() virtual¶
class handler for the
Json.Parser::parse-startsignal
Signal Details¶
- Json.Parser.signals.array_element(parser, array, index_)¶
- Signal Name:
array-element- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signalarray (
Json.Array) – a JSON arrayindex (
int) – the index of the newly parsed array element
The
::array-elementsignal is emitted each time a parser has successfully parsed a single element of a JSON array.Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.array_element] virtual function
- Json.Parser.signals.array_end(parser, array)¶
- Signal Name:
array-end- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signalarray (
Json.Array) – the parsed JSON array
The
::array-endsignal is emitted each time a parser has successfully parsed an entire JSON array.Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.array_end] virtual function
- Json.Parser.signals.array_start(parser)¶
- Signal Name:
array-start- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signal
The
::array-startsignal is emitted each time a parser starts parsing a JSON array.Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.array_start] virtual function
- Json.Parser.signals.error(parser, error)¶
- Signal Name:
error- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signal
The
::errorsignal is emitted each time a parser encounters an error in a JSON stream.Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.error] virtual function
- Json.Parser.signals.object_end(parser, object)¶
- Signal Name:
object-end- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signalobject (
Json.Object) – the parsed JSON object
The
::object-endsignal is emitted each time a parser has successfully parsed an entire JSON object.Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.object_end] virtual function
- Json.Parser.signals.object_member(parser, object, member_name)¶
- Signal Name:
object-member- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signalobject (
Json.Object) – the JSON object being parsedmember_name (
str) – the name of the newly parsed member
The
::object-membersignal is emitted each time a parser has successfully parsed a single member of a JSON object.Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.object_member] virtual function
- Json.Parser.signals.object_start(parser)¶
- Signal Name:
object-start- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signal
This signal is emitted each time a parser starts parsing a JSON object.
Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.object_start] virtual function
- Json.Parser.signals.parse_end(parser)¶
- Signal Name:
parse-end- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signal
This signal is emitted when a parser successfully finished parsing a JSON data stream.
Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.parse_end] virtual function
- Json.Parser.signals.parse_start(parser)¶
- Signal Name:
parse-start- Flags:
- Parameters:
parser (
Json.Parser) – The object which received the signal
This signal is emitted when a parser starts parsing a JSON data stream.
Deprecated since version 1.10: Derive your own parser type from
JsonParserand override the [vfunc`Json`.Parser.parse_start] virtual function
Property Details¶
- Json.Parser.props.immutable¶
- Name:
immutable- Type:
- Default Value:
- Flags:
Whether the tree built by the parser should be immutable when created.
Making the output immutable on creation avoids the expense of traversing it to make it immutable later.
New in version 1.2.