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 [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 GTask and the synchronous JsonParser API inside the task itself.

Ancestors

  • GObject

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_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

json_parser_steal_root

Steals the top level node from the parsed JSON stream.

Available since: 1.4

Properties

Json.Parser:immutable

Whether the tree built by the parser should be immutable when created.

Available since: 1.2

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.

Json.Parser::array-end

The ::array-end signal is emitted each time a parser has successfully parsed an entire JSON array.

Json.Parser::array-start

The ::array-start signal is emitted each time a parser starts parsing a JSON array.

Json.Parser::error

The ::error signal is emitted each time a parser encounters an error in a JSON stream.

Json.Parser::object-end

The ::object-end signal is emitted each time a parser has successfully parsed an entire JSON object.

Json.Parser::object-member

The ::object-member signal is emitted each time a parser has successfully parsed a single member of a JSON object.

Json.Parser::object-start

This signal is emitted each time a parser starts parsing a JSON object.

Json.Parser::parse-end

This signal is emitted when a parser successfully finished parsing a JSON data stream.

Json.Parser::parse-start

This signal is emitted when a parser starts parsing a JSON data stream.

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
void (* parse_start) (
    JsonParser* parser
  )
  No description available.
object_start
void (* object_start) (
    JsonParser* parser
  )
  No description available.
object_member
void (* object_member) (
    JsonParser* parser,
    JsonObject* object,
    const gchar* member_name
  )
  No description available.
object_end
void (* object_end) (
    JsonParser* parser,
    JsonObject* object
  )
  No description available.
array_start
void (* array_start) (
    JsonParser* parser
  )
  No description available.
array_element
void (* array_element) (
    JsonParser* parser,
    JsonArray* array,
    gint index_
  )
  No description available.
array_end
void (* array_end) (
    JsonParser* parser,
    JsonArray* array
  )
  No description available.
parse_end
void (* parse_end) (
    JsonParser* parser
  )
  No description available.
error
void (* error) (
    JsonParser* parser,
    const GError* error
  )
  No description available.
_json_reserved1
void (* _json_reserved1) (
void
  )
  No description available.
_json_reserved2
void (* _json_reserved2) (
void
  )
  No description available.
_json_reserved3
void (* _json_reserved3) (
void
  )
  No description available.
_json_reserved4
void (* _json_reserved4) (
void
  )
  No description available.
_json_reserved5
void (* _json_reserved5) (
void
  )
  No description available.
_json_reserved6
void (* _json_reserved6) (
void
  )
  No description available.
_json_reserved7
void (* _json_reserved7) (
void
  )
  No description available.
_json_reserved8
void (* _json_reserved8) (
void
  )
  No description available.

Virtual methods

Json.ParserClass.array_element
No description available.
Json.ParserClass.array_end
No description available.
Json.ParserClass.array_start
No description available.
Json.ParserClass.error
No description available.
Json.ParserClass.object_end
No description available.
Json.ParserClass.object_member
No description available.
Json.ParserClass.object_start
No description available.
Json.ParserClass.parse_end
No description available.
Json.ParserClass.parse_start
No description available.