Json.Parser¶
| Subclasses: | None | 
|---|
Methods¶
| Inherited: | GObject.Object (37) | 
|---|---|
| Structs: | GObject.ObjectClass (5) | 
| class | new() | 
| class | new_immutable() | 
| get_current_line() | |
| get_current_pos() | |
| get_root() | |
| has_assignment() | |
| load_from_data(data, length) | |
| load_from_file(filename) | |
| load_from_mapped_file(filename) | |
| load_from_stream(stream, cancellable) | |
| load_from_stream_async(stream, cancellable, callback, *user_data) | |
| load_from_stream_finish(result) | |
| steal_root() | 
Virtual Methods¶
| Inherited: | GObject.Object (7) | 
|---|
| do_array_element(array, index_) | |
| do_array_end(array) | |
| do_array_start() | |
| do_error(error) | |
| do_object_end(object) | |
| do_object_member(object, member_name) | |
| do_object_start() | |
| do_parse_end() | |
| do_parse_start() | 
Properties¶
| Name | Type | Flags | Short Description | 
|---|---|---|---|
| immutable | bool | r/w/co | Whether the parser output is immutable. | 
Signals¶
| Inherited: | GObject.Object (1) | 
|---|
| Name | Short Description | 
|---|---|
| array-element | The ::array-elementsignal is emitted each time theJson.Parserhas successfully parsed a single element of aJson.Array. | 
| array-end | The ::array-endsignal is emitted each time theJson.Parserhas successfully parsed an entireJson.Array | 
| array-start | The ::array-startsignal is emitted each time theJson.Parserstarts parsing aJson.Array | 
| error | The ::errorsignal is emitted each time aJson.Parserencounters an error in a JSON stream. | 
| object-end | The ::object-endsignal is emitted each time theJson.Parserhas successfully parsed an entireJson.Object. | 
| object-member | The ::object-membersignal is emitted each time theJson.Parserhas successfully parsed a single member of aJson.Object. | 
| object-start | The ::object-startsignal is emitted each time theJson.Parserstarts parsing aJson.Object. | 
| parse-end | The ::parse-endsignal is emitted when the parser successfully finished parsing a JSON data stream | 
| parse-start | The ::parse-startsignal is emitted when the parser began parsing a JSON data stream. | 
Class Details¶
- 
class Json.Parser(**kwargs)¶
- Bases: - GObject.Object- Abstract: - No - Structure: - Json.ParserClass- JSON data streams parser. The contents of the - Json.Parserstructure are private and should only be accessed via the provided API.- 
classmethod new()¶
- Returns: - the newly created - Json.Parser. Use- GObject.Object.unref() to release all the memory it allocates.- Return type: - Json.Parser- Creates a new - Json.Parserinstance. You can use the- Json.Parserto 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: - a new - Json.Parser- Return type: - Json.Parser- Creates a new - Json.Parserinstance with its- Json.Parser- :immutableproperty set to- Trueto create immutable output trees.- New in version 1.2. 
 - 
get_current_line()¶
- Returns: - the currently parsed line, or 0. - Return type: - int- 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 - Json.Parserwill yield 0.
 - 
get_current_pos()¶
- Returns: - the position in the current line, or 0. - Return type: - int- 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 - Json.Parserwill yield 0.
 - 
get_root()¶
- Returns: - the root - Json.Node. The returned node is owned by the- Json.Parserand should never be modified or freed.- Return type: - Json.Nodeor- None- Retrieves the top level node from the parsed JSON stream. If the parser input was an empty string, or if parsing failed, this will be - None. It will also be- Noneif it has been stolen using- Json.Parser.steal_root().
 - 
has_assignment()¶
- Returns: - Trueif there was an assignment,- Falseotherwise. If variable_name is not- Noneit will be set to the name of the variable used in the assignment. The string is owned by- Json.Parserand should never be modified or freed.- variable_name: - Return location for the variable name, or - None- Return type: - ( - bool, variable_name:- str)- 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. - Json.Parserwill ignore the left hand identifier and parse the right hand value of the assignment.- Json.Parserwill 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. In case of error, error is set accordingly and- Falseis returned- Return type: - Loads a JSON stream from a buffer and parses it. You can call this function multiple times with the same - Json.Parserobject, 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: - GLib.Error- Returns: - Trueif the file was successfully loaded and parsed. In case of error, error is set accordingly and- Falseis returned- Return type: - bool- Loads a JSON stream from the content of filename and parses it. See - Json.Parser.load_from_data().- If the file is large or shared between processes, - Json.Parser.load_from_mapped_file() may be a more efficient way to load it.
 - 
load_from_mapped_file(filename)¶
- Parameters: - filename ( - str) – the path for the file to parse- Raises: - GLib.Error- Returns: - Trueif the file was successfully loaded and parsed. In case of error, error is set accordingly and- Falseis returned- Return type: - bool- Loads a JSON stream from the content of filename and parses it. Unlike - Json.Parser.load_from_file(), filename will be memory mapped as read-only and parsed. filename will be unmapped before this function returns.- If mapping or reading the file fails, a %G_FILE_ERROR will be returned. - New in version 1.6. 
 - 
load_from_stream(stream, cancellable)¶
- Parameters: - stream (Gio.InputStream) – an openGio.InputStream
- cancellable (Gio.CancellableorNone) – aGio.Cancellable, orNone
 - Raises: - Returns: - Trueif the data stream was successfully read and parsed, and- Falseotherwise- Return type: - Loads the contents of an input stream and parses them. - If cancellable is not - None, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error- Gio.IOErrorEnum.CANCELLEDwill be set on the passed error.- New in version 0.12. 
- stream (
 - 
load_from_stream_async(stream, cancellable, callback, *user_data)¶
- Parameters: - stream (Gio.InputStream) – aGio.InputStream
- cancellable (Gio.CancellableorNone) – aGio.Cancellable, orNone
- callback (Gio.AsyncReadyCallbackorNone) – aGio.AsyncReadyCallbackto call when the request is satisfied
- user_data (objectorNone) – the data to pass to callback
 - Asynchronously reads the contents of stream. - For more details, see - 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 - Json.Parser.load_from_stream_finish() to get the result of the operation.- New in version 0.12. 
- stream (
 - 
load_from_stream_finish(result)¶
- Parameters: - result ( - Gio.AsyncResult) – a- Gio.AsyncResult- Raises: - GLib.Error- Returns: - Trueif the content of the stream was successfully retrieves and parsed, and- Falseotherwise. In case of error, the- GLib.Errorwill be filled accordingly.- Return type: - bool- Finishes an asynchronous stream loading started with - Json.Parser.load_from_stream_async().- New in version 0.12. 
 - 
steal_root()¶
- Returns: - the top level - Json.Node- Return type: - Json.Nodeor- None- Steals the top level node from the parsed JSON stream. This will be - Nonein the same situations as- Json.Parser.get_root() returns- None.- New in version 1.4. 
 - 
do_array_element(array, index_) virtual¶
- Parameters: - array (Json.Array) –
- index (int) –
 
- array (
 - 
do_array_end(array) virtual¶
- Parameters: - array ( - Json.Array) –
 - 
do_array_start() virtual¶
 - 
do_error(error) virtual¶
- Parameters: - error ( - GLib.Error) –
 - 
do_object_end(object) virtual¶
- Parameters: - object ( - Json.Object) –
 - 
do_object_member(object, member_name) virtual¶
- Parameters: - object (Json.Object) –
- member_name (str) –
 
- object (
 - 
do_object_start() virtual¶
 - 
do_parse_end() virtual¶
 - 
do_parse_start() virtual¶
 
- 
classmethod 
Signal Details¶
- 
Json.Parser.signals.array_element(parser, array, index_)¶
- Signal Name: - array-element- Flags: - Parameters: - parser (Json.Parser) – The object which received the signal
- array (Json.Array) – aJson.Array
- index (int) – the index of the newly parsed element
 - The - ::array-elementsignal is emitted each time the- Json.Parserhas successfully parsed a single element of a- Json.Array. The array and element index are passed to the signal handlers.
- parser (
- 
Json.Parser.signals.array_end(parser, array)¶
- Signal Name: - array-end- Flags: - Parameters: - parser (Json.Parser) – The object which received the signal
- array (Json.Array) – the parsedJson.Array
 - The - ::array-endsignal is emitted each time the- Json.Parserhas successfully parsed an entire- Json.Array
- parser (
- 
Json.Parser.signals.array_start(parser)¶
- Signal Name: - array-start- Flags: - RUN_LAST- Parameters: - parser ( - Json.Parser) – The object which received the signal- The - ::array-startsignal is emitted each time the- Json.Parserstarts parsing a- Json.Array
- 
Json.Parser.signals.error(parser, error)¶
- Signal Name: - error- Flags: - Parameters: - parser (Json.Parser) – The object which received the signal
- error (objectorNone) – a pointer to theGLib.Error
 - The - ::errorsignal is emitted each time a- Json.Parserencounters an error in a JSON stream.
- parser (
- 
Json.Parser.signals.object_end(parser, object)¶
- Signal Name: - object-end- Flags: - Parameters: - parser (Json.Parser) – The object which received the signal
- object (Json.Object) – the parsedJson.Object
 - The - ::object-endsignal is emitted each time the- Json.Parserhas successfully parsed an entire- Json.Object.
- parser (
- 
Json.Parser.signals.object_member(parser, object, member_name)¶
- Signal Name: - object-member- Flags: - Parameters: - parser (Json.Parser) – The object which received the signal
- object (Json.Object) – aJson.Object
- member_name (str) – the name of the newly parsed member
 - The - ::object-membersignal is emitted each time the- Json.Parserhas successfully parsed a single member of a- Json.Object. The object and member are passed to the signal handlers.
- parser (
- 
Json.Parser.signals.object_start(parser)¶
- Signal Name: - object-start- Flags: - RUN_LAST- Parameters: - parser ( - Json.Parser) – The object which received the signal- The - ::object-startsignal is emitted each time the- Json.Parserstarts parsing a- Json.Object.
- 
Json.Parser.signals.parse_end(parser)¶
- Signal Name: - parse-end- Flags: - RUN_LAST- Parameters: - parser ( - Json.Parser) – The object which received the signal- The - ::parse-endsignal is emitted when the parser successfully finished parsing a JSON data stream
- 
Json.Parser.signals.parse_start(parser)¶
- Signal Name: - parse-start- Flags: - RUN_LAST- Parameters: - parser ( - Json.Parser) – The object which received the signal- The - ::parse-startsignal is emitted when the parser began parsing a JSON data stream.
Property Details¶
- 
Json.Parser.props.immutable¶
- Name: - immutable- Type: - bool- Default Value: - False- Flags: - READABLE,- WRITABLE,- CONSTRUCT_ONLY- Whether the - Json.Nodetree built by the- Json.Parsershould 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.