[json] Update the internal JSON-GLib copy

There have been changes in JSON-GLib upstream to clean up the
data structures, and facilitate introspection.

We still not use the updated JsonParser with the (private) JsonScanner
code, since it's a fork of GLib's GScanner.
This commit is contained in:
Emmanuele Bassi
2009-06-09 12:42:19 +01:00
parent 91856b1bed
commit d743aeaa21
8 changed files with 1459 additions and 133 deletions

View File

@ -2,19 +2,23 @@
*
* This file is part of JSON-GLib
* Copyright (C) 2007 OpenedHand Ltd.
* Copyright (C) 2009 Intel Corp.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Author:
* Emmanuele Bassi <ebassi@openedhand.com>
* Emmanuele Bassi <ebassi@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
@ -23,7 +27,7 @@
#include <glib.h>
#include "json-types.h"
#include "json-types-private.h"
/**
* SECTION:json-node
@ -36,14 +40,62 @@
* When parsing a JSON data stream you extract the root node and walk
* the node tree by retrieving the type of data contained inside the
* node with the %JSON_NODE_TYPE macro. If the node contains a fundamental
* type you can retrieve a copy of the GValue holding it with the
* json_node_get_value() function, and then use the GValue API to extract
* type you can retrieve a copy of the #GValue holding it with the
* json_node_get_value() function, and then use the #GValue API to extract
* the data; if the node contains a complex type you can retrieve the
* #JsonObject or the #JsonArray using json_node_get_object() or
* json_node_get_array() respectively, and then retrieve the nodes
* they contain.
*/
GType
json_node_get_type (void)
{
static GType node_type = 0;
if (G_UNLIKELY (node_type == 0))
node_type = g_boxed_type_register_static (g_intern_static_string ("JsonNode"),
(GBoxedCopyFunc) json_node_copy,
(GBoxedFreeFunc) json_node_free);
return node_type;
}
/**
* json_node_get_value_type:
* @node: a #JsonNode
*
* Returns the #GType of the payload of the node.
*
* Return value: a #GType for the payload.
*
* Since: 0.4
*/
GType
json_node_get_value_type (JsonNode *node)
{
g_return_val_if_fail (node != NULL, G_TYPE_INVALID);
switch (node->type)
{
case JSON_NODE_OBJECT:
return JSON_TYPE_OBJECT;
case JSON_NODE_ARRAY:
return JSON_TYPE_ARRAY;
case JSON_NODE_NULL:
return G_TYPE_INVALID;
case JSON_NODE_VALUE:
return G_VALUE_TYPE (&(node->data.value));
default:
g_assert_not_reached ();
return G_TYPE_INVALID;
}
}
/**
* json_node_new:
* @type: a #JsonNodeType
@ -57,7 +109,8 @@ json_node_new (JsonNodeType type)
{
JsonNode *data;
g_return_val_if_fail (type >= JSON_NODE_OBJECT && type <= JSON_NODE_NULL, NULL);
g_return_val_if_fail (type >= JSON_NODE_OBJECT &&
type <= JSON_NODE_NULL, NULL);
data = g_slice_new0 (JsonNode);
data->type = type;
@ -81,23 +134,32 @@ json_node_copy (JsonNode *node)
g_return_val_if_fail (node != NULL, NULL);
copy = g_slice_new (JsonNode);
*copy = *node;
copy = g_slice_new0 (JsonNode);
copy->type = node->type;
switch (copy->type)
{
case JSON_NODE_OBJECT:
copy->data.object = json_object_ref (node->data.object);
if (node->data.object)
copy->data.object = json_object_ref (node->data.object);
break;
case JSON_NODE_ARRAY:
copy->data.array = json_array_ref (node->data.array);
if (node->data.array)
copy->data.array = json_array_ref (node->data.array);
break;
case JSON_NODE_VALUE:
g_value_init (&(copy->data.value), G_VALUE_TYPE (&(node->data.value)));
g_value_copy (&(node->data.value), &(copy->data.value));
if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
{
g_value_init (&(copy->data.value), G_VALUE_TYPE (&(node->data.value)));
g_value_copy (&(node->data.value), &(copy->data.value));
}
break;
case JSON_NODE_NULL:
break;
default:
g_assert_not_reached ();
}
@ -158,7 +220,7 @@ json_node_take_object (JsonNode *node,
*
* Retrieves the #JsonObject stored inside a #JsonNode
*
* Return value: (transfer none): the #JsonObject
* Return value: the #JsonObject
*/
JsonObject *
json_node_get_object (JsonNode *node)
@ -243,7 +305,7 @@ json_node_take_array (JsonNode *node,
*
* Retrieves the #JsonArray stored inside a #JsonNode
*
* Return value: (transfer none): the #JsonArray
* Return value: the #JsonArray
*/
JsonArray *
json_node_get_array (JsonNode *node)
@ -290,7 +352,7 @@ json_node_get_value (JsonNode *node,
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
if (G_VALUE_TYPE (&(node->data.value)) != 0)
if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
{
g_value_init (value, G_VALUE_TYPE (&(node->data.value)));
g_value_copy (&(node->data.value), value);
@ -391,7 +453,7 @@ json_node_type_name (JsonNode *node)
*
* Retrieves the parent #JsonNode of @node.
*
* Return value: (transfer none): the parent node, or %NULL if @node is the root node
* Return value: the parent node, or %NULL if @node is the root node
*/
JsonNode *
json_node_get_parent (JsonNode *node)
@ -443,7 +505,9 @@ G_CONST_RETURN gchar *
json_node_get_string (JsonNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, NULL);
if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
return NULL;
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
return g_value_get_string (&(node->data.value));
@ -451,14 +515,25 @@ json_node_get_string (JsonNode *node)
return NULL;
}
/**
* json_node_dup_string:
* @node: a #JsonNode of type %JSON_NODE_VALUE
*
* Gets a copy of the string value stored inside a #JsonNode
*
* Return value: a newly allocated string containing a copy of
* the #JsonNode contents
*/
gchar *
json_node_dup_string (JsonNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, NULL);
if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
return NULL;
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
return g_strdup (g_value_get_string (&(node->data.value)));
return g_value_dup_string (&(node->data.value));
return NULL;
}
@ -505,7 +580,9 @@ gint
json_node_get_int (JsonNode *node)
{
g_return_val_if_fail (node != NULL, 0);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0);
if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
return 0;
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT)
return g_value_get_int (&(node->data.value));
@ -555,7 +632,9 @@ gdouble
json_node_get_double (JsonNode *node)
{
g_return_val_if_fail (node != NULL, 0.0);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0.0);
if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
return 0;
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_DOUBLE)
return g_value_get_double (&(node->data.value));
@ -605,10 +684,50 @@ gboolean
json_node_get_boolean (JsonNode *node)
{
g_return_val_if_fail (node != NULL, FALSE);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, FALSE);
if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
return FALSE;
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_BOOLEAN)
return g_value_get_boolean (&(node->data.value));
return FALSE;
}
/**
* json_node_get_node_type:
* @node: a #JsonNode
*
* Retrieves the #JsonNodeType of @node
*
* Return value: the type of the node
*
* Since: 0.8
*/
JsonNodeType
json_node_get_node_type (JsonNode *node)
{
g_return_val_if_fail (node != NULL, JSON_NODE_NULL);
return node->type;
}
/**
* json_node_is_null:
* @node: a #JsonNode
*
* Checks whether @node is a %JSON_NODE_NULL
*
* <note>A null node is not the same as a %NULL #JsonNode</note>
*
* Return value: %TRUE if the node is null
*
* Since: 0.8
*/
gboolean
json_node_is_null (JsonNode *node)
{
g_return_val_if_fail (node != NULL, TRUE);
return node->type == JSON_NODE_NULL;
}