1 /* json-parser.c - JSON streams parser
3 * This file is part of JSON-GLib
5 * Copyright © 2007, 2008, 2009 OpenedHand Ltd
6 * Copyright © 2009, 2010 Intel Corp.
7 * Copyright © 2015 Collabora Ltd.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 * Emmanuele Bassi <ebassi@linux.intel.com>
24 * Philip Withnall <philip.withnall@collabora.co.uk>
29 * @short_description: Parse JSON data streams
31 * #JsonParser provides an object for parsing a JSON data stream, either
32 * inside a file or inside a static buffer.
39 #include <glib/gi18n-lib.h>
41 #include "json-types-private.h"
43 #include "json-debug.h"
44 #include "json-parser.h"
45 #include "json-scanner.h"
47 struct _JsonParserPrivate
50 JsonNode *current_node;
54 JsonParserError error_code;
60 guint has_assignment : 1;
61 guint is_filename : 1;
62 guint is_immutable : 1;
65 static const gchar symbol_names[] =
76 { 0, JSON_TOKEN_TRUE },
77 { 5, JSON_TOKEN_FALSE },
78 { 11, JSON_TOKEN_NULL },
79 { 16, JSON_TOKEN_VAR }
82 static const guint n_symbols = G_N_ELEMENTS (symbols);
99 static guint parser_signals[LAST_SIGNAL] = { 0, };
107 static GParamSpec *parser_props[PROP_LAST] = { NULL, };
109 G_DEFINE_QUARK (json-parser-error-quark, json_parser_error)
111 G_DEFINE_TYPE_WITH_PRIVATE (JsonParser, json_parser, G_TYPE_OBJECT)
113 static guint json_parse_array (JsonParser *parser,
114 JsonScanner *scanner,
116 static guint json_parse_object (JsonParser *parser,
117 JsonScanner *scanner,
121 json_parser_clear (JsonParser *parser)
123 JsonParserPrivate *priv = parser->priv;
125 g_free (priv->variable_name);
126 priv->variable_name = NULL;
128 if (priv->last_error)
130 g_error_free (priv->last_error);
131 priv->last_error = NULL;
136 json_node_unref (priv->root);
142 json_parser_dispose (GObject *gobject)
144 json_parser_clear (JSON_PARSER (gobject));
146 G_OBJECT_CLASS (json_parser_parent_class)->dispose (gobject);
150 json_parser_finalize (GObject *gobject)
152 JsonParserPrivate *priv = JSON_PARSER (gobject)->priv;
154 g_free (priv->variable_name);
155 g_free (priv->filename);
157 G_OBJECT_CLASS (json_parser_parent_class)->finalize (gobject);
161 json_parser_set_property (GObject *gobject,
166 JsonParserPrivate *priv = JSON_PARSER (gobject)->priv;
171 /* Construct-only. */
172 priv->is_immutable = g_value_get_boolean (value);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
181 json_parser_get_property (GObject *gobject,
186 JsonParserPrivate *priv = JSON_PARSER (gobject)->priv;
191 g_value_set_boolean (value, priv->is_immutable);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
200 json_parser_class_init (JsonParserClass *klass)
202 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
204 gobject_class->set_property = json_parser_set_property;
205 gobject_class->get_property = json_parser_get_property;
206 gobject_class->dispose = json_parser_dispose;
207 gobject_class->finalize = json_parser_finalize;
210 * JsonParser:immutable:
212 * Whether the #JsonNode tree built by the #JsonParser should be immutable
213 * when created. Making the output immutable on creation avoids the expense
214 * of traversing it to make it immutable later.
218 parser_props[PROP_IMMUTABLE] =
219 g_param_spec_boolean ("immutable",
221 "Whether the parser output is immutable.",
223 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
225 g_object_class_install_properties (gobject_class, PROP_LAST, parser_props);
228 * JsonParser::parse-start:
229 * @parser: the #JsonParser that received the signal
231 * The ::parse-start signal is emitted when the parser began parsing
232 * a JSON data stream.
234 parser_signals[PARSE_START] =
235 g_signal_new ("parse-start",
236 G_OBJECT_CLASS_TYPE (gobject_class),
238 G_STRUCT_OFFSET (JsonParserClass, parse_start),
243 * JsonParser::parse-end:
244 * @parser: the #JsonParser that received the signal
246 * The ::parse-end signal is emitted when the parser successfully
247 * finished parsing a JSON data stream
249 parser_signals[PARSE_END] =
250 g_signal_new ("parse-end",
251 G_OBJECT_CLASS_TYPE (gobject_class),
253 G_STRUCT_OFFSET (JsonParserClass, parse_end),
257 * JsonParser::object-start:
258 * @parser: the #JsonParser that received the signal
260 * The ::object-start signal is emitted each time the #JsonParser
261 * starts parsing a #JsonObject.
263 parser_signals[OBJECT_START] =
264 g_signal_new ("object-start",
265 G_OBJECT_CLASS_TYPE (gobject_class),
267 G_STRUCT_OFFSET (JsonParserClass, object_start),
271 * JsonParser::object-member:
272 * @parser: the #JsonParser that received the signal
273 * @object: a #JsonObject
274 * @member_name: the name of the newly parsed member
276 * The ::object-member signal is emitted each time the #JsonParser
277 * has successfully parsed a single member of a #JsonObject. The
278 * object and member are passed to the signal handlers.
280 parser_signals[OBJECT_MEMBER] =
281 g_signal_new ("object-member",
282 G_OBJECT_CLASS_TYPE (gobject_class),
284 G_STRUCT_OFFSET (JsonParserClass, object_member),
290 * JsonParser::object-end:
291 * @parser: the #JsonParser that received the signal
292 * @object: the parsed #JsonObject
294 * The ::object-end signal is emitted each time the #JsonParser
295 * has successfully parsed an entire #JsonObject.
297 parser_signals[OBJECT_END] =
298 g_signal_new ("object-end",
299 G_OBJECT_CLASS_TYPE (gobject_class),
301 G_STRUCT_OFFSET (JsonParserClass, object_end),
306 * JsonParser::array-start:
307 * @parser: the #JsonParser that received the signal
309 * The ::array-start signal is emitted each time the #JsonParser
310 * starts parsing a #JsonArray
312 parser_signals[ARRAY_START] =
313 g_signal_new ("array-start",
314 G_OBJECT_CLASS_TYPE (gobject_class),
316 G_STRUCT_OFFSET (JsonParserClass, array_start),
320 * JsonParser::array-element:
321 * @parser: the #JsonParser that received the signal
322 * @array: a #JsonArray
323 * @index_: the index of the newly parsed element
325 * The ::array-element signal is emitted each time the #JsonParser
326 * has successfully parsed a single element of a #JsonArray. The
327 * array and element index are passed to the signal handlers.
329 parser_signals[ARRAY_ELEMENT] =
330 g_signal_new ("array-element",
331 G_OBJECT_CLASS_TYPE (gobject_class),
333 G_STRUCT_OFFSET (JsonParserClass, array_element),
339 * JsonParser::array-end:
340 * @parser: the #JsonParser that received the signal
341 * @array: the parsed #JsonArray
343 * The ::array-end signal is emitted each time the #JsonParser
344 * has successfully parsed an entire #JsonArray
346 parser_signals[ARRAY_END] =
347 g_signal_new ("array-end",
348 G_OBJECT_CLASS_TYPE (gobject_class),
350 G_STRUCT_OFFSET (JsonParserClass, array_end),
356 * @parser: the parser instance that received the signal
357 * @error: a pointer to the #GError
359 * The ::error signal is emitted each time a #JsonParser encounters
360 * an error in a JSON stream.
362 parser_signals[ERROR] =
363 g_signal_new ("error",
364 G_OBJECT_CLASS_TYPE (gobject_class),
366 G_STRUCT_OFFSET (JsonParserClass, error),
373 json_parser_init (JsonParser *parser)
375 JsonParserPrivate *priv = json_parser_get_instance_private (parser);
380 priv->current_node = NULL;
382 priv->error_code = JSON_PARSER_ERROR_PARSE;
383 priv->last_error = NULL;
385 priv->has_assignment = FALSE;
386 priv->variable_name = NULL;
388 priv->is_filename = FALSE;
389 priv->filename = FALSE;
393 json_parse_value (JsonParser *parser,
394 JsonScanner *scanner,
398 JsonParserPrivate *priv = parser->priv;
399 JsonNode *current_node = priv->current_node;
400 gboolean is_negative = FALSE;
404 guint next_token = json_scanner_peek_next_token (scanner);
406 if (next_token == G_TOKEN_INT ||
407 next_token == G_TOKEN_FLOAT)
410 token = json_scanner_get_next_token (scanner);
419 JSON_NOTE (PARSER, "abs(node): %" G_GINT64_FORMAT " (sign: %s)",
420 scanner->value.v_int64,
421 is_negative ? "negative" : "positive");
422 *node = json_node_init_int (json_node_alloc (),
423 is_negative ? scanner->value.v_int64 * -1
424 : scanner->value.v_int64);
428 JSON_NOTE (PARSER, "abs(node): %.6f (sign: %s)",
429 scanner->value.v_float,
430 is_negative ? "negative" : "positive");
431 *node = json_node_init_double (json_node_alloc (),
432 is_negative ? scanner->value.v_float * -1.0
433 : scanner->value.v_float);
437 JSON_NOTE (PARSER, "node: '%s'",
438 scanner->value.v_string);
439 *node = json_node_init_string (json_node_alloc (), scanner->value.v_string);
442 case JSON_TOKEN_TRUE:
443 case JSON_TOKEN_FALSE:
444 JSON_NOTE (PARSER, "node: '%s'",
445 JSON_TOKEN_TRUE ? "<true>" : "<false>");
446 *node = json_node_init_boolean (json_node_alloc (), token == JSON_TOKEN_TRUE ? TRUE : FALSE);
449 case JSON_TOKEN_NULL:
450 JSON_NOTE (PARSER, "node: <null>");
451 *node = json_node_init_null (json_node_alloc ());
454 case G_TOKEN_IDENTIFIER:
455 JSON_NOTE (PARSER, "node: identifier '%s'", scanner->value.v_identifier);
456 priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
458 return G_TOKEN_SYMBOL;
462 JsonNodeType cur_type;
466 JSON_NOTE (PARSER, "node: invalid token");
468 cur_type = json_node_get_node_type (current_node);
469 if (cur_type == JSON_NODE_ARRAY)
471 priv->error_code = JSON_PARSER_ERROR_PARSE;
472 return G_TOKEN_RIGHT_BRACE;
474 else if (cur_type == JSON_NODE_OBJECT)
476 priv->error_code = JSON_PARSER_ERROR_PARSE;
477 return G_TOKEN_RIGHT_CURLY;
481 priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
482 return G_TOKEN_SYMBOL;
488 if (priv->is_immutable && *node != NULL)
489 json_node_seal (*node);
495 json_parse_array (JsonParser *parser,
496 JsonScanner *scanner,
499 JsonParserPrivate *priv = parser->priv;
500 JsonNode *old_current;
505 old_current = priv->current_node;
506 priv->current_node = json_node_init_array (json_node_alloc (), NULL);
508 array = json_array_new ();
510 token = json_scanner_get_next_token (scanner);
511 g_assert (token == G_TOKEN_LEFT_BRACE);
513 g_signal_emit (parser, parser_signals[ARRAY_START], 0);
516 while (token != G_TOKEN_RIGHT_BRACE)
518 guint next_token = json_scanner_peek_next_token (scanner);
519 JsonNode *element = NULL;
521 /* parse the element */
524 case G_TOKEN_LEFT_BRACE:
525 JSON_NOTE (PARSER, "Nested array at index %d", idx);
526 token = json_parse_array (parser, scanner, &element);
529 case G_TOKEN_LEFT_CURLY:
530 JSON_NOTE (PARSER, "Nested object at index %d", idx);
531 token = json_parse_object (parser, scanner, &element);
534 case G_TOKEN_RIGHT_BRACE:
538 token = json_scanner_get_next_token (scanner);
539 token = json_parse_value (parser, scanner, token, &element);
543 if (token != G_TOKEN_NONE || element == NULL)
545 /* the json_parse_* functions will have set the error code */
546 json_array_unref (array);
547 json_node_unref (priv->current_node);
548 priv->current_node = old_current;
553 next_token = json_scanner_peek_next_token (scanner);
555 /* look for missing commas */
556 if (next_token != G_TOKEN_COMMA && next_token != G_TOKEN_RIGHT_BRACE)
558 priv->error_code = JSON_PARSER_ERROR_MISSING_COMMA;
560 json_array_unref (array);
561 json_node_free (priv->current_node);
562 json_node_free (element);
563 priv->current_node = old_current;
565 return G_TOKEN_COMMA;
568 /* look for trailing commas */
569 if (next_token == G_TOKEN_COMMA)
571 token = json_scanner_get_next_token (scanner);
572 next_token = json_scanner_peek_next_token (scanner);
574 if (next_token == G_TOKEN_RIGHT_BRACE)
576 priv->error_code = JSON_PARSER_ERROR_TRAILING_COMMA;
578 json_array_unref (array);
579 json_node_unref (priv->current_node);
580 json_node_unref (element);
581 priv->current_node = old_current;
583 return G_TOKEN_RIGHT_BRACE;
587 JSON_NOTE (PARSER, "Array element %d completed", idx);
588 json_node_set_parent (element, priv->current_node);
589 if (priv->is_immutable)
590 json_node_seal (element);
591 json_array_add_element (array, element);
593 g_signal_emit (parser, parser_signals[ARRAY_ELEMENT], 0,
602 json_scanner_get_next_token (scanner);
604 if (priv->is_immutable)
605 json_array_seal (array);
607 json_node_take_array (priv->current_node, array);
608 if (priv->is_immutable)
609 json_node_seal (priv->current_node);
610 json_node_set_parent (priv->current_node, old_current);
612 g_signal_emit (parser, parser_signals[ARRAY_END], 0, array);
614 if (node != NULL && *node == NULL)
615 *node = priv->current_node;
617 priv->current_node = old_current;
623 json_parse_object (JsonParser *parser,
624 JsonScanner *scanner,
627 JsonParserPrivate *priv = parser->priv;
629 JsonNode *old_current;
632 old_current = priv->current_node;
633 priv->current_node = json_node_init_object (json_node_alloc (), NULL);
635 object = json_object_new ();
637 token = json_scanner_get_next_token (scanner);
638 g_assert (token == G_TOKEN_LEFT_CURLY);
640 g_signal_emit (parser, parser_signals[OBJECT_START], 0);
642 while (token != G_TOKEN_RIGHT_CURLY)
644 guint next_token = json_scanner_peek_next_token (scanner);
645 JsonNode *member = NULL;
648 /* we need to abort here because empty objects do not
651 if (next_token == G_TOKEN_RIGHT_CURLY)
654 /* parse the member's name */
655 if (next_token != G_TOKEN_STRING)
657 JSON_NOTE (PARSER, "Missing object member name");
659 priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
661 json_object_unref (object);
662 json_node_unref (priv->current_node);
663 priv->current_node = old_current;
665 return G_TOKEN_STRING;
669 token = json_scanner_get_next_token (scanner);
670 name = g_strdup (scanner->value.v_string);
673 JSON_NOTE (PARSER, "Empty object member name");
675 priv->error_code = JSON_PARSER_ERROR_EMPTY_MEMBER_NAME;
677 json_object_unref (object);
678 json_node_unref (priv->current_node);
679 priv->current_node = old_current;
681 return G_TOKEN_STRING;
684 JSON_NOTE (PARSER, "Object member '%s'", name);
686 /* a colon separates names from values */
687 next_token = json_scanner_peek_next_token (scanner);
688 if (next_token != ':')
690 JSON_NOTE (PARSER, "Missing object member name separator");
692 priv->error_code = JSON_PARSER_ERROR_MISSING_COLON;
695 json_object_unref (object);
696 json_node_unref (priv->current_node);
697 priv->current_node = old_current;
702 /* we swallow the ':' */
703 token = json_scanner_get_next_token (scanner);
704 g_assert (token == ':');
705 next_token = json_scanner_peek_next_token (scanner);
707 /* parse the member's value */
710 case G_TOKEN_LEFT_BRACE:
711 JSON_NOTE (PARSER, "Nested array at member %s", name);
712 token = json_parse_array (parser, scanner, &member);
715 case G_TOKEN_LEFT_CURLY:
716 JSON_NOTE (PARSER, "Nested object at member %s", name);
717 token = json_parse_object (parser, scanner, &member);
721 /* once a member name is defined we need a value */
722 token = json_scanner_get_next_token (scanner);
723 token = json_parse_value (parser, scanner, token, &member);
727 if (token != G_TOKEN_NONE || member == NULL)
729 /* the json_parse_* functions will have set the error code */
731 json_object_unref (object);
732 json_node_unref (priv->current_node);
733 priv->current_node = old_current;
738 next_token = json_scanner_peek_next_token (scanner);
739 if (next_token == G_TOKEN_COMMA)
741 token = json_scanner_get_next_token (scanner);
742 next_token = json_scanner_peek_next_token (scanner);
744 /* look for trailing commas */
745 if (next_token == G_TOKEN_RIGHT_CURLY)
747 priv->error_code = JSON_PARSER_ERROR_TRAILING_COMMA;
749 json_object_unref (object);
750 json_node_unref (member);
751 json_node_unref (priv->current_node);
752 priv->current_node = old_current;
754 return G_TOKEN_RIGHT_BRACE;
757 else if (next_token == G_TOKEN_STRING)
759 priv->error_code = JSON_PARSER_ERROR_MISSING_COMMA;
761 json_object_unref (object);
762 json_node_unref (member);
763 json_node_unref (priv->current_node);
764 priv->current_node = old_current;
766 return G_TOKEN_COMMA;
769 JSON_NOTE (PARSER, "Object member '%s' completed", name);
770 json_node_set_parent (member, priv->current_node);
771 if (priv->is_immutable)
772 json_node_seal (member);
773 json_object_set_member (object, name, member);
775 g_signal_emit (parser, parser_signals[OBJECT_MEMBER], 0,
784 json_scanner_get_next_token (scanner);
786 if (priv->is_immutable)
787 json_object_seal (object);
789 json_node_take_object (priv->current_node, object);
790 if (priv->is_immutable)
791 json_node_seal (priv->current_node);
792 json_node_set_parent (priv->current_node, old_current);
794 g_signal_emit (parser, parser_signals[OBJECT_END], 0, object);
796 if (node != NULL && *node == NULL)
797 *node = priv->current_node;
799 priv->current_node = old_current;
805 json_parse_statement (JsonParser *parser,
806 JsonScanner *scanner)
808 JsonParserPrivate *priv = parser->priv;
811 token = json_scanner_peek_next_token (scanner);
814 case G_TOKEN_LEFT_CURLY:
815 JSON_NOTE (PARSER, "Statement is object declaration");
816 return json_parse_object (parser, scanner, &priv->root);
818 case G_TOKEN_LEFT_BRACE:
819 JSON_NOTE (PARSER, "Statement is array declaration");
820 return json_parse_array (parser, scanner, &priv->root);
822 /* some web APIs are not only passing the data structures: they are
823 * also passing an assigment, which makes parsing horribly complicated
824 * only because web developers are lazy, and writing "var foo = " is
825 * evidently too much to request from them.
832 JSON_NOTE (PARSER, "Statement is an assignment");
834 /* swallow the 'var' token... */
835 token = json_scanner_get_next_token (scanner);
837 /* ... swallow the variable name... */
838 next_token = json_scanner_get_next_token (scanner);
839 if (next_token != G_TOKEN_IDENTIFIER)
841 priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
842 return G_TOKEN_IDENTIFIER;
845 name = g_strdup (scanner->value.v_identifier);
847 /* ... and finally swallow the '=' */
848 next_token = json_scanner_get_next_token (scanner);
849 if (next_token != '=')
851 priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
856 priv->has_assignment = TRUE;
857 priv->variable_name = name;
859 token = json_parse_statement (parser, scanner);
861 /* remove the trailing semi-colon */
862 next_token = json_scanner_peek_next_token (scanner);
863 if (next_token == ';')
865 token = json_scanner_get_next_token (scanner);
873 case JSON_TOKEN_NULL:
874 case JSON_TOKEN_TRUE:
875 case JSON_TOKEN_FALSE:
880 case G_TOKEN_IDENTIFIER:
881 JSON_NOTE (PARSER, "Statement is a value");
882 token = json_scanner_get_next_token (scanner);
883 return json_parse_value (parser, scanner, token, &priv->root);
886 JSON_NOTE (PARSER, "Unknown statement");
887 json_scanner_get_next_token (scanner);
888 priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
889 return G_TOKEN_SYMBOL;
894 json_scanner_msg_handler (JsonScanner *scanner,
897 JsonParser *parser = scanner->user_data;
898 JsonParserPrivate *priv = parser->priv;
899 GError *error = NULL;
901 g_set_error (&error, JSON_PARSER_ERROR,
903 /* translators: %s: is the file name, the first %d is the line
904 * number, the second %d is the position on the line, and %s is
907 _("%s:%d:%d: Parse error: %s"),
908 priv->is_filename ? priv->filename : "<data>",
913 parser->priv->last_error = error;
914 g_signal_emit (parser, parser_signals[ERROR], 0, error);
918 json_scanner_create (JsonParser *parser)
920 JsonScanner *scanner;
923 scanner = json_scanner_new ();
924 scanner->msg_handler = json_scanner_msg_handler;
925 scanner->user_data = parser;
927 /* XXX: this is eminently stupid, but we use the symbols later on, so
928 * we cannot move them into JsonScanner without moving a bunch of code
931 for (i = 0; i < n_symbols; i++)
933 json_scanner_scope_add_symbol (scanner, 0,
934 symbol_names + symbols[i].name_offset,
935 GINT_TO_POINTER (symbols[i].token));
944 * Creates a new #JsonParser instance. You can use the #JsonParser to
945 * load a JSON stream from either a file or a buffer and then walk the
946 * hierarchy using the data types API.
948 * Return value: the newly created #JsonParser. Use g_object_unref()
949 * to release all the memory it allocates.
952 json_parser_new (void)
954 return g_object_new (JSON_TYPE_PARSER, NULL);
958 * json_parser_new_immutable:
960 * Creates a new #JsonParser instance with its #JsonParser:immutable property
961 * set to %TRUE to create immutable output trees.
964 * Returns: (transfer full): a new #JsonParser
967 json_parser_new_immutable (void)
969 return g_object_new (JSON_TYPE_PARSER, "immutable", TRUE, NULL);
973 json_parser_load (JsonParser *parser,
978 JsonParserPrivate *priv = parser->priv;
979 JsonScanner *scanner;
981 gboolean retval = TRUE;
984 json_parser_clear (parser);
986 if (!g_utf8_validate (data, length, NULL))
988 g_set_error_literal (error, JSON_PARSER_ERROR,
989 JSON_PARSER_ERROR_INVALID_DATA,
990 _("JSON data must be UTF-8 encoded"));
991 g_signal_emit (parser, parser_signals[ERROR], 0, *error);
995 scanner = json_scanner_create (parser);
996 json_scanner_input_text (scanner, data, length);
998 priv->scanner = scanner;
1000 g_signal_emit (parser, parser_signals[PARSE_START], 0);
1005 if (json_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1009 guint expected_token;
1012 /* we try to show the expected token, if possible */
1013 expected_token = json_parse_statement (parser, scanner);
1014 if (expected_token != G_TOKEN_NONE)
1016 const gchar *symbol_name;
1019 cur_token = scanner->token;
1023 if (scanner->scope_id == 0)
1025 if (expected_token > JSON_TOKEN_INVALID &&
1026 expected_token < JSON_TOKEN_LAST)
1028 for (i = 0; i < n_symbols; i++)
1029 if (symbols[i].token == expected_token)
1030 symbol_name = symbol_names + symbols[i].name_offset;
1033 msg = g_strconcat ("e.g. '", symbol_name, "'", NULL);
1036 if (cur_token > JSON_TOKEN_INVALID &&
1037 cur_token < JSON_TOKEN_LAST)
1039 symbol_name = "???";
1041 for (i = 0; i < n_symbols; i++)
1042 if (symbols[i].token == cur_token)
1043 symbol_name = symbol_names + symbols[i].name_offset;
1047 /* this will emit the ::error signal via the custom
1048 * message handler we install
1050 json_scanner_unexp_token (scanner, expected_token,
1054 /* and this will propagate the error we create in the
1055 * same message handler
1057 if (priv->last_error)
1059 g_propagate_error (error, priv->last_error);
1060 priv->last_error = NULL;
1071 g_signal_emit (parser, parser_signals[PARSE_END], 0);
1073 /* remove the scanner */
1074 json_scanner_destroy (scanner);
1075 priv->scanner = NULL;
1076 priv->current_node = NULL;
1082 * json_parser_load_from_file:
1083 * @parser: a #JsonParser
1084 * @filename: the path for the file to parse
1085 * @error: return location for a #GError, or %NULL
1087 * Loads a JSON stream from the content of @filename and parses it. See
1088 * json_parser_load_from_data().
1090 * Return value: %TRUE if the file was successfully loaded and parsed.
1091 * In case of error, @error is set accordingly and %FALSE is returned
1094 json_parser_load_from_file (JsonParser *parser,
1095 const gchar *filename,
1098 JsonParserPrivate *priv;
1099 GError *internal_error;
1102 gboolean retval = TRUE;
1104 g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
1105 g_return_val_if_fail (filename != NULL, FALSE);
1107 priv = parser->priv;
1109 internal_error = NULL;
1110 if (!g_file_get_contents (filename, &data, &length, &internal_error))
1112 g_propagate_error (error, internal_error);
1116 g_free (priv->filename);
1118 priv->is_filename = TRUE;
1119 priv->filename = g_strdup (filename);
1121 if (!json_parser_load (parser, data, length, &internal_error))
1123 g_propagate_error (error, internal_error);
1133 * json_parser_load_from_data:
1134 * @parser: a #JsonParser
1135 * @data: the buffer to parse
1136 * @length: the length of the buffer, or -1
1137 * @error: return location for a #GError, or %NULL
1139 * Loads a JSON stream from a buffer and parses it. You can call this function
1140 * multiple times with the same #JsonParser object, but the contents of the
1141 * parser will be destroyed each time.
1143 * Return value: %TRUE if the buffer was succesfully parser. In case
1144 * of error, @error is set accordingly and %FALSE is returned
1147 json_parser_load_from_data (JsonParser *parser,
1152 JsonParserPrivate *priv;
1153 GError *internal_error;
1154 gboolean retval = TRUE;
1156 g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
1157 g_return_val_if_fail (data != NULL, FALSE);
1159 priv = parser->priv;
1162 length = strlen (data);
1164 priv->is_filename = FALSE;
1165 g_free (priv->filename);
1166 priv->filename = NULL;
1168 internal_error = NULL;
1169 if (!json_parser_load (parser, data, length, &internal_error))
1171 g_propagate_error (error, internal_error);
1179 * json_parser_get_root:
1180 * @parser: a #JsonParser
1182 * Retrieves the top level node from the parsed JSON stream.
1184 * Return value: (transfer none): the root #JsonNode . The returned
1185 * node is owned by the #JsonParser and should never be modified
1189 json_parser_get_root (JsonParser *parser)
1191 g_return_val_if_fail (JSON_IS_PARSER (parser), NULL);
1194 g_return_val_if_fail (!parser->priv->is_immutable ||
1195 json_node_is_immutable (parser->priv->root), NULL);
1197 return parser->priv->root;
1201 * json_parser_get_current_line:
1202 * @parser: a #JsonParser
1204 * Retrieves the line currently parsed, starting from 1.
1206 * This function has defined behaviour only while parsing; calling this
1207 * function from outside the signal handlers emitted by #JsonParser will
1210 * Return value: the currently parsed line, or 0.
1213 json_parser_get_current_line (JsonParser *parser)
1215 g_return_val_if_fail (JSON_IS_PARSER (parser), 0);
1217 if (parser->priv->scanner != NULL)
1218 return parser->priv->scanner->line;
1224 * json_parser_get_current_pos:
1225 * @parser: a #JsonParser
1227 * Retrieves the current position inside the current line, starting
1230 * This function has defined behaviour only while parsing; calling this
1231 * function from outside the signal handlers emitted by #JsonParser will
1234 * Return value: the position in the current line, or 0.
1237 json_parser_get_current_pos (JsonParser *parser)
1239 g_return_val_if_fail (JSON_IS_PARSER (parser), 0);
1241 if (parser->priv->scanner != NULL)
1242 return parser->priv->scanner->position;
1248 * json_parser_has_assignment:
1249 * @parser: a #JsonParser
1250 * @variable_name: (out) (allow-none) (transfer none): Return location for the variable
1253 * A JSON data stream might sometimes contain an assignment, like:
1256 * var _json_data = { "member_name" : [ ...
1259 * even though it would technically constitute a violation of the RFC.
1261 * #JsonParser will ignore the left hand identifier and parse the right
1262 * hand value of the assignment. #JsonParser will record, though, the
1263 * existence of the assignment in the data stream and the variable name
1266 * Return value: %TRUE if there was an assignment, %FALSE otherwise. If
1267 * @variable_name is not %NULL it will be set to the name of the variable
1268 * used in the assignment. The string is owned by #JsonParser and should
1269 * never be modified or freed.
1274 json_parser_has_assignment (JsonParser *parser,
1275 gchar **variable_name)
1277 JsonParserPrivate *priv;
1279 g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
1281 priv = parser->priv;
1283 if (priv->has_assignment && variable_name)
1284 *variable_name = priv->variable_name;
1286 return priv->has_assignment;
1289 #define GET_DATA_BLOCK_SIZE 8192
1292 * json_parser_load_from_stream:
1293 * @parser: a #JsonParser
1294 * @stream: an open #GInputStream
1295 * @cancellable: (allow-none): a #GCancellable, or %NULL
1296 * @error: the return location for a #GError, or %NULL
1298 * Loads the contents of an input stream and parses them.
1300 * If @cancellable is not %NULL, then the operation can be cancelled by
1301 * triggering the @cancellable object from another thread. If the
1302 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be set
1303 * on the passed @error.
1305 * Return value: %TRUE if the data stream was successfully read and
1306 * parsed, and %FALSE otherwise
1311 json_parser_load_from_stream (JsonParser *parser,
1312 GInputStream *stream,
1313 GCancellable *cancellable,
1316 GByteArray *content;
1319 gboolean retval = FALSE;
1320 GError *internal_error;
1322 g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
1323 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
1324 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
1326 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1329 content = g_byte_array_new ();
1332 g_byte_array_set_size (content, pos + GET_DATA_BLOCK_SIZE + 1);
1333 while ((res = g_input_stream_read (stream, content->data + pos,
1334 GET_DATA_BLOCK_SIZE,
1335 cancellable, error)) > 0)
1338 g_byte_array_set_size (content, pos + GET_DATA_BLOCK_SIZE + 1);
1343 /* error has already been set */
1348 /* zero-terminate the content; we allocated an extra byte for this */
1349 content->data[pos] = 0;
1351 internal_error = NULL;
1352 retval = json_parser_load (parser, (const gchar *) content->data, pos, &internal_error);
1354 if (internal_error != NULL)
1355 g_propagate_error (error, internal_error);
1358 g_byte_array_free (content, TRUE);
1364 GInputStream *stream;
1365 GByteArray *content;
1370 load_data_free (gpointer data_)
1374 LoadData *data = data_;
1376 g_object_unref (data->stream);
1377 g_byte_array_unref (data->content);
1383 * json_parser_load_from_stream_finish:
1384 * @parser: a #JsonParser
1385 * @result: a #GAsyncResult
1386 * @error: the return location for a #GError or %NULL
1388 * Finishes an asynchronous stream loading started with
1389 * json_parser_load_from_stream_async().
1391 * Return value: %TRUE if the content of the stream was successfully retrieves
1392 * and parsed, and %FALSE otherwise. In case of error, the #GError will be
1393 * filled accordingly.
1398 json_parser_load_from_stream_finish (JsonParser *parser,
1399 GAsyncResult *result,
1404 g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE);
1405 g_return_val_if_fail (g_task_is_valid (result, parser), FALSE);
1407 res = g_task_propagate_boolean (G_TASK (result), error);
1410 LoadData *data = g_task_get_task_data (G_TASK (result));
1411 GError *internal_error = NULL;
1413 /* We need to do this inside the finis() function because JsonParser will emit
1414 * signals, and we need to ensure that the signals are emitted in the right
1415 * context; it's easier to do that if we just rely on the async callback being
1416 * called in the right context, even if it means making the finish() function
1417 * necessary to complete the async operation.
1419 res = json_parser_load (parser, (const gchar *) data->content->data, data->pos, &internal_error);
1420 if (internal_error != NULL)
1421 g_propagate_error (error, internal_error);
1428 read_from_stream (GTask *task,
1429 gpointer source_obj,
1431 GCancellable *cancellable)
1433 LoadData *data = task_data;
1434 GError *error = NULL;
1438 g_byte_array_set_size (data->content, data->pos + GET_DATA_BLOCK_SIZE + 1);
1439 while ((res = g_input_stream_read (data->stream,
1440 data->content->data + data->pos,
1441 GET_DATA_BLOCK_SIZE,
1442 cancellable, &error)) > 0)
1445 g_byte_array_set_size (data->content, data->pos + GET_DATA_BLOCK_SIZE + 1);
1450 g_task_return_error (task, error);
1454 /* zero-terminate the content; we allocated an extra byte for this */
1455 data->content->data[data->pos] = 0;
1456 g_task_return_boolean (task, TRUE);
1460 * json_parser_load_from_stream_async:
1461 * @parser: a #JsonParser
1462 * @stream: a #GInputStream
1463 * @cancellable: (allow-none): a #GCancellable, or %NULL
1464 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1465 * @user_data: the data to pass to @callback
1467 * Asynchronously reads the contents of @stream.
1469 * For more details, see json_parser_load_from_stream() which is the
1470 * synchronous version of this call.
1472 * When the operation is finished, @callback will be called. You should
1473 * then call json_parser_load_from_stream_finish() to get the result
1479 json_parser_load_from_stream_async (JsonParser *parser,
1480 GInputStream *stream,
1481 GCancellable *cancellable,
1482 GAsyncReadyCallback callback,
1488 g_return_if_fail (JSON_IS_PARSER (parser));
1489 g_return_if_fail (G_IS_INPUT_STREAM (stream));
1490 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1492 data = g_new (LoadData, 1);
1493 data->stream = g_object_ref (stream);
1494 data->content = g_byte_array_new ();
1497 task = g_task_new (parser, cancellable, callback, user_data);
1498 g_task_set_task_data (task, data, load_data_free);
1500 g_task_run_in_thread (task, read_from_stream);
1501 g_object_unref (task);