1
0
Fork 0

Remove the internal copy of JSON-GLib

The internal copy of JSON-GLib was meant to go away right after the 1.0
release, given that JSON-GLib was still young and relatively unknown.

Nowadays, many projects started depending on this little library, and
distributions ship it and keep it up to date.

Keeping a copy of JSON-GLib means keeping it up to date; unfortunately,
this would also imply updating the code not just for the API but for the
internal implementations.

Starting with the 1.2 release, Clutter preferably dependend on the
system copy; with the 1.4 release we stopped falling back automatically.
The 1.6 cycle finally removes the internal copy and requires a copy of
JSON-GLib installed on the target system in order to compile Clutter.
This commit is contained in:
Emmanuele Bassi 2010-09-22 12:56:33 +01:00
parent e36cc40a49
commit 66b0c1969c
20 changed files with 13 additions and 5089 deletions

15
README
View file

@ -10,6 +10,7 @@ REQUIREMENTS
Clutter currently requires:
• GLib >= 2.18.0
• JSON-GLib >= 0.10
• Atk >= 1.17
• Cairo >= 1.6
• PangoCairo >= 1.20
@ -18,7 +19,6 @@ Clutter currently requires:
Clutter also has optional dependencies:
• JSON-GLib >= 0.10 (optional, see --with-json below)
• GDK-Pixbuf >= 2.0 (optional, see --with-imagebackend below)
On X11, Clutter depends on the following extensions
@ -197,19 +197,6 @@ Clutter has additional command line options for the configure script:
--with-gles=[1.1/2.0]
Select the GLES version (for EGL backends) (default=1.1)
--with-json=[internal/check/system]
Select the JSON-GLib copy to use (default=check)
internal:
Use the internal copy of JSON-GLib for ClutterScript
check:
Check for the existence of a system copy of JSON-GLib
and if it is available, make Clutter depend on it
system:
Only use the system copy of JSON-GLib and warn if not found
VERSIONING
-------------------------------------------------------------------------------

View file

@ -474,36 +474,6 @@ cally_include_HEADERS = $(cally_sources_h)
pc_files += cally/cally-$(CLUTTER_API_VERSION).pc
EXTRA_DIST += cally/cally.pc.in
# json
if LOCAL_JSON_GLIB
json_gir = ClutterJson-@CLUTTER_API_VERSION@.gir
json_gir_include = --include-uninstalled=$(top_builddir)/clutter/ClutterJson-@CLUTTER_API_VERSION@.gir
json_sources_h = \
$(srcdir)/json/json-generator.h \
$(srcdir)/json/json-glib.h \
$(srcdir)/json/json-parser.h \
$(srcdir)/json/json-types.h \
$(NULL)
json_sources_c = \
$(srcdir)/json/json-array.c \
$(srcdir)/json/json-node.c \
$(srcdir)/json/json-object.c \
$(srcdir)/json/json-parser.c \
$(NULL)
json_sources_priv = \
$(srcdir)/json/json-marshal.c \
$(srcdir)/json/json-marshal.h \
$(srcdir)/json/json-types-private.h \
$(NULL)
INCLUDES += $(top_srcdir)/clutter/json
else
json_gir_include=--include=Json-1.0
endif # LOCAL_JSON_GLIB
# general build rules:
# you should not need to modify anything below this point
@ -530,7 +500,6 @@ DISTCLEANFILES += $(pc_files)
clutter_include_HEADERS = $(source_h) $(top_srcdir)/clutter/clutter.h
nodist_clutter_include_HEADERS = \
$(top_builddir)/clutter/clutter-json.h \
$(top_builddir)/clutter/clutter-version.h \
$(built_source_h)
@ -558,9 +527,6 @@ libclutter_@CLUTTER_SONAME_INFIX@_@CLUTTER_API_VERSION@_la_SOURCES = \
$(cally_sources_c) \
$(cally_sources_h) \
$(cally_sources_private) \
$(json_sources_c) \
$(json_sources_h) \
$(json_sources_priv) \
$(NULL)
nodist_libclutter_@CLUTTER_SONAME_INFIX@_@CLUTTER_API_VERSION@_la_SOURCES = \
@ -573,7 +539,7 @@ libclutter_@CLUTTER_SONAME_INFIX@_@CLUTTER_API_VERSION@_la_LDFLAGS = \
$(CLUTTER_LT_LDFLAGS) \
$(GCOV_LDFLAGS) \
-export-dynamic \
-export-symbols-regex "^(clutter|cogl|cally|json).*" \
-export-symbols-regex "^(clutter|cogl|cally).*" \
-rpath $(libdir) \
$(win32_resources_ldflag) \
$(NULL)
@ -584,24 +550,23 @@ libclutter_@CLUTTER_SONAME_INFIX@_@CLUTTER_API_VERSION@_la_LDFLAGS = \
if HAVE_INTROSPECTION
INTROSPECTION_GIRS += Clutter-@CLUTTER_API_VERSION@.gir
Clutter-@CLUTTER_API_VERSION@.gir: libclutter-@CLUTTER_SONAME_INFIX@-@CLUTTER_API_VERSION@.la $(clutter_json_gir) Makefile
Clutter-@CLUTTER_API_VERSION@.gir: libclutter-@CLUTTER_SONAME_INFIX@-@CLUTTER_API_VERSION@.la Makefile
Clutter_@CLUTTER_API_VERSION_AM@_gir_NAMESPACE = Clutter
Clutter_@CLUTTER_API_VERSION_AM@_gir_VERSION = @CLUTTER_API_VERSION@
Clutter_@CLUTTER_API_VERSION_AM@_gir_LIBS = libclutter-@CLUTTER_SONAME_INFIX@-@CLUTTER_API_VERSION@.la
Clutter_@CLUTTER_API_VERSION_AM@_gir_FILES = $(clutter_HEADERS) $(nodist_clutter_HEADERS) $(source_c) $(built_source_c)
Clutter_@CLUTTER_API_VERSION_AM@_gir_CFLAGS = $(INCLUDES) $(CLUTTER_CFLAGS) $(AM_CPPFLAGS) -UCLUTTER_DISABLE_DEPRECATED
Clutter_@CLUTTER_API_VERSION_AM@_gir_INCLUDES = GL-1.0 GObject-2.0 Atk-1.0 Pango-1.0 PangoCairo-1.0
Clutter_@CLUTTER_API_VERSION_AM@_gir_INCLUDES = GL-1.0 GObject-2.0 Atk-1.0 Pango-1.0 PangoCairo-1.0 Json-1.0
Clutter_@CLUTTER_API_VERSION_AM@_gir_SCANNERFLAGS = \
--warn-all \
--c-include='clutter/clutter.h' \
--pkg-export clutter-@CLUTTER_API_VERSION@ \
$(json_gir_include) \
--include-uninstalled=$(top_builddir)/clutter/cogl/cogl/Cogl-@CLUTTER_API_VERSION@.gir
# ClutterJson.gir and Cogl.gir are used included into Clutter.gir, so they
# need to be built before the typelib is generated
Clutter-@CLUTTER_API_VERSION@.typelib: $(json_gir) Cogl-@CLUTTER_API_VERSION@.gir
# Cogl.gir is used included into Clutter.gir, so it needs to be built
# before the typelib is generated
Clutter-@CLUTTER_API_VERSION@.typelib: Cogl-@CLUTTER_API_VERSION@.gir
# We build Cogl.gir in the cogl/ subdir, but it needs to reference the shared
# library that it's built into, so we delay compiling the gir into typelib
@ -626,29 +591,11 @@ Cally_@CLUTTER_API_VERSION_AM@_gir_SCANNERFLAGS = \
--warn-all \
--c-include='cally/cally.h' \
--pkg-export=cally-@CLUTTER_API_VERSION@ \
$(json_gir_include) \
--include-uninstalled $(top_builddir)/clutter/Cogl-@CLUTTER_API_VERSION@.gir \
--include-uninstalled $(top_builddir)/clutter/Clutter-@CLUTTER_API_VERSION@.gir
INTROSPECTION_GIRS += Cally-@CLUTTER_API_VERSION@.gir
if LOCAL_JSON_GLIB
ClutterJson-@CLUTTER_API_VERSION@.gir: libclutter-@CLUTTER_SONAME_INFIX@-@CLUTTER_API_VERSION@.la
ClutterJson_@CLUTTER_API_VERSION_AM@_gir_NAMESPACE = ClutterJson
ClutterJson_@CLUTTER_API_VERSION_AM@_gir_VERSION = @CLUTTER_API_VERSION@
ClutterJson_@CLUTTER_API_VERSION_AM@_gir_LIBS = libclutter-@CLUTTER_SONAME_INFIX@-@CLUTTER_API_VERSION@.la
ClutterJson_@CLUTTER_API_VERSION_AM@_gir_FILES = $(json_sources_c) $(json_sources_h)
ClutterJson_@CLUTTER_API_VERSION_AM@_gir_CFLAGS = $(INCLUDES) $(CLUTTER_CFLAGS) $(AM_CPPFLAGS)
ClutterJson_@CLUTTER_API_VERSION_AM@_gir_INCLUDES = GObject-2.0
ClutterJson_@CLUTTER_API_VERSION_AM@_gir_SCANNERFLAGS = \
--warn-all \
--identifier-prefix=Json \
--symbol-prefix=json
INTROSPECTION_GIRS += ClutterJson-@CLUTTER_API_VERSION@.gir
endif # LOCAL_JSON_GLIB
if SUPPORT_X11
ClutterX11-@CLUTTER_API_VERSION@.gir: Makefile Clutter-@CLUTTER_API_VERSION@.gir Cogl-@CLUTTER_API_VERSION@.gir
@ -656,7 +603,6 @@ ClutterX11_@CLUTTER_API_VERSION_AM@_gir_SCANNERFLAGS = \
--warn-all \
--identifier-prefix=ClutterX11 \
--symbol-prefix=clutter_x11 \
$(json_gir_include) \
--include-uninstalled=$(top_builddir)/clutter/Cogl-@CLUTTER_API_VERSION@.gir \
--include-uninstalled=$(top_builddir)/clutter/Clutter-@CLUTTER_API_VERSION@.gir
ClutterX11_@CLUTTER_API_VERSION_AM@_gir_INCLUDES = xlib-2.0

View file

@ -1,11 +0,0 @@
#ifndef __CLUTTER_JSON_H__
#define __CLUTTER_JSON_H__
/* Include json-glib types opaquely, so that we can swap out
* the internal copy of JSON-GLib with the installed one
* without changing the other headers.
*/
#include "@JSON_PREFIX@/json-glib.h"
#endif /* __CLUTTER_JSON_H__ */

View file

@ -27,7 +27,8 @@
#define __CLUTTER_SCRIPT_PRIVATE_H__
#include <glib-object.h>
#include "clutter-json.h"
#include <json-glib/json-glib.h>
#include "clutter-color.h"
#include "clutter-types.h"
#include "clutter-script.h"

View file

@ -210,8 +210,6 @@
#include "clutter-private.h"
#include "clutter-debug.h"
#include "json/json-parser.h"
enum
{
PROP_0,

View file

@ -30,8 +30,8 @@
#define __CLUTTER_SCRIPTABLE_H__
#include <glib-object.h>
#include <json-glib/json-glib.h>
#include <clutter/clutter-script.h>
#include <clutter/clutter-json.h>
G_BEGIN_DECLS

View file

@ -1,725 +0,0 @@
/* json-array.c - JSON array implementation
*
* 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.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@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "json-types-private.h"
/*
* SECTION:json-array
* @short_description: a JSON array representation
*
* #JsonArray is the representation of the array type inside JSON. It contains
* #JsonNode<!-- -->s, which may contain fundamental types, other arrays or
* objects.
*
* Since arrays can be expensive, they are reference counted. You can control
* the lifetime of a #JsonArray using json_array_ref() and json_array_unref().
*
* To append an element, use json_array_add_element().
* To extract an element at a given index, use json_array_get_element().
* To retrieve the entire array in list form, use json_array_get_elements().
* To retrieve the length of the array, use json_array_get_length().
*/
GType
json_array_get_type (void)
{
static GType array_type = 0;
if (G_UNLIKELY (!array_type))
array_type = g_boxed_type_register_static (g_intern_static_string ("JsonArray"),
(GBoxedCopyFunc) json_array_ref,
(GBoxedFreeFunc) json_array_unref);
return array_type;
}
/*
* json_array_new:
*
* Creates a new #JsonArray.
*
* Return value: the newly created #JsonArray
*/
JsonArray *
json_array_new (void)
{
JsonArray *array;
array = g_slice_new (JsonArray);
array->ref_count = 1;
array->elements = g_ptr_array_new ();
return array;
}
/*
* json_array_sized_new:
* @n_elements: number of slots to pre-allocate
*
* Creates a new #JsonArray with @n_elements slots already allocated.
*
* Return value: the newly created #JsonArray
*/
JsonArray *
json_array_sized_new (guint n_elements)
{
JsonArray *array;
array = g_slice_new (JsonArray);
array->ref_count = 1;
array->elements = g_ptr_array_sized_new (n_elements);
return array;
}
/*
* json_array_ref:
* @array: a #JsonArray
*
* Increase by one the reference count of a #JsonArray.
*
* Return value: the passed #JsonArray, with the reference count
* increased by one.
*/
JsonArray *
json_array_ref (JsonArray *array)
{
g_return_val_if_fail (array != NULL, NULL);
g_return_val_if_fail (array->ref_count > 0, NULL);
g_atomic_int_exchange_and_add (&array->ref_count, 1);
return array;
}
/*
* json_array_unref:
* @array: a #JsonArray
*
* Decreases by one the reference count of a #JsonArray. If the
* reference count reaches zero, the array is destroyed and all
* its allocated resources are freed.
*/
void
json_array_unref (JsonArray *array)
{
gint old_ref;
g_return_if_fail (array != NULL);
g_return_if_fail (array->ref_count > 0);
old_ref = g_atomic_int_get (&array->ref_count);
if (old_ref > 1)
g_atomic_int_compare_and_exchange (&array->ref_count, old_ref, old_ref - 1);
else
{
guint i;
for (i = 0; i < array->elements->len; i++)
json_node_free (g_ptr_array_index (array->elements, i));
g_ptr_array_free (array->elements, TRUE);
array->elements = NULL;
g_slice_free (JsonArray, array);
}
}
/*
* json_array_get_elements:
* @array: a #JsonArray
*
* Gets the elements of a #JsonArray as a list of #JsonNode<!-- -->s.
*
* Return value: a #GList containing the elements of the array. The
* contents of the list are owned by the array and should never be
* modified or freed. Use g_list_free() on the returned list when
* done using it
*/
GList *
json_array_get_elements (JsonArray *array)
{
GList *retval;
guint i;
g_return_val_if_fail (array != NULL, NULL);
retval = NULL;
for (i = 0; i < array->elements->len; i++)
retval = g_list_prepend (retval,
g_ptr_array_index (array->elements, i));
return g_list_reverse (retval);
}
/*
* json_array_dup_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Retrieves a copy of the #JsonNode containing the value of the
* element at @index_ inside a #JsonArray
*
* Return value: a copy of the #JsonNode at the requested index.
* Use json_node_free() when done.
*
* Since: 0.6
*/
JsonNode *
json_array_dup_element (JsonArray *array,
guint index_)
{
JsonNode *retval;
g_return_val_if_fail (array != NULL, NULL);
g_return_val_if_fail (index_ < array->elements->len, NULL);
retval = json_array_get_element (array, index_);
if (!retval)
return NULL;
return json_node_copy (retval);
}
/*
* json_array_get_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Retrieves the #JsonNode containing the value of the element at @index_
* inside a #JsonArray.
*
* Return value: a pointer to the #JsonNode at the requested index
*/
JsonNode *
json_array_get_element (JsonArray *array,
guint index_)
{
g_return_val_if_fail (array != NULL, NULL);
g_return_val_if_fail (index_ < array->elements->len, NULL);
return g_ptr_array_index (array->elements, index_);
}
/*
* json_array_get_int_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Conveniently retrieves the integer value of the element at @index_
* inside @array
*
* See also: json_array_get_element(), json_node_get_int()
*
* Return value: the integer value
*
* Since: 0.8
*/
gint64
json_array_get_int_element (JsonArray *array,
guint index_)
{
JsonNode *node;
g_return_val_if_fail (array != NULL, 0);
g_return_val_if_fail (index_ < array->elements->len, 0);
node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, 0);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0);
return json_node_get_int (node);
}
/*
* json_array_get_double_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Conveniently retrieves the floating point value of the element at
* @index_ inside @array
*
* See also: json_array_get_element(), json_node_get_double()
*
* Return value: the floating point value
*
* Since: 0.8
*/
gdouble
json_array_get_double_element (JsonArray *array,
guint index_)
{
JsonNode *node;
g_return_val_if_fail (array != NULL, 0.0);
g_return_val_if_fail (index_ < array->elements->len, 0.0);
node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, 0.0);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0.0);
return json_node_get_double (node);
}
/*
* json_array_get_boolean_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Conveniently retrieves the boolean value of the element at @index_
* inside @array
*
* See also: json_array_get_element(), json_node_get_boolean()
*
* Return value: the integer value
*
* Since: 0.8
*/
gboolean
json_array_get_boolean_element (JsonArray *array,
guint index_)
{
JsonNode *node;
g_return_val_if_fail (array != NULL, FALSE);
g_return_val_if_fail (index_ < array->elements->len, FALSE);
node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, FALSE);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, FALSE);
return json_node_get_boolean (node);
}
/*
* json_array_get_string_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Conveniently retrieves the string value of the element at @index_
* inside @array
*
* See also: json_array_get_element(), json_node_get_string()
*
* Return value: the string value; the returned string is owned by
* the #JsonArray and should not be modified or freed
*
* Since: 0.8
*/
G_CONST_RETURN gchar *
json_array_get_string_element (JsonArray *array,
guint index_)
{
JsonNode *node;
g_return_val_if_fail (array != NULL, NULL);
g_return_val_if_fail (index_ < array->elements->len, NULL);
node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node) || JSON_NODE_HOLDS_NULL (node), NULL);
if (JSON_NODE_HOLDS_NULL (node))
return NULL;
return json_node_get_string (node);
}
/*
* json_array_get_null_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Conveniently retrieves whether the element at @index_ is set to null
*
* See also: json_array_get_element(), JSON_NODE_TYPE(), %JSON_NODE_NULL
*
* Return value: %TRUE if the element is null
*
* Since: 0.8
*/
gboolean
json_array_get_null_element (JsonArray *array,
guint index_)
{
JsonNode *node;
g_return_val_if_fail (array != NULL, FALSE);
g_return_val_if_fail (index_ < array->elements->len, FALSE);
node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, FALSE);
return JSON_NODE_TYPE (node) == JSON_NODE_NULL;
}
/*
* json_array_get_array_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Conveniently retrieves the array from the element at @index_
* inside @array
*
* See also: json_array_get_element(), json_node_get_array()
*
* Return value: the array
*
* Since: 0.8
*/
JsonArray *
json_array_get_array_element (JsonArray *array,
guint index_)
{
JsonNode *node;
g_return_val_if_fail (array != NULL, NULL);
g_return_val_if_fail (index_ < array->elements->len, NULL);
node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node) || JSON_NODE_HOLDS_NULL (node), NULL);
if (JSON_NODE_HOLDS_NULL (node))
return NULL;
return json_node_get_array (node);
}
/*
* json_array_get_object_element:
* @array: a #JsonArray
* @index_: the index of the element to retrieve
*
* Conveniently retrieves the object from the element at @index_
* inside @array
*
* See also: json_array_get_element(), json_node_get_object()
*
* Return value: the object
*
* Since: 0.8
*/
JsonObject *
json_array_get_object_element (JsonArray *array,
guint index_)
{
JsonNode *node;
g_return_val_if_fail (array != NULL, NULL);
g_return_val_if_fail (index_ < array->elements->len, NULL);
node = g_ptr_array_index (array->elements, index_);
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node) || JSON_NODE_HOLDS_NULL (node), NULL);
if (JSON_NODE_HOLDS_NULL (node))
return NULL;
return json_node_get_object (node);
}
/*
* json_array_get_length:
* @array: a #JsonArray
*
* Retrieves the length of a #JsonArray
*
* Return value: the length of the array
*/
guint
json_array_get_length (JsonArray *array)
{
g_return_val_if_fail (array != NULL, 0);
return array->elements->len;
}
/*
* json_array_add_element:
* @array: a #JsonArray
* @node: a #JsonNode
*
* Appends @node inside @array. The array will take ownership of the
* #JsonNode.
*/
void
json_array_add_element (JsonArray *array,
JsonNode *node)
{
g_return_if_fail (array != NULL);
g_return_if_fail (node != NULL);
g_ptr_array_add (array->elements, node);
}
/*
* json_array_add_int_element:
* @array: a #JsonArray
* @value: an integer value
*
* Conveniently adds an integer @value into @array
*
* See also: json_array_add_element(), json_node_set_int()
*
* Since: 0.8
*/
void
json_array_add_int_element (JsonArray *array,
gint64 value)
{
JsonNode *node;
g_return_if_fail (array != NULL);
node = json_node_new (JSON_NODE_VALUE);
json_node_set_int (node, value);
g_ptr_array_add (array->elements, node);
}
/*
* json_array_add_double_element:
* @array: a #JsonArray
* @value: a floating point value
*
* Conveniently adds a floating point @value into @array
*
* See also: json_array_add_element(), json_node_set_double()
*
* Since: 0.8
*/
void
json_array_add_double_element (JsonArray *array,
gdouble value)
{
JsonNode *node;
g_return_if_fail (array != NULL);
node = json_node_new (JSON_NODE_VALUE);
json_node_set_double (node, value);
g_ptr_array_add (array->elements, node);
}
/*
* json_array_add_boolean_element:
* @array: a #JsonArray
* @value: a boolean value
*
* Conveniently adds a boolean @value into @array
*
* See also: json_array_add_element(), json_node_set_boolean()
*
* Since: 0.8
*/
void
json_array_add_boolean_element (JsonArray *array,
gboolean value)
{
JsonNode *node;
g_return_if_fail (array != NULL);
node = json_node_new (JSON_NODE_VALUE);
json_node_set_boolean (node, value);
g_ptr_array_add (array->elements, node);
}
/*
* json_array_add_string_element:
* @array: a #JsonArray
* @value: a string value
*
* Conveniently adds a string @value into @array
*
* See also: json_array_add_element(), json_node_set_string()
*
* Since: 0.8
*/
void
json_array_add_string_element (JsonArray *array,
const gchar *value)
{
JsonNode *node;
g_return_if_fail (array != NULL);
g_return_if_fail (value != NULL);
if (value != NULL)
{
node = json_node_new (JSON_NODE_VALUE);
json_node_set_string (node, value);
}
else
node = json_node_new (JSON_NODE_NULL);
g_ptr_array_add (array->elements, node);
}
/*
* json_array_add_null_element:
* @array: a #JsonArray
*
* Conveniently adds a null element into @array
*
* See also: json_array_add_element(), %JSON_NODE_NULL
*
* Since: 0.8
*/
void
json_array_add_null_element (JsonArray *array)
{
JsonNode *node;
g_return_if_fail (array != NULL);
node = json_node_new (JSON_NODE_NULL);
g_ptr_array_add (array->elements, node);
}
/*
* json_array_add_array_element:
* @array: a #JsonArray
* @value: a #JsonArray
*
* Conveniently adds an array into @array. The @array takes ownership
* of the newly added #JsonArray
*
* See also: json_array_add_element(), json_node_take_array()
*
* Since: 0.8
*/
void
json_array_add_array_element (JsonArray *array,
JsonArray *value)
{
JsonNode *node;
g_return_if_fail (array != NULL);
g_return_if_fail (value != NULL);
if (value != NULL)
{
node = json_node_new (JSON_NODE_ARRAY);
json_node_take_array (node, value);
}
else
node = json_node_new (JSON_NODE_NULL);
g_ptr_array_add (array->elements, node);
}
/*
* json_array_add_object_element:
* @array: a #JsonArray
* @value: a #JsonObject
*
* Conveniently adds an object into @array. The @array takes ownership
* of the newly added #JsonObject
*
* See also: json_array_add_element(), json_node_take_object()
*
* Since: 0.8
*/
void
json_array_add_object_element (JsonArray *array,
JsonObject *value)
{
JsonNode *node;
g_return_if_fail (array != NULL);
g_return_if_fail (value != NULL);
if (value != NULL)
{
node = json_node_new (JSON_NODE_OBJECT);
json_node_take_object (node, value);
}
else
node = json_node_new (JSON_NODE_NULL);
g_ptr_array_add (array->elements, node);
}
/*
* json_array_remove_element:
* @array: a #JsonArray
* @index_: the position of the element to be removed
*
* Removes the #JsonNode inside @array at @index_ freeing its allocated
* resources.
*/
void
json_array_remove_element (JsonArray *array,
guint index_)
{
g_return_if_fail (array != NULL);
g_return_if_fail (index_ < array->elements->len);
json_node_free (g_ptr_array_remove_index (array->elements, index_));
}
/*
* json_array_foreach_element:
* @array: a #JsonArray
* @func: the function to be called on each element
* @data: data to be passed to the function
*
* Iterates over all elements of @array and calls @func on
* each one of them.
*
* It is safe to change the value of a #JsonNode of the @array
* from within the iterator @func, but it is not safe to add or
* remove elements from the @array.
*
* Since: 0.8
*/
void
json_array_foreach_element (JsonArray *array,
JsonArrayForeach func,
gpointer data)
{
gint i;
g_return_if_fail (array != NULL);
g_return_if_fail (func != NULL);
for (i = 0; i < array->elements->len; i++)
{
JsonNode *element_node;
element_node = g_ptr_array_index (array->elements, i);
(* func) (array, i, element_node, data);
}
}

View file

@ -1,646 +0,0 @@
/* json-generator.c - JSON streams generator
*
* 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.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@linux.intel.com>
*/
/*
* SECTION:json-generator
* @short_description: Generates JSON data streams
*
* #JsonGenerator provides an object for generating a JSON data stream and
* put it into a buffer or a file.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "json-types-private.h"
#include "json-marshal.h"
#include "json-generator.h"
#define JSON_GENERATOR_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), JSON_TYPE_GENERATOR, JsonGeneratorPrivate))
struct _JsonGeneratorPrivate
{
JsonNode *root;
guint indent;
gunichar indent_char;
guint pretty : 1;
};
enum
{
PROP_0,
PROP_PRETTY,
PROP_INDENT,
PROP_ROOT,
PROP_INDENT_CHAR
};
static gchar *dump_value (JsonGenerator *generator,
gint level,
const gchar *name,
JsonNode *node);
static gchar *dump_array (JsonGenerator *generator,
gint level,
const gchar *name,
JsonArray *array,
gsize *length);
static gchar *dump_object (JsonGenerator *generator,
gint level,
const gchar *name,
JsonObject *object,
gsize *length);
/* non-ASCII characters can't be escaped, otherwise UTF-8
* chars will break, so we just pregenerate this table of
* high characters and then we feed it to g_strescape()
*/
static const char json_exceptions[] = {
0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae,
0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe,
0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6,
0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
0xff,
'\0' /* g_strescape() expects a NUL-terminated string */
};
G_DEFINE_TYPE (JsonGenerator, json_generator, G_TYPE_OBJECT);
static gchar *
json_strescape (const gchar *str)
{
return g_strescape (str, json_exceptions);
}
static void
json_generator_finalize (GObject *gobject)
{
JsonGeneratorPrivate *priv = JSON_GENERATOR_GET_PRIVATE (gobject);
if (priv->root)
json_node_free (priv->root);
G_OBJECT_CLASS (json_generator_parent_class)->finalize (gobject);
}
static void
json_generator_set_property (GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
JsonGeneratorPrivate *priv = JSON_GENERATOR_GET_PRIVATE (gobject);
switch (prop_id)
{
case PROP_PRETTY:
priv->pretty = g_value_get_boolean (value);
break;
case PROP_INDENT:
priv->indent = g_value_get_uint (value);
break;
case PROP_INDENT_CHAR:
priv->indent_char = g_value_get_uint (value);
break;
case PROP_ROOT:
json_generator_set_root (JSON_GENERATOR (gobject),
g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void
json_generator_get_property (GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
JsonGeneratorPrivate *priv = JSON_GENERATOR_GET_PRIVATE (gobject);
switch (prop_id)
{
case PROP_PRETTY:
g_value_set_boolean (value, priv->pretty);
break;
case PROP_INDENT:
g_value_set_uint (value, priv->indent);
break;
case PROP_INDENT_CHAR:
g_value_set_uint (value, priv->indent_char);
break;
case PROP_ROOT:
g_value_set_boxed (value, priv->root);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void
json_generator_class_init (JsonGeneratorClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (JsonGeneratorPrivate));
gobject_class->set_property = json_generator_set_property;
gobject_class->get_property = json_generator_get_property;
gobject_class->finalize = json_generator_finalize;
/*
* JsonGenerator:pretty:
*
* Whether the output should be "pretty-printed", with indentation and
* newlines. The indentation level can be controlled by using the
* JsonGenerator:indent property
*/
g_object_class_install_property (gobject_class,
PROP_PRETTY,
g_param_spec_boolean ("pretty",
"Pretty",
"Pretty-print the output",
FALSE,
G_PARAM_READWRITE));
/*
* JsonGenerator:indent:
*
* Number of spaces to be used to indent when pretty printing.
*/
g_object_class_install_property (gobject_class,
PROP_INDENT,
g_param_spec_uint ("indent",
"Indent",
"Number of indentation spaces",
0, G_MAXUINT,
2,
G_PARAM_READWRITE));
/*
* JsonGenerator:root:
*
* The root #JsonNode to be used when constructing a JSON data
* stream.
*
* Since: 0.4
*/
g_object_class_install_property (gobject_class,
PROP_ROOT,
g_param_spec_boxed ("root",
"Root",
"Root of the JSON data tree",
JSON_TYPE_NODE,
G_PARAM_READWRITE));
/*
* JsonGenerator:indent-char:
*
* The character that should be used when indenting in pretty print.
*
* Since: 0.6
*/
g_object_class_install_property (gobject_class,
PROP_INDENT_CHAR,
g_param_spec_unichar ("indent-char",
"Indent Char",
"Character that should be used when indenting",
' ',
G_PARAM_READWRITE));
}
static void
json_generator_init (JsonGenerator *generator)
{
JsonGeneratorPrivate *priv;
generator->priv = priv = JSON_GENERATOR_GET_PRIVATE (generator);
priv->pretty = FALSE;
priv->indent = 2;
priv->indent_char = ' ';
}
static gchar *
dump_value (JsonGenerator *generator,
gint level,
const gchar *name,
JsonNode *node)
{
JsonGeneratorPrivate *priv = generator->priv;
gboolean pretty = priv->pretty;
guint indent = priv->indent;
GValue value = { 0, };
GString *buffer;
buffer = g_string_new ("");
if (pretty)
{
guint i;
for (i = 0; i < (level * indent); i++)
g_string_append_c (buffer, priv->indent_char);
}
if (name && name[0] != '\0')
g_string_append_printf (buffer, "\"%s\" : ", name);
json_node_get_value (node, &value);
switch (G_VALUE_TYPE (&value))
{
case G_TYPE_INT64:
g_string_append_printf (buffer, "%" G_GINT64_FORMAT, g_value_get_int64 (&value));
break;
case G_TYPE_STRING:
{
gchar *tmp;
tmp = json_strescape (g_value_get_string (&value));
g_string_append_printf (buffer, "\"%s\"", tmp);
g_free (tmp);
}
break;
case G_TYPE_DOUBLE:
{
gchar buf[65];
g_ascii_formatd (buf, 65, "%g", g_value_get_double (&value));
g_string_append (buffer, buf);
}
break;
case G_TYPE_BOOLEAN:
g_string_append_printf (buffer, "%s",
g_value_get_boolean (&value) ? "true" : "false");
break;
default:
break;
}
g_value_unset (&value);
return g_string_free (buffer, FALSE);
}
static gchar *
dump_array (JsonGenerator *generator,
gint level,
const gchar *name,
JsonArray *array,
gsize *length)
{
JsonGeneratorPrivate *priv = generator->priv;
guint array_len = json_array_get_length (array);
guint i;
GString *buffer;
gboolean pretty = priv->pretty;
guint indent = priv->indent;
buffer = g_string_new ("");
if (pretty)
{
for (i = 0; i < (level * indent); i++)
g_string_append_c (buffer, priv->indent_char);
}
if (name && name[0] != '\0')
g_string_append_printf (buffer, "\"%s\" : ", name);
g_string_append_c (buffer, '[');
if (pretty)
g_string_append_c (buffer, '\n');
else
g_string_append_c (buffer, ' ');
for (i = 0; i < array_len; i++)
{
JsonNode *cur = json_array_get_element (array, i);
guint sub_level = level + 1;
guint j;
gchar *value;
switch (JSON_NODE_TYPE (cur))
{
case JSON_NODE_NULL:
if (pretty)
{
for (j = 0; j < (sub_level * indent); j++)
g_string_append_c (buffer, priv->indent_char);
}
g_string_append (buffer, "null");
break;
case JSON_NODE_VALUE:
value = dump_value (generator, sub_level, NULL, cur);
g_string_append (buffer, value);
g_free (value);
break;
case JSON_NODE_ARRAY:
value = dump_array (generator, sub_level, NULL, json_node_get_array (cur), NULL);
g_string_append (buffer, value);
g_free (value);
break;
case JSON_NODE_OBJECT:
value = dump_object (generator, sub_level, NULL, json_node_get_object (cur), NULL);
g_string_append (buffer, value);
g_free (value);
break;
}
if ((i + 1) != array_len)
g_string_append_c (buffer, ',');
if (pretty)
g_string_append_c (buffer, '\n');
else
g_string_append_c (buffer, ' ');
}
if (pretty)
{
for (i = 0; i < (level * indent); i++)
g_string_append_c (buffer, priv->indent_char);
}
g_string_append_c (buffer, ']');
if (length)
*length = buffer->len;
return g_string_free (buffer, FALSE);
}
static gchar *
dump_object (JsonGenerator *generator,
gint level,
const gchar *name,
JsonObject *object,
gsize *length)
{
JsonGeneratorPrivate *priv = generator->priv;
GList *members, *l;
GString *buffer;
gboolean pretty = priv->pretty;
guint indent = priv->indent;
guint i;
buffer = g_string_new ("");
if (pretty)
{
for (i = 0; i < (level * indent); i++)
g_string_append_c (buffer, priv->indent_char);
}
if (name && name[0] != '\0')
g_string_append_printf (buffer, "\"%s\" : ", name);
g_string_append_c (buffer, '{');
if (pretty)
g_string_append_c (buffer, '\n');
else
g_string_append_c (buffer, ' ');
members = json_object_get_members (object);
for (l = members; l != NULL; l = l->next)
{
const gchar *member_name = l->data;
JsonNode *cur = json_object_get_member (object, member_name);
guint sub_level = level + 1;
guint j;
gchar *value;
switch (JSON_NODE_TYPE (cur))
{
case JSON_NODE_NULL:
if (pretty)
{
for (j = 0; j < (sub_level * indent); j++)
g_string_append_c (buffer, priv->indent_char);
}
g_string_append_printf (buffer, "\"%s\" : null", member_name);
break;
case JSON_NODE_VALUE:
value = dump_value (generator, sub_level, member_name, cur);
g_string_append (buffer, value);
g_free (value);
break;
case JSON_NODE_ARRAY:
value = dump_array (generator, sub_level, member_name,
json_node_get_array (cur), NULL);
g_string_append (buffer, value);
g_free (value);
break;
case JSON_NODE_OBJECT:
value = dump_object (generator, sub_level, member_name,
json_node_get_object (cur), NULL);
g_string_append (buffer, value);
g_free (value);
break;
}
if (l->next != NULL)
g_string_append_c (buffer, ',');
if (pretty)
g_string_append_c (buffer, '\n');
else
g_string_append_c (buffer, ' ');
}
g_list_free (members);
if (pretty)
{
for (i = 0; i < (level * indent); i++)
g_string_append_c (buffer, priv->indent_char);
}
g_string_append_c (buffer, '}');
if (length)
*length = buffer->len;
return g_string_free (buffer, FALSE);
}
/*
* json_generator_new:
*
* Creates a new #JsonGenerator. You can use this object to generate a
* JSON data stream starting from a data object model composed by
* #JsonNode<!-- -->s.
*
* Return value: the newly created #JsonGenerator instance
*/
JsonGenerator *
json_generator_new (void)
{
return g_object_new (JSON_TYPE_GENERATOR, NULL);
}
/*
* json_generator_to_data:
* @generator: a #JsonGenerator
* @length: (out): return location for the length of the returned
* buffer, or %NULL
*
* Generates a JSON data stream from @generator and returns it as a
* buffer.
*
* Return value: a newly allocated buffer holding a JSON data stream.
* Use g_free() to free the allocated resources.
*/
gchar *
json_generator_to_data (JsonGenerator *generator,
gsize *length)
{
JsonNode *root;
gchar *retval = NULL;
g_return_val_if_fail (JSON_IS_GENERATOR (generator), NULL);
root = generator->priv->root;
if (!root)
{
if (length)
*length = 0;
return NULL;
}
switch (JSON_NODE_TYPE (root))
{
case JSON_NODE_ARRAY:
retval = dump_array (generator, 0, NULL, json_node_get_array (root), length);
break;
case JSON_NODE_OBJECT:
retval = dump_object (generator, 0, NULL, json_node_get_object (root), length);
break;
case JSON_NODE_NULL:
retval = g_strdup ("null");
if (length)
*length = 4;
break;
case JSON_NODE_VALUE:
retval = NULL;
break;
}
return retval;
}
/*
* json_generator_to_file:
* @generator: a #JsonGenerator
* @filename: path to the target file
* @error: return location for a #GError, or %NULL
*
* Creates a JSON data stream and puts it inside @filename, overwriting the
* current file contents. This operation is atomic.
*
* Return value: %TRUE if saving was successful.
*/
gboolean
json_generator_to_file (JsonGenerator *generator,
const gchar *filename,
GError **error)
{
gchar *buffer;
gsize len;
gboolean retval;
g_return_val_if_fail (JSON_IS_GENERATOR (generator), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
buffer = json_generator_to_data (generator, &len);
retval = g_file_set_contents (filename, buffer, len, error);
g_free (buffer);
return retval;
}
/*
* json_generator_set_root:
* @generator: a #JsonGenerator
* @node: a #JsonNode
*
* Sets @node as the root of the JSON data stream to be serialized by
* the #JsonGenerator.
*
* <note>The node is copied by the generator object, so it can be safely
* freed after calling this function.</note>
*/
void
json_generator_set_root (JsonGenerator *generator,
JsonNode *node)
{
g_return_if_fail (JSON_IS_GENERATOR (generator));
if (generator->priv->root)
{
json_node_free (generator->priv->root);
generator->priv->root = NULL;
}
if (node)
generator->priv->root = json_node_copy (node);
}

View file

@ -1,83 +0,0 @@
/* json-generator.h - JSON streams generator
*
* This file is part of JSON-GLib
* Copyright (C) 2007 OpenedHand Ltd.
*
* 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.
*
* 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.
*
* Author:
* Emmanuele Bassi <ebassi@openedhand.com>
*/
#ifndef __JSON_GENERATOR_H__
#define __JSON_GENERATOR_H__
#include <glib-object.h>
#include "json-types.h"
G_BEGIN_DECLS
#define JSON_TYPE_GENERATOR (json_generator_get_type ())
#define JSON_GENERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), JSON_TYPE_GENERATOR, JsonGenerator))
#define JSON_IS_GENERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), JSON_TYPE_GENERATOR))
#define JSON_GENERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), JSON_TYPE_GENERATOR, JsonGeneratorClass))
#define JSON_IS_GENERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), JSON_TYPE_GENERATOR))
#define JSON_GENERATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), JSON_TYPE_GENERATOR, JsonGeneratorClass))
typedef struct _JsonGenerator JsonGenerator;
typedef struct _JsonGeneratorPrivate JsonGeneratorPrivate;
typedef struct _JsonGeneratorClass JsonGeneratorClass;
/**
* JsonGenerator:
*
* JSON data streams generator. The contents of the #JsonGenerator structure
* are private and should only be accessed via the provided API.
*/
struct _JsonGenerator
{
/*< private >*/
GObject parent_instance;
JsonGeneratorPrivate *priv;
};
/**
* JsonGeneratorClass:
*
* #JsonGenerator class
*/
struct _JsonGeneratorClass
{
/*< private >*/
GObjectClass parent_class;
/* padding, for future expansion */
void (* _json_reserved1) (void);
void (* _json_reserved2) (void);
void (* _json_reserved3) (void);
void (* _json_reserved4) (void);
};
GType json_generator_get_type (void) G_GNUC_CONST;
JsonGenerator *json_generator_new (void);
gchar * json_generator_to_data (JsonGenerator *generator,
gsize *length);
gboolean json_generator_to_file (JsonGenerator *generator,
const gchar *filename,
GError **error);
void json_generator_set_root (JsonGenerator *generator,
JsonNode *node);
G_END_DECLS
#endif /* __JSON_GENERATOR_H__ */

View file

@ -1,12 +0,0 @@
#ifndef __JSON_GLIB_H__
#define __JSON_GLIB_H__
#define __JSON_GLIB_INSIDE__
#include "json-types.h"
#include "json-generator.h"
#include "json-parser.h"
#undef __JSON_GLIB_INSIDE__
#endif /* __JSON_GLIB_H__ */

View file

@ -1,130 +0,0 @@
#include "json-marshal.h"
#include <glib-object.h>
#ifdef G_ENABLE_DEBUG
#define g_marshal_value_peek_boolean(v) g_value_get_boolean (v)
#define g_marshal_value_peek_char(v) g_value_get_char (v)
#define g_marshal_value_peek_uchar(v) g_value_get_uchar (v)
#define g_marshal_value_peek_int(v) g_value_get_int (v)
#define g_marshal_value_peek_uint(v) g_value_get_uint (v)
#define g_marshal_value_peek_long(v) g_value_get_long (v)
#define g_marshal_value_peek_ulong(v) g_value_get_ulong (v)
#define g_marshal_value_peek_int64(v) g_value_get_int64 (v)
#define g_marshal_value_peek_uint64(v) g_value_get_uint64 (v)
#define g_marshal_value_peek_enum(v) g_value_get_enum (v)
#define g_marshal_value_peek_flags(v) g_value_get_flags (v)
#define g_marshal_value_peek_float(v) g_value_get_float (v)
#define g_marshal_value_peek_double(v) g_value_get_double (v)
#define g_marshal_value_peek_string(v) (char*) g_value_get_string (v)
#define g_marshal_value_peek_param(v) g_value_get_param (v)
#define g_marshal_value_peek_boxed(v) g_value_get_boxed (v)
#define g_marshal_value_peek_pointer(v) g_value_get_pointer (v)
#define g_marshal_value_peek_object(v) g_value_get_object (v)
#else /* !G_ENABLE_DEBUG */
/* WARNING: This code accesses GValues directly, which is UNSUPPORTED API.
* Do not access GValues directly in your code. Instead, use the
* g_value_get_*() functions
*/
#define g_marshal_value_peek_boolean(v) (v)->data[0].v_int
#define g_marshal_value_peek_char(v) (v)->data[0].v_int
#define g_marshal_value_peek_uchar(v) (v)->data[0].v_uint
#define g_marshal_value_peek_int(v) (v)->data[0].v_int
#define g_marshal_value_peek_uint(v) (v)->data[0].v_uint
#define g_marshal_value_peek_long(v) (v)->data[0].v_long
#define g_marshal_value_peek_ulong(v) (v)->data[0].v_ulong
#define g_marshal_value_peek_int64(v) (v)->data[0].v_int64
#define g_marshal_value_peek_uint64(v) (v)->data[0].v_uint64
#define g_marshal_value_peek_enum(v) (v)->data[0].v_long
#define g_marshal_value_peek_flags(v) (v)->data[0].v_ulong
#define g_marshal_value_peek_float(v) (v)->data[0].v_float
#define g_marshal_value_peek_double(v) (v)->data[0].v_double
#define g_marshal_value_peek_string(v) (v)->data[0].v_pointer
#define g_marshal_value_peek_param(v) (v)->data[0].v_pointer
#define g_marshal_value_peek_boxed(v) (v)->data[0].v_pointer
#define g_marshal_value_peek_pointer(v) (v)->data[0].v_pointer
#define g_marshal_value_peek_object(v) (v)->data[0].v_pointer
#endif /* !G_ENABLE_DEBUG */
/* VOID:VOID (./json-marshal.list:1) */
/* VOID:BOXED (./json-marshal.list:2) */
/* VOID:BOXED,STRING (./json-marshal.list:3) */
void
_json_marshal_VOID__BOXED_STRING (GClosure *closure,
GValue *return_value,
guint n_param_values,
const GValue *param_values,
gpointer invocation_hint,
gpointer marshal_data)
{
typedef void (*GMarshalFunc_VOID__BOXED_STRING) (gpointer data1,
gpointer arg_1,
gpointer arg_2,
gpointer data2);
register GMarshalFunc_VOID__BOXED_STRING callback;
register GCClosure *cc = (GCClosure*) closure;
register gpointer data1, data2;
g_return_if_fail (n_param_values == 3);
if (G_CCLOSURE_SWAP_DATA (closure))
{
data1 = closure->data;
data2 = g_value_peek_pointer (param_values + 0);
}
else
{
data1 = g_value_peek_pointer (param_values + 0);
data2 = closure->data;
}
callback = (GMarshalFunc_VOID__BOXED_STRING) (marshal_data ? marshal_data : cc->callback);
callback (data1,
g_marshal_value_peek_boxed (param_values + 1),
g_marshal_value_peek_string (param_values + 2),
data2);
}
/* VOID:BOXED,INT (./json-marshal.list:4) */
void
_json_marshal_VOID__BOXED_INT (GClosure *closure,
GValue *return_value,
guint n_param_values,
const GValue *param_values,
gpointer invocation_hint,
gpointer marshal_data)
{
typedef void (*GMarshalFunc_VOID__BOXED_INT) (gpointer data1,
gpointer arg_1,
gint arg_2,
gpointer data2);
register GMarshalFunc_VOID__BOXED_INT callback;
register GCClosure *cc = (GCClosure*) closure;
register gpointer data1, data2;
g_return_if_fail (n_param_values == 3);
if (G_CCLOSURE_SWAP_DATA (closure))
{
data1 = closure->data;
data2 = g_value_peek_pointer (param_values + 0);
}
else
{
data1 = g_value_peek_pointer (param_values + 0);
data2 = closure->data;
}
callback = (GMarshalFunc_VOID__BOXED_INT) (marshal_data ? marshal_data : cc->callback);
callback (data1,
g_marshal_value_peek_boxed (param_values + 1),
g_marshal_value_peek_int (param_values + 2),
data2);
}
/* VOID:POINTER (./json-marshal.list:5) */

View file

@ -1,37 +0,0 @@
#ifndef ___json_marshal_MARSHAL_H__
#define ___json_marshal_MARSHAL_H__
#include <glib-object.h>
G_BEGIN_DECLS
/* VOID:VOID (./json-marshal.list:1) */
#define _json_marshal_VOID__VOID g_cclosure_marshal_VOID__VOID
/* VOID:BOXED (./json-marshal.list:2) */
#define _json_marshal_VOID__BOXED g_cclosure_marshal_VOID__BOXED
/* VOID:BOXED,STRING (./json-marshal.list:3) */
extern void _json_marshal_VOID__BOXED_STRING (GClosure *closure,
GValue *return_value,
guint n_param_values,
const GValue *param_values,
gpointer invocation_hint,
gpointer marshal_data);
/* VOID:BOXED,INT (./json-marshal.list:4) */
extern void _json_marshal_VOID__BOXED_INT (GClosure *closure,
GValue *return_value,
guint n_param_values,
const GValue *param_values,
gpointer invocation_hint,
gpointer marshal_data);
/* VOID:POINTER (./json-marshal.list:5) */
#define _json_marshal_VOID__POINTER g_cclosure_marshal_VOID__POINTER
G_END_DECLS
#endif /* ___json_marshal_MARSHAL_H__ */

View file

@ -1,770 +0,0 @@
/* json-node.c - JSON object model node
*
* 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.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@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include "json-types-private.h"
/*
* SECTION:json-node
* @short_description: Node in a JSON object model
*
* A #JsonNode is a generic container of elements inside a JSON stream.
* It can contain fundamental types (integers, booleans, floating point
* numbers, strings) and complex types (arrays and objects).
*
* 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
* 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
*
* Creates a new #JsonNode of @type.
*
* Return value: the newly created #JsonNode
*/
JsonNode *
json_node_new (JsonNodeType type)
{
JsonNode *data;
g_return_val_if_fail (type >= JSON_NODE_OBJECT &&
type <= JSON_NODE_NULL, NULL);
data = g_slice_new0 (JsonNode);
data->type = type;
return data;
}
/*
* json_node_copy:
* @node: a #JsonNode
*
* Copies @node. If the node contains complex data types then the reference
* count of the objects is increased.
*
* Return value: the copied #JsonNode
*/
JsonNode *
json_node_copy (JsonNode *node)
{
JsonNode *copy;
g_return_val_if_fail (node != NULL, NULL);
copy = g_slice_new0 (JsonNode);
copy->type = node->type;
switch (copy->type)
{
case JSON_NODE_OBJECT:
if (node->data.object)
copy->data.object = json_object_ref (node->data.object);
break;
case JSON_NODE_ARRAY:
if (node->data.array)
copy->data.array = json_array_ref (node->data.array);
break;
case JSON_NODE_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 ();
}
return copy;
}
/*
* json_node_set_object:
* @node: a #JsonNode
* @object: a #JsonObject
*
* Sets @objects inside @node. The reference count of @object is increased.
*/
void
json_node_set_object (JsonNode *node,
JsonObject *object)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT);
if (node->data.object)
json_object_unref (node->data.object);
if (object)
node->data.object = json_object_ref (object);
else
node->data.object = NULL;
}
/*
* json_node_take_object:
* @node: a #JsonNode
* @object: a #JsonObject
*
* Sets @object inside @node. The reference count of @object is not increased.
*/
void
json_node_take_object (JsonNode *node,
JsonObject *object)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT);
if (node->data.object)
{
json_object_unref (node->data.object);
node->data.object = NULL;
}
if (object)
node->data.object = object;
}
/*
* json_node_get_object:
* @node: a #JsonNode
*
* Retrieves the #JsonObject stored inside a #JsonNode
*
* Return value: the #JsonObject
*/
JsonObject *
json_node_get_object (JsonNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL);
return node->data.object;
}
/*
* json_node_dup_object:
* @node: a #JsonNode
*
* Retrieves the #JsonObject inside @node. The reference count of
* the returned object is increased.
*
* Return value: the #JsonObject
*/
JsonObject *
json_node_dup_object (JsonNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_OBJECT, NULL);
if (node->data.object)
return json_object_ref (node->data.object);
return NULL;
}
/*
* json_node_set_array:
* @node: a #JsonNode
* @array: a #JsonArray
*
* Sets @array inside @node and increases the #JsonArray reference count
*/
void
json_node_set_array (JsonNode *node,
JsonArray *array)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);
if (node->data.array)
json_array_unref (node->data.array);
if (array)
node->data.array = json_array_ref (array);
else
node->data.array = NULL;
}
/*
* json_node_take_array:
* @node: a #JsonNode
* @array: a #JsonArray
*
* Sets @array into @node without increasing the #JsonArray reference count.
*/
void
json_node_take_array (JsonNode *node,
JsonArray *array)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY);
if (node->data.array)
{
json_array_unref (node->data.array);
node->data.array = NULL;
}
if (array)
node->data.array = array;
}
/*
* json_node_get_array:
* @node: a #JsonNode
*
* Retrieves the #JsonArray stored inside a #JsonNode
*
* Return value: the #JsonArray
*/
JsonArray *
json_node_get_array (JsonNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);
return node->data.array;
}
/*
* json_node_dup_array
* @node: a #JsonNode
*
* Retrieves the #JsonArray stored inside a #JsonNode and returns it
* with its reference count increased by one.
*
* Return value: the #JsonArray with its reference count increased.
*/
JsonArray *
json_node_dup_array (JsonNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_ARRAY, NULL);
if (node->data.array)
return json_array_ref (node->data.array);
return NULL;
}
/*
* json_node_get_value:
* @node: a #JsonNode
* @value: return location for an uninitialized value
*
* Retrieves a value from a #JsonNode and copies into @value. When done
* using it, call g_value_unset() on the #GValue.
*/
void
json_node_get_value (JsonNode *node,
GValue *value)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
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);
}
}
static void inline
node_value_unset (JsonNode *node)
{
if (G_VALUE_TYPE (&(node->data.value)) != G_TYPE_INVALID)
g_value_unset (&(node->data.value));
}
/*
* json_node_set_value:
* @node: a #JsonNode
* @value: the #GValue to set
*
* Sets @value inside @node. The passed #GValue is copied into the #JsonNode
*/
void
json_node_set_value (JsonNode *node,
const GValue *value)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
g_return_if_fail (G_VALUE_TYPE (value) != G_TYPE_INVALID);
switch (G_VALUE_TYPE (value))
{
/* direct copy for the types we use */
case G_TYPE_INT64:
case G_TYPE_BOOLEAN:
case G_TYPE_DOUBLE:
case G_TYPE_STRING:
node_value_unset (node);
g_value_init (&(node->data.value), G_VALUE_TYPE (value));
g_value_copy (value, &(node->data.value));
break;
/* auto-promote ints to long longs */
case G_TYPE_INT:
node_value_unset (node);
g_value_init (&(node->data.value), G_TYPE_INT64);
g_value_set_int64 (&(node->data.value),
g_value_get_int (value));
break;
/* auto-promote single precision to double precision */
case G_TYPE_FLOAT:
node_value_unset (node);
g_value_init (&(node->data.value), G_TYPE_DOUBLE);
g_value_set_double (&(node->data.value),
g_value_get_float (value));
break;
default:
g_warning ("Invalid value of type '%s'",
g_type_name (G_VALUE_TYPE (value)));
return;
}
}
/*
* json_node_free:
* @node: a #JsonNode
*
* Frees the resources allocated by @node.
*/
void
json_node_free (JsonNode *node)
{
if (G_LIKELY (node))
{
switch (node->type)
{
case JSON_NODE_OBJECT:
if (node->data.object)
json_object_unref (node->data.object);
break;
case JSON_NODE_ARRAY:
if (node->data.array)
json_array_unref (node->data.array);
break;
case JSON_NODE_VALUE:
g_value_unset (&(node->data.value));
break;
case JSON_NODE_NULL:
break;
}
g_slice_free (JsonNode, node);
}
}
/*
* json_node_type_name:
* @node: a #JsonNode
*
* Retrieves the user readable name of the data type contained by @node.
*
* Return value: a string containing the name of the type. The returned string
* is owned by the node and should never be modified or freed
*/
G_CONST_RETURN gchar *
json_node_type_name (JsonNode *node)
{
g_return_val_if_fail (node != NULL, "(null)");
switch (node->type)
{
case JSON_NODE_OBJECT:
return "JsonObject";
case JSON_NODE_ARRAY:
return "JsonArray";
case JSON_NODE_NULL:
return "NULL";
case JSON_NODE_VALUE:
return g_type_name (G_VALUE_TYPE (&(node->data.value)));
}
return "unknown";
}
/*
* json_node_get_parent:
* @node: a #JsonNode
*
* Retrieves the parent #JsonNode of @node.
*
* Return value: the parent node, or %NULL if @node is the root node
*/
JsonNode *
json_node_get_parent (JsonNode *node)
{
g_return_val_if_fail (node != NULL, NULL);
return node->parent;
}
/*
* json_node_set_string:
* @node: a #JsonNode of type %JSON_NODE_VALUE
* @value: a string value
*
* Sets @value as the string content of the @node, replacing any existing
* content.
*/
void
json_node_set_string (JsonNode *node,
const gchar *value)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
g_value_set_string (&(node->data.value), value);
else
{
GValue copy = { 0, };
g_value_init (&copy, G_TYPE_STRING);
g_value_set_string (&copy, value);
json_node_set_value (node, &copy);
g_value_unset (&copy);
}
}
/*
* json_node_get_string:
* @node: a #JsonNode of type %JSON_NODE_VALUE
*
* Gets the string value stored inside a #JsonNode
*
* Return value: a string value.
*/
G_CONST_RETURN gchar *
json_node_get_string (JsonNode *node)
{
g_return_val_if_fail (node != NULL, 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));
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);
if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
return NULL;
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_STRING)
return g_value_dup_string (&(node->data.value));
return NULL;
}
/*
* json_node_set_int:
* @node: a #JsonNode of type %JSON_NODE_VALUE
* @value: an integer value
*
* Sets @value as the integer content of the @node, replacing any existing
* content.
*/
void
json_node_set_int (JsonNode *node,
gint64 value)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT64)
g_value_set_int64 (&(node->data.value), value);
else
{
GValue copy = { 0, };
g_value_init (&copy, G_TYPE_INT64);
g_value_set_int64 (&copy, value);
json_node_set_value (node, &copy);
g_value_unset (&copy);
}
}
/*
* json_node_get_int:
* @node: a #JsonNode of type %JSON_NODE_VALUE
*
* Gets the integer value stored inside a #JsonNode
*
* Return value: an integer value.
*/
gint64
json_node_get_int (JsonNode *node)
{
g_return_val_if_fail (node != NULL, 0);
if (JSON_NODE_TYPE (node) == JSON_NODE_NULL)
return 0;
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_INT64)
return g_value_get_int64 (&(node->data.value));
return 0;
}
/*
* json_node_set_double:
* @node: a #JsonNode of type %JSON_NODE_VALUE
* @value: a double value
*
* Sets @value as the double content of the @node, replacing any existing
* content.
*/
void
json_node_set_double (JsonNode *node,
gdouble value)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_DOUBLE)
g_value_set_double (&(node->data.value), value);
else
{
GValue copy = { 0, };
g_value_init (&copy, G_TYPE_DOUBLE);
g_value_set_double (&copy, value);
json_node_set_value (node, &copy);
g_value_unset (&copy);
}
}
/*
* json_node_get_double:
* @node: a #JsonNode of type %JSON_NODE_VALUE
*
* Gets the double value stored inside a #JsonNode
*
* Return value: a double value.
*/
gdouble
json_node_get_double (JsonNode *node)
{
g_return_val_if_fail (node != NULL, 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));
return 0.0;
}
/*
* json_node_set_boolean:
* @node: a #JsonNode of type %JSON_NODE_VALUE
* @value: a boolean value
*
* Sets @value as the boolean content of the @node, replacing any existing
* content.
*/
void
json_node_set_boolean (JsonNode *node,
gboolean value)
{
g_return_if_fail (node != NULL);
g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE);
if (G_VALUE_TYPE (&(node->data.value)) == G_TYPE_BOOLEAN)
g_value_set_boolean (&(node->data.value), value);
else
{
GValue copy = { 0, };
g_value_init (&copy, G_TYPE_BOOLEAN);
g_value_set_boolean (&copy, value);
json_node_set_value (node, &copy);
g_value_unset (&copy);
}
}
/*
* json_node_get_boolean:
* @node: a #JsonNode of type %JSON_NODE_VALUE
*
* Gets the boolean value stored inside a #JsonNode
*
* Return value: a boolean value.
*/
gboolean
json_node_get_boolean (JsonNode *node)
{
g_return_val_if_fail (node != NULL, 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;
}

View file

@ -1,888 +0,0 @@
/* json-object.c - JSON object implementation
*
* 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.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@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include "json-types-private.h"
/*
* SECTION:json-object
* @short_description: a JSON object representation
*
* #JsonArray is the representation of the object type inside JSON. It contains
* #JsonNode<!-- -->s, which may contain fundamental types, arrays or other
* objects. Each member of an object is accessed using its name. Please note
* that the member names are normalized internally before being used; every
* delimiter matching the %G_STR_DELIMITER macro will be transformed into an
* underscore, so for instance "member-name" and "member_name" are equivalent
* for a #JsonObject.
*
* Since objects can be expensive, they are reference counted. You can control
* the lifetime of a #JsonObject using json_object_ref() and json_object_unref().
*
* To add or overwrite a member with a given name, use json_object_set_member().
* To extract a member with a given name, use json_object_get_member().
* To retrieve the list of members, use json_object_get_members().
* To retrieve the size of the object (that is, the number of members it has),
* use json_object_get_size().
*/
GType
json_object_get_type (void)
{
static GType object_type = 0;
if (G_UNLIKELY (!object_type))
object_type = g_boxed_type_register_static (g_intern_static_string ("JsonObject"),
(GBoxedCopyFunc) json_object_ref,
(GBoxedFreeFunc) json_object_unref);
return object_type;
}
/*
* json_object_new:
*
* Creates a new #JsonObject, an JSON object type representation.
*
* Return value: the newly created #JsonObject
*/
JsonObject *
json_object_new (void)
{
JsonObject *object;
object = g_slice_new (JsonObject);
object->ref_count = 1;
object->members = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free,
(GDestroyNotify) json_node_free);
return object;
}
/*
* json_object_ref:
* @object: a #JsonObject
*
* Increase by one the reference count of a #JsonObject.
*
* Return value: the passed #JsonObject, with the reference count
* increased by one.
*/
JsonObject *
json_object_ref (JsonObject *object)
{
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (object->ref_count > 0, NULL);
g_atomic_int_exchange_and_add (&object->ref_count, 1);
return object;
}
/*
* json_object_unref:
* @object: a #JsonObject
*
* Decreases by one the reference count of a #JsonObject. If the
* reference count reaches zero, the object is destroyed and all
* its allocated resources are freed.
*/
void
json_object_unref (JsonObject *object)
{
gint old_ref;
g_return_if_fail (object != NULL);
g_return_if_fail (object->ref_count > 0);
old_ref = g_atomic_int_get (&object->ref_count);
if (old_ref > 1)
g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1);
else
{
g_hash_table_destroy (object->members);
object->members = NULL;
g_slice_free (JsonObject, object);
}
}
static inline void
object_set_member_internal (JsonObject *object,
const gchar *member_name,
JsonNode *node)
{
g_hash_table_replace (object->members,
g_strdup (member_name),
node);
}
/*
* json_object_add_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @node: the value of the member
*
* Adds a member named @member_name and containing @node into a #JsonObject.
* The object will take ownership of the #JsonNode.
*
* This function will return if the @object already contains a member
* @member_name.
*
* Deprecated: 0.8: Use json_object_set_member() instead
*/
void
json_object_add_member (JsonObject *object,
const gchar *member_name,
JsonNode *node)
{
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
g_return_if_fail (node != NULL);
if (json_object_has_member (object, member_name))
{
g_warning ("JsonObject already has a `%s' member of type `%s'",
member_name,
json_node_type_name (node));
return;
}
object_set_member_internal (object, member_name, node);
}
/*
* json_object_set_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @node: the value of the member
*
* Sets @node as the value of @member_name inside @object.
*
* If @object already contains a member called @member_name then
* the member's current value is overwritten. Otherwise, a new
* member is added to @object.
*
* Since: 0.8
*/
void
json_object_set_member (JsonObject *object,
const gchar *member_name,
JsonNode *node)
{
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
g_return_if_fail (node != NULL);
object_set_member_internal (object, member_name, node);
}
/*
* json_object_set_int_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @value: the value of the member
*
* Convenience function for setting an integer @value of
* @member_name inside @object.
*
* See also: json_object_set_member()
*
* Since: 0.8
*/
void
json_object_set_int_member (JsonObject *object,
const gchar *member_name,
gint64 value)
{
JsonNode *node;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
node = json_node_new (JSON_NODE_VALUE);
json_node_set_int (node, value);
object_set_member_internal (object, member_name, node);
}
/*
* json_object_set_double_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @value: the value of the member
*
* Convenience function for setting a floating point @value
* of @member_name inside @object.
*
* See also: json_object_set_member()
*
* Since: 0.8
*/
void
json_object_set_double_member (JsonObject *object,
const gchar *member_name,
gdouble value)
{
JsonNode *node;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
node = json_node_new (JSON_NODE_VALUE);
json_node_set_double (node, value);
object_set_member_internal (object, member_name, node);
}
/*
* json_object_set_boolean_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @value: the value of the member
*
* Convenience function for setting a boolean @value of
* @member_name inside @object.
*
* See also: json_object_set_member()
*
* Since: 0.8
*/
void
json_object_set_boolean_member (JsonObject *object,
const gchar *member_name,
gboolean value)
{
JsonNode *node;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
node = json_node_new (JSON_NODE_VALUE);
json_node_set_boolean (node, value);
object_set_member_internal (object, member_name, node);
}
/*
* json_object_set_string_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @value: the value of the member
*
* Convenience function for setting a string @value of
* @member_name inside @object.
*
* See also: json_object_set_member()
*
* Since: 0.8
*/
void
json_object_set_string_member (JsonObject *object,
const gchar *member_name,
const gchar *value)
{
JsonNode *node;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
if (value != NULL)
{
node = json_node_new (JSON_NODE_VALUE);
json_node_set_string (node, value);
}
else
node = json_node_new (JSON_NODE_NULL);
object_set_member_internal (object, member_name, node);
}
/*
* json_object_set_null_member:
* @object: a #JsonObject
* @member_name: the name of the member
*
* Convenience function for setting a null @value of
* @member_name inside @object.
*
* See also: json_object_set_member()
*
* Since: 0.8
*/
void
json_object_set_null_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *node;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
node = json_node_new (JSON_NODE_NULL);
object_set_member_internal (object, member_name, node);
}
/*
* json_object_set_array_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @value: the value of the member
*
* Convenience function for setting an array @value of
* @member_name inside @object.
*
* The @object will take ownership of the passed #JsonArray
*
* See also: json_object_set_member()
*
* Since: 0.8
*/
void
json_object_set_array_member (JsonObject *object,
const gchar *member_name,
JsonArray *value)
{
JsonNode *node;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
if (value != NULL)
{
node = json_node_new (JSON_NODE_ARRAY);
json_node_take_array (node, value);
}
else
node = json_node_new (JSON_NODE_NULL);
object_set_member_internal (object, member_name, node);
}
/*
* json_object_set_object_member:
* @object: a #JsonObject
* @member_name: the name of the member
* @value: the value of the member
*
* Convenience function for setting an object @value of
* @member_name inside @object.
*
* The @object will take ownership of the passed #JsonObject
*
* See also: json_object_set_member()
*
* Since: 0.8
*/
void
json_object_set_object_member (JsonObject *object,
const gchar *member_name,
JsonObject *value)
{
JsonNode *node;
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
if (value != NULL)
{
node = json_node_new (JSON_NODE_OBJECT);
json_node_take_object (node, value);
}
else
node = json_node_new (JSON_NODE_NULL);
object_set_member_internal (object, member_name, node);
}
/* FIXME: yuck */
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 14
static void
get_keys (gpointer key,
gpointer value,
gpointer user_data)
{
GList **keys = user_data;
*keys = g_list_prepend (*keys, key);
}
static void
get_values (gpointer key,
gpointer value,
gpointer user_data)
{
GList **values = user_data;
*values = g_list_prepend (*values, value);
}
static GList *
g_hash_table_get_keys (GHashTable *hash_table)
{
GList *retval = NULL;
g_return_val_if_fail (hash_table != NULL, NULL);
g_hash_table_foreach (hash_table, get_keys, &retval);
return retval;
}
static GList *
g_hash_table_get_values (GHashTable *hash_table)
{
GList *retval = NULL;
g_return_val_if_fail (hash_table != NULL, NULL);
g_hash_table_foreach (hash_table, get_values, &retval);
return retval;
}
#endif /* GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 14 */
/*
* json_object_get_members:
* @object: a #JsonObject
*
* Retrieves all the names of the members of a #JsonObject. You can
* obtain the value for each member using json_object_get_member().
*
* Return value: a #GList of member names. The content of the list
* is owned by the #JsonObject and should never be modified or
* freed. When you have finished using the returned list, use
* g_list_free() to free the resources it has allocated.
*/
GList *
json_object_get_members (JsonObject *object)
{
g_return_val_if_fail (object != NULL, NULL);
return g_hash_table_get_keys (object->members);
}
/*
* json_object_get_values:
* @object: a #JsonObject
*
* Retrieves all the values of the members of a #JsonObject.
*
* Return value: a #GList of #JsonNode<!-- -->s. The content of the
* list is owned by the #JsonObject and should never be modified
* or freed. When you have finished using the returned list, use
* g_list_free() to free the resources it has allocated.
*/
GList *
json_object_get_values (JsonObject *object)
{
g_return_val_if_fail (object != NULL, NULL);
return g_hash_table_get_values (object->members);
}
/*
* json_object_dup_member:
* @object: a #JsonObject
* @member_name: the name of the JSON object member to access
*
* Retrieves a copy of the #JsonNode containing the value of @member_name
* inside a #JsonObject
*
* Return value: a copy of the node for the requested object member
* or %NULL. Use json_node_free() when done.
*
* Since: 0.6
*/
JsonNode *
json_object_dup_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *retval;
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (member_name != NULL, NULL);
retval = json_object_get_member (object, member_name);
if (!retval)
return NULL;
return json_node_copy (retval);
}
static inline JsonNode *
object_get_member_internal (JsonObject *object,
const gchar *member_name)
{
return g_hash_table_lookup (object->members, member_name);
}
/*
* json_object_get_member:
* @object: a #JsonObject
* @member_name: the name of the JSON object member to access
*
* Retrieves the #JsonNode containing the value of @member_name inside
* a #JsonObject.
*
* Return value: a pointer to the node for the requested object
* member, or %NULL
*/
JsonNode *
json_object_get_member (JsonObject *object,
const gchar *member_name)
{
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (member_name != NULL, NULL);
return object_get_member_internal (object, member_name);
}
/*
* json_object_get_int_member:
* @object: a #JsonObject
* @member_name: the name of the member
*
* Convenience function that retrieves the integer value
* stored in @member_name of @object
*
* See also: json_object_get_member()
*
* Return value: the integer value of the object's member
*
* Since: 0.8
*/
gint64
json_object_get_int_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *node;
g_return_val_if_fail (object != NULL, 0);
g_return_val_if_fail (member_name != NULL, 0);
node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, 0);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0);
return json_node_get_int (node);
}
/*
* json_object_get_double_member:
* @object: a #JsonObject
* @member_name: the name of the member
*
* Convenience function that retrieves the floating point value
* stored in @member_name of @object
*
* See also: json_object_get_member()
*
* Return value: the floating point value of the object's member
*
* Since: 0.8
*/
gdouble
json_object_get_double_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *node;
g_return_val_if_fail (object != NULL, 0.0);
g_return_val_if_fail (member_name != NULL, 0.0);
node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, 0.0);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, 0.0);
return json_node_get_double (node);
}
/*
* json_object_get_boolean_member:
* @object: a #JsonObject
* @member_name: the name of the member
*
* Convenience function that retrieves the boolean value
* stored in @member_name of @object
*
* See also: json_object_get_member()
*
* Return value: the boolean value of the object's member
*
* Since: 0.8
*/
gboolean
json_object_get_boolean_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *node;
g_return_val_if_fail (object != NULL, FALSE);
g_return_val_if_fail (member_name != NULL, FALSE);
node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, FALSE);
g_return_val_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE, FALSE);
return json_node_get_boolean (node);
}
/*
* json_object_get_null_member:
* @object: a #JsonObject
* @member_name: the name of the member
*
* Convenience function that checks whether the value
* stored in @member_name of @object is null
*
* See also: json_object_get_member()
*
* Return value: %TRUE if the value is null
*
* Since: 0.8
*/
gboolean
json_object_get_null_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *node;
g_return_val_if_fail (object != NULL, FALSE);
g_return_val_if_fail (member_name != NULL, FALSE);
node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, FALSE);
return JSON_NODE_TYPE (node) == JSON_NODE_NULL;
}
/*
* json_object_get_string_member:
* @object: a #JsonObject
* @member_name: the name of the member
*
* Convenience function that retrieves the string value
* stored in @member_name of @object
*
* See also: json_object_get_member()
*
* Return value: the string value of the object's member
*
* Since: 0.8
*/
G_CONST_RETURN gchar *
json_object_get_string_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *node;
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (member_name != NULL, NULL);
node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node) || JSON_NODE_HOLDS_NULL (node), NULL);
if (JSON_NODE_HOLDS_NULL (node))
return NULL;
return json_node_get_string (node);
}
/*
* json_object_get_array_member:
* @object: a #JsonObject
* @member_name: the name of the member
*
* Convenience function that retrieves the array
* stored in @member_name of @object
*
* See also: json_object_get_member()
*
* Return value: the array inside the object's member
*
* Since: 0.8
*/
JsonArray *
json_object_get_array_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *node;
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (member_name != NULL, NULL);
node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node) || JSON_NODE_HOLDS_NULL (node), NULL);
if (JSON_NODE_HOLDS_NULL (node))
return NULL;
return json_node_get_array (node);
}
/*
* json_object_get_object_member:
* @object: a #JsonObject
* @member_name: the name of the member
*
* Convenience function that retrieves the object
* stored in @member_name of @object
*
* See also: json_object_get_member()
*
* Return value: the object inside the object's member
*
* Since: 0.8
*/
JsonObject *
json_object_get_object_member (JsonObject *object,
const gchar *member_name)
{
JsonNode *node;
g_return_val_if_fail (object != NULL, NULL);
g_return_val_if_fail (member_name != NULL, NULL);
node = object_get_member_internal (object, member_name);
g_return_val_if_fail (node != NULL, NULL);
g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node) || JSON_NODE_HOLDS_NULL (node), NULL);
if (JSON_NODE_HOLDS_NULL (node))
return NULL;
return json_node_get_object (node);
}
/*
* json_object_has_member:
* @object: a #JsonObject
* @member_name: the name of a JSON object member
*
* Checks whether @object has a member named @member_name.
*
* Return value: %TRUE if the JSON object has the requested member
*/
gboolean
json_object_has_member (JsonObject *object,
const gchar *member_name)
{
g_return_val_if_fail (object != NULL, FALSE);
g_return_val_if_fail (member_name != NULL, FALSE);
return (g_hash_table_lookup (object->members, member_name) != NULL);
}
/*
* json_object_get_size:
* @object: a #JsonObject
*
* Retrieves the number of members of a #JsonObject.
*
* Return value: the number of members
*/
guint
json_object_get_size (JsonObject *object)
{
g_return_val_if_fail (object != NULL, 0);
return g_hash_table_size (object->members);
}
/*
* json_object_remove_member:
* @object: a #JsonObject
* @member_name: the name of the member to remove
*
* Removes @member_name from @object, freeing its allocated resources.
*/
void
json_object_remove_member (JsonObject *object,
const gchar *member_name)
{
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
g_hash_table_remove (object->members, member_name);
}
typedef struct _ForeachClosure ForeachClosure;
struct _ForeachClosure
{
JsonObject *object;
JsonObjectForeach func;
gpointer data;
};
static void
json_object_foreach_internal (gpointer key,
gpointer value,
gpointer data)
{
ForeachClosure *clos = data;
const gchar *member_name = key;
JsonNode *member_node = value;
clos->func (clos->object, member_name, member_node, clos->data);
}
/*
* json_object_foreach_member:
* @object: a #JsonObject
* @func: the function to be called on each member
* @data: data to be passed to the function
*
* Iterates over all members of @object and calls @func on
* each one of them.
*
* It is safe to change the value of a #JsonNode of the @object
* from within the iterator @func, but it is not safe to add or
* remove members from the @object.
*
* Since: 0.8
*/
void
json_object_foreach_member (JsonObject *object,
JsonObjectForeach func,
gpointer data)
{
ForeachClosure clos;
g_return_if_fail (object != NULL);
g_return_if_fail (func != NULL);
clos.object = object;
clos.func = func;
clos.data = data;
g_hash_table_foreach (object->members,
json_object_foreach_internal,
&clos);
}

File diff suppressed because it is too large Load diff

View file

@ -1,147 +0,0 @@
/* json-parser.h - JSON streams parser
*
* This file is part of JSON-GLib
* Copyright (C) 2007 OpenedHand Ltd.
*
* 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.
*
* 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.
*
* Author:
* Emmanuele Bassi <ebassi@openedhand.com>
*/
#ifndef __JSON_PARSER_H__
#define __JSON_PARSER_H__
#include <glib-object.h>
#include "json-types.h"
G_BEGIN_DECLS
#define JSON_TYPE_PARSER (json_parser_get_type ())
#define JSON_PARSER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), JSON_TYPE_PARSER, JsonParser))
#define JSON_IS_PARSER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), JSON_TYPE_PARSER))
#define JSON_PARSER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), JSON_TYPE_PARSER, JsonParserClass))
#define JSON_IS_PARSER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), JSON_TYPE_PARSER))
#define JSON_PARSER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), JSON_TYPE_PARSER, JsonParserClass))
#define JSON_PARSER_ERROR (json_parser_error_quark ())
typedef struct _JsonParser JsonParser;
typedef struct _JsonParserPrivate JsonParserPrivate;
typedef struct _JsonParserClass JsonParserClass;
/**
* JsonParserError:
* @JSON_PARSER_ERROR_PARSE: parse error
* @JSON_PARSER_ERROR_UNKNOWN: unknown error
*
* Error enumeration for #JsonParser
*/
typedef enum {
JSON_PARSER_ERROR_PARSE,
JSON_PARSER_ERROR_UNKNOWN
} JsonParserError;
typedef enum {
JSON_TOKEN_INVALID = G_TOKEN_LAST,
JSON_TOKEN_TRUE,
JSON_TOKEN_FALSE,
JSON_TOKEN_NULL,
JSON_TOKEN_LAST
} JsonTokenType;
/**
* JsonParser:
*
* JSON data streams parser. The contents of the #JsonParser structure are
* private and should only be accessed via the provided API.
*/
struct _JsonParser
{
/*< private >*/
GObject parent_instance;
JsonParserPrivate *priv;
};
/**
* JsonParserClass:
* @parse_start: class handler for the JsonParser::parse-start signal
* @object_start: class handler for the JsonParser::object-start signal
* @object_member: class handler for the JsonParser::object-member signal
* @object_end: class handler for the JsonParser::object-end signal
* @array_start: class handler for the JsonParser::array-start signal
* @array_element: class handler for the JsonParser::array-element signal
* @array_end: class handler for the JsonParser::array-end signal
* @parse_end: class handler for the JsonParser::parse-end signal
* @error: class handler for the JsonParser::error signal
*
* #JsonParser class.
*/
struct _JsonParserClass
{
/*< private >*/
GObjectClass parent_class;
/*< public >*/
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);
/*< private >*/
/* padding for future expansion */
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);
};
GQuark json_parser_error_quark (void);
GType json_parser_get_type (void) G_GNUC_CONST;
JsonParser *json_parser_new (void);
gboolean json_parser_load_from_file (JsonParser *parser,
const gchar *filename,
GError **error);
gboolean json_parser_load_from_data (JsonParser *parser,
const gchar *data,
gssize length,
GError **error);
JsonNode * json_parser_get_root (JsonParser *parser);
guint json_parser_get_current_line (JsonParser *parser);
guint json_parser_get_current_pos (JsonParser *parser);
G_END_DECLS
#endif /* __JSON_PARSER_H__ */

View file

@ -1,61 +0,0 @@
/* json-types-private.h - JSON data types private header
*
* 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.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@linux.intel.com>
*/
#ifndef __JSON_TYPES_PRIVATE_H__
#define __JSON_TYPES_PRIVATE_H__
#include "json-types.h"
G_BEGIN_DECLS
struct _JsonNode
{
/*< private >*/
JsonNodeType type;
union {
JsonObject *object;
JsonArray *array;
GValue value;
} data;
JsonNode *parent;
};
struct _JsonArray
{
GPtrArray *elements;
volatile gint ref_count;
};
struct _JsonObject
{
GHashTable *members;
volatile gint ref_count;
};
G_END_DECLS
#endif /* __JSON_TYPES_PRIVATE_H__ */

View file

@ -1,288 +0,0 @@
/* json-types.h - JSON data types
*
* 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.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@linux.intel.com>
*/
#if !defined(__JSON_GLIB_INSIDE__) && !defined(JSON_COMPILATION)
#error "Only <json-glib/json-glib.h> can be included directly."
#endif
#ifndef __JSON_TYPES_H__
#define __JSON_TYPES_H__
#include <glib-object.h>
G_BEGIN_DECLS
/**
* JSON_NODE_TYPE:
* @node: a #JsonNode
*
* Evaluates to the #JsonNodeType contained by @node
*/
#define JSON_NODE_TYPE(node) (json_node_get_node_type ((node)))
#define JSON_TYPE_NODE (json_node_get_type ())
#define JSON_TYPE_OBJECT (json_object_get_type ())
#define JSON_TYPE_ARRAY (json_array_get_type ())
#define JSON_NODE_HOLDS(node,type) (JSON_NODE_TYPE (node) == (type))
#define JSON_NODE_HOLDS_VALUE(node) (JSON_NODE_HOLDS (node, JSON_NODE_VALUE))
#define JSON_NODE_HOLDS_ARRAY(node) (JSON_NODE_HOLDS (node, JSON_NODE_ARRAY))
#define JSON_NODE_HOLDS_OBJECT(node) (JSON_NODE_HOLDS (node, JSON_NODE_OBJECT))
#define JSON_NODE_HOLDS_NULL(node) (JSON_NODE_HOLDS (node, JSON_NODE_NULL))
/**
* JsonNode:
* @type: the type of node
*
* A generic container of JSON data types. The contents of the #JsonNode
* structure are private and should only be accessed via the provided
* functions and never directly.
*/
typedef struct _JsonNode JsonNode;
/**
* JsonObject:
*
* A JSON object type. The contents of the #JsonObject structure are private
* and should only be accessed by the provided API
*/
typedef struct _JsonObject JsonObject;
/**
* JsonArray:
*
* A JSON array type. The contents of the #JsonArray structure are private
* and should only be accessed by the provided API
*/
typedef struct _JsonArray JsonArray;
/**
* JsonNodeType:
* @JSON_NODE_OBJECT: The node contains a #JsonObject
* @JSON_NODE_ARRAY: The node contains a #JsonArray
* @JSON_NODE_VALUE: The node contains a fundamental type
* @JSON_NODE_NULL: Special type, for nodes containing null
*
* Indicates the content of a #JsonNode.
*/
typedef enum {
JSON_NODE_OBJECT,
JSON_NODE_ARRAY,
JSON_NODE_VALUE,
JSON_NODE_NULL
} JsonNodeType;
/**
* JsonObjectForeach:
* @object: the iterated #JsonObject
* @member_name: the name of the member
* @member_node: a #JsonNode containing the @member_name value
* @user_data: data passed to the function
*
* The function to be passed to json_object_foreach_member(). You
* should not add or remove members to and from @object within
* this function. It is safe to change the value of @member_node.
*
* Since: 0.8
*/
typedef void (* JsonObjectForeach) (JsonObject *object,
const gchar *member_name,
JsonNode *member_node,
gpointer user_data);
/**
* JsonArrayForeach:
* @array: the iterated #JsonArray
* @index_: the index of the element
* @element_node: a #JsonNode containing the value at @index_
* @user_data: data passed to the function
*
* The function to be passed to json_array_foreach_element(). You
* should not add or remove elements to and from @array within
* this function. It is safe to change the value of @element_node.
*
* Since: 0.8
*/
typedef void (* JsonArrayForeach) (JsonArray *array,
guint index_,
JsonNode *element_node,
gpointer user_data);
/*
* JsonNode
*/
GType json_node_get_type (void) G_GNUC_CONST;
JsonNode * json_node_new (JsonNodeType type);
JsonNode * json_node_copy (JsonNode *node);
void json_node_free (JsonNode *node);
JsonNodeType json_node_get_node_type (JsonNode *node);
GType json_node_get_value_type (JsonNode *node);
JsonNode * json_node_get_parent (JsonNode *node);
G_CONST_RETURN gchar *json_node_type_name (JsonNode *node);
void json_node_set_object (JsonNode *node,
JsonObject *object);
void json_node_take_object (JsonNode *node,
JsonObject *object);
JsonObject * json_node_get_object (JsonNode *node);
JsonObject * json_node_dup_object (JsonNode *node);
void json_node_set_array (JsonNode *node,
JsonArray *array);
void json_node_take_array (JsonNode *node,
JsonArray *array);
JsonArray * json_node_get_array (JsonNode *node);
JsonArray * json_node_dup_array (JsonNode *node);
void json_node_set_value (JsonNode *node,
const GValue *value);
void json_node_get_value (JsonNode *node,
GValue *value);
void json_node_set_string (JsonNode *node,
const gchar *value);
G_CONST_RETURN gchar *json_node_get_string (JsonNode *node);
gchar * json_node_dup_string (JsonNode *node);
void json_node_set_int (JsonNode *node,
gint64 value);
gint64 json_node_get_int (JsonNode *node);
void json_node_set_double (JsonNode *node,
gdouble value);
gdouble json_node_get_double (JsonNode *node);
void json_node_set_boolean (JsonNode *node,
gboolean value);
gboolean json_node_get_boolean (JsonNode *node);
gboolean json_node_is_null (JsonNode *node);
/*
* JsonObject
*/
GType json_object_get_type (void) G_GNUC_CONST;
JsonObject * json_object_new (void);
JsonObject * json_object_ref (JsonObject *object);
void json_object_unref (JsonObject *object);
#ifndef JSON_DISABLE_DEPRECATED
void json_object_add_member (JsonObject *object,
const gchar *member_name,
JsonNode *node) G_GNUC_DEPRECATED;
#endif /* JSON_DISABLE_DEPRECATED */
void json_object_set_member (JsonObject *object,
const gchar *member_name,
JsonNode *node);
void json_object_set_int_member (JsonObject *object,
const gchar *member_name,
gint64 value);
void json_object_set_double_member (JsonObject *object,
const gchar *member_name,
gdouble value);
void json_object_set_boolean_member (JsonObject *object,
const gchar *member_name,
gboolean value);
void json_object_set_string_member (JsonObject *object,
const gchar *member_name,
const gchar *value);
void json_object_set_null_member (JsonObject *object,
const gchar *member_name);
void json_object_set_array_member (JsonObject *object,
const gchar *member_name,
JsonArray *value);
void json_object_set_object_member (JsonObject *object,
const gchar *member_name,
JsonObject *value);
GList * json_object_get_members (JsonObject *object);
JsonNode * json_object_get_member (JsonObject *object,
const gchar *member_name);
JsonNode * json_object_dup_member (JsonObject *object,
const gchar *member_name);
gint64 json_object_get_int_member (JsonObject *object,
const gchar *member_name);
gdouble json_object_get_double_member (JsonObject *object,
const gchar *member_name);
gboolean json_object_get_boolean_member (JsonObject *object,
const gchar *member_name);
G_CONST_RETURN gchar *json_object_get_string_member (JsonObject *object,
const gchar *member_name);
gboolean json_object_get_null_member (JsonObject *object,
const gchar *member_name);
JsonArray * json_object_get_array_member (JsonObject *object,
const gchar *member_name);
JsonObject * json_object_get_object_member (JsonObject *object,
const gchar *member_name);
gboolean json_object_has_member (JsonObject *object,
const gchar *member_name);
void json_object_remove_member (JsonObject *object,
const gchar *member_name);
GList * json_object_get_values (JsonObject *object);
guint json_object_get_size (JsonObject *object);
void json_object_foreach_member (JsonObject *object,
JsonObjectForeach func,
gpointer data);
GType json_array_get_type (void) G_GNUC_CONST;
JsonArray * json_array_new (void);
JsonArray * json_array_sized_new (guint n_elements);
JsonArray * json_array_ref (JsonArray *array);
void json_array_unref (JsonArray *array);
void json_array_add_element (JsonArray *array,
JsonNode *node);
void json_array_add_int_element (JsonArray *array,
gint64 value);
void json_array_add_double_element (JsonArray *array,
gdouble value);
void json_array_add_boolean_element (JsonArray *array,
gboolean value);
void json_array_add_string_element (JsonArray *array,
const gchar *value);
void json_array_add_null_element (JsonArray *array);
void json_array_add_array_element (JsonArray *array,
JsonArray *value);
void json_array_add_object_element (JsonArray *array,
JsonObject *value);
GList * json_array_get_elements (JsonArray *array);
JsonNode * json_array_get_element (JsonArray *array,
guint index_);
gint64 json_array_get_int_element (JsonArray *array,
guint index_);
gdouble json_array_get_double_element (JsonArray *array,
guint index_);
gboolean json_array_get_boolean_element (JsonArray *array,
guint index_);
G_CONST_RETURN gchar *json_array_get_string_element (JsonArray *array,
guint index_);
gboolean json_array_get_null_element (JsonArray *array,
guint index_);
JsonArray * json_array_get_array_element (JsonArray *array,
guint index_);
JsonObject * json_array_get_object_element (JsonArray *array,
guint index_);
JsonNode * json_array_dup_element (JsonArray *array,
guint index_);
void json_array_remove_element (JsonArray *array,
guint index_);
guint json_array_get_length (JsonArray *array);
void json_array_foreach_element (JsonArray *array,
JsonArrayForeach func,
gpointer data);
G_END_DECLS
#endif /* __JSON_TYPES_H__ */

View file

@ -795,80 +795,6 @@ AS_IF([test "x$SUPPORT_XLIB" = "x1"],
AM_CONDITIONAL(X11_TESTS, [test "x$x11_tests" = "xyes"])
dnl === JSON parser check =====================================================
# we allow building clutter with the internal copy of json-glib
# for platforms without it, but by default we depend on the
# system copy
m4_define([default_json], [system])
AC_ARG_WITH([json],
AC_HELP_STRING([--with-json=@<:@internal/check/system@:>@],
[Select the JSON-GLib copy to use @<:@default=default_json@:>@]),
[],
[with_json=default_json])
experimental_json=no
AS_CASE([$with_json],
[internal],
[
JSON_PREFIX=json
experimental_json=yes
have_json=no
],
[system],
[
AC_MSG_CHECKING([for installed JSON-GLib])
PKG_CHECK_EXISTS([json-glib-1.0 >= 0.10],
[have_json=yes],
[have_json=no])
AS_IF([test "x$have_json" = "xyes"],
[
JSON_PREFIX=json-glib
JSON_GLIB_PC="json-glib-1.0 >= 0.10"
AC_DEFINE([HAVE_JSON_GLIB],
[1],
[Have the JSON-GLib library installed])
AC_MSG_RESULT([found])
],
[AC_MSG_ERROR([not found])]
)
],
[check],
[
AC_MSG_CHECKING([for installed JSON-GLib])
PKG_CHECK_EXISTS([json-glib-1.0 >= 0.8],
[have_json=yes],
[have_json=no])
AS_IF([test "x$have_json" = "xyes"],
[
JSON_PREFIX=json-glib
JSON_GLIB_PC="json-glib-1.0 >= 0.8"
AC_DEFINE([HAVE_JSON_GLIB],
[1],
[Have the JSON-GLib library installed])
AC_MSG_RESULT([found])
],
[
JSON_PREFIX=json
experimental_json=yes
AC_MSG_RESULT([not found, using internal copy])
]
)
],
[AC_MSG_ERROR([Unknown argument for --with-json])]
)
AC_SUBST(JSON_PREFIX)
AM_CONDITIONAL(LOCAL_JSON_GLIB, test "x$have_json" = "xno")
dnl === Enable debug level ====================================================
m4_define([debug_default], [m4_if(m4_eval(clutter_minor_version % 2), [1], [yes], [minimum])])
@ -1020,15 +946,12 @@ AS_IF([test "x$enable_maintainer_flags" = "xyes" && test "x$GCC" = "xyes"],
AC_SUBST(MAINTAINER_CFLAGS)
dnl === Dependencies, compiler flags and linker libraries =====================
CLUTTER_REQUIRES="cairo >= 1.4 atk >= 1.7 pangocairo >= 1.20 $IMAGE_PC_FILES $BACKEND_PC_FILES $JSON_GLIB_PC"
CLUTTER_REQUIRES="cairo >= 1.8 atk >= 1.7 pangocairo >= 1.20 json-glib-1.0 >= 0.10 $IMAGE_PC_FILES $BACKEND_PC_FILES"
PKG_CHECK_MODULES(CLUTTER_DEPS, [$CLUTTER_REQUIRES])
AC_SUBST(CLUTTER_REQUIRES)
CLUTTER_CFLAGS="$FLAVOUR_CFLAGS $CLUTTER_DEPS_CFLAGS $CLUTTER_PROFILE_CFLAGS $GLIB_CFLAGS"
CLUTTER_LIBS="$FLAVOUR_LIBS $CLUTTER_DEPS_LIBS $CLUTTER_PROFILE_LDFLAGS $GLIB_LIBS"
AC_SUBST(CLUTTER_CFLAGS)
AC_SUBST(CLUTTER_LIBS)
@ -1117,7 +1040,6 @@ AC_CONFIG_FILES([
clutter/Makefile
clutter/clutter-version.h
clutter/clutter-json.h
clutter/clutter-$CLUTTER_API_VERSION.pc:clutter/clutter.pc.in
clutter/cally/cally-$CLUTTER_API_VERSION.pc:clutter/cally/cally.pc.in
@ -1193,12 +1115,6 @@ echo " • Extra:"
echo " Build introspection data: ${enable_introspection}"
echo " Build conformance test suite: ${enable_conformance}"
if test "x$have_json" = "xyes"; then
echo " JSON-GLib: system"
else
echo " JSON-GLib: internal (WARNING: Experimental)"
fi
# COGL flags
echo ""
echo " • COGL Backend:"
@ -1239,8 +1155,8 @@ echo ""
# General warning about experimental features
if test "x$experimental_backend" = "xyes" ||
test "x$experimental_image" = "xyes" ||
test "x$experimental_json" = "xyes"; then
test "x$experimental_image" = "xyes"
then
echo ""
echo "☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠☠"
echo " *WARNING*"

View file

@ -71,7 +71,6 @@ IGNORE_HFILES=\
clutter-deprecated.h \
clutter-enum-types.h \
clutter-id-pool.h \
clutter-json.h \
clutter-keysyms.h \
clutter-keysyms-compat.h \
clutter-keysyms-table.h \
@ -88,7 +87,6 @@ IGNORE_HFILES=\
egl \
fruity \
glx \
json \
osx \
x11 \
win32