gi-json-1.0.5: JSON GObject bindings
CopyrightWill Thompson and Iñaki García Etxebarria
LicenseLGPL-2.1
MaintainerIñaki García Etxebarria
Safe HaskellSafe-Inferred
LanguageHaskell2010

GI.Json.Objects.Builder

Description

JsonBuilder provides an object for generating a JSON tree.

The root of the JSON tree can be either a [structjson.Object] or a [structjson.Array]. Thus the first call must necessarily be either builderBeginObject or builderBeginArray.

For convenience to language bindings, most JsonBuilder method return the instance, making it easy to chain function calls.

Using JsonBuilder

c code

g_autoptr(JsonBuilder) builder = json_builder_new ();

json_builder_begin_object (builder);

json_builder_set_member_name (builder, "url");
json_builder_add_string_value (builder, "http://www.gnome.org/img/flash/two-thirty.png");

json_builder_set_member_name (builder, "size");
json_builder_begin_array (builder);
json_builder_add_int_value (builder, 652);
json_builder_add_int_value (builder, 242);
json_builder_end_array (builder);

json_builder_end_object (builder);

g_autoptr(JsonNode) root root = json_builder_get_root (builder);

g_autoptr(JsonGenerator) gen = json_generator_new ();
json_generator_set_root (gen, root);
g_autofree char *str = json_generator_to_data (gen, NULL);

// str now contains the following JSON data
// { "url" : "http://www.gnome.org/img/flash/two-thirty.png", "size" : [ 652, 242 ] }
Synopsis

Exported types

newtype Builder Source #

Memory-managed wrapper type.

Constructors

Builder (ManagedPtr Builder) 

Instances

Instances details
Eq Builder Source # 
Instance details

Defined in GI.Json.Objects.Builder

Methods

(==) :: Builder -> Builder -> Bool #

(/=) :: Builder -> Builder -> Bool #

GObject Builder Source # 
Instance details

Defined in GI.Json.Objects.Builder

ManagedPtrNewtype Builder Source # 
Instance details

Defined in GI.Json.Objects.Builder

Methods

toManagedPtr :: Builder -> ManagedPtr Builder

TypedObject Builder Source # 
Instance details

Defined in GI.Json.Objects.Builder

Methods

glibType :: IO GType

HasParentTypes Builder Source # 
Instance details

Defined in GI.Json.Objects.Builder

IsGValue (Maybe Builder) Source #

Convert Builder to and from GValue. See toGValue and fromGValue.

Instance details

Defined in GI.Json.Objects.Builder

Methods

gvalueGType_ :: IO GType

gvalueSet_ :: Ptr GValue -> Maybe Builder -> IO ()

gvalueGet_ :: Ptr GValue -> IO (Maybe Builder)

type ParentTypes Builder Source # 
Instance details

Defined in GI.Json.Objects.Builder

type ParentTypes Builder = '[Object]

class (GObject o, IsDescendantOf Builder o) => IsBuilder o Source #

Type class for types which can be safely cast to Builder, for instance with toBuilder.

Instances

Instances details
(GObject o, IsDescendantOf Builder o) => IsBuilder o Source # 
Instance details

Defined in GI.Json.Objects.Builder

toBuilder :: (MonadIO m, IsBuilder o) => o -> m Builder Source #

Cast to Builder, for types for which this is known to be safe. For general casts, use castTo.

Methods

addBooleanValue

builderAddBooleanValue Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> Bool

value: the value of the member or element

-> m (Maybe Builder)

Returns: the builder instance

Adds a boolean value to the currently open object member or array.

If called after builderSetMemberName, sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: builderAddValue

addDoubleValue

builderAddDoubleValue Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> Double

value: the value of the member or element

-> m (Maybe Builder)

Returns: the builder instance

Adds a floating point value to the currently open object member or array.

If called after builderSetMemberName, sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: builderAddValue

addIntValue

builderAddIntValue Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> Int64

value: the value of the member or element

-> m (Maybe Builder)

Returns: the builder instance

Adds an integer value to the currently open object member or array.

If called after builderSetMemberName, sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: builderAddValue

addNullValue

builderAddNullValue Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> m (Maybe Builder)

Returns: the builder instance

Adds a null value to the currently open object member or array.

If called after builderSetMemberName, sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: builderAddValue

addStringValue

builderAddStringValue Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> Text

value: the value of the member or element

-> m (Maybe Builder)

Returns: the builder instance

Adds a boolean value to the currently open object member or array.

If called after builderSetMemberName, sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: builderAddValue

addValue

builderAddValue Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> Node

node: the value of the member or element

-> m (Maybe Builder)

Returns: the builder instance

Adds a value to the currently open object member or array.

If called after builderSetMemberName, sets the given node as the value of the current member in the open object; otherwise, the node is appended to the elements of the open array.

The builder will take ownership of the node.

beginArray

builderBeginArray Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> m (Maybe Builder)

Returns: the builder instance

Opens an array inside the given builder.

You can add a new element to the array by using builderAddValue.

Once you added all elements to the array, you must call builderEndArray to close the array.

beginObject

builderBeginObject Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> m (Maybe Builder)

Returns: the builder instance

Opens an object inside the given builder.

You can add a new member to the object by using builderSetMemberName, followed by builderAddValue.

Once you added all members to the object, you must call builderEndObject to close the object.

If the builder is in an inconsistent state, this function will return NULL.

endArray

builderEndArray Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> m (Maybe Builder)

Returns: the builder instance

Closes the array inside the given builder that was opened by the most recent call to builderBeginArray.

This function cannot be called after builderSetMemberName.

endObject

builderEndObject Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> m (Maybe Builder)

Returns: the builder instance

Closes the object inside the given builder that was opened by the most recent call to builderBeginObject.

This function cannot be called after builderSetMemberName.

getRoot

builderGetRoot Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> m (Maybe Node)

Returns: the root node

Returns the root of the currently constructed tree.

if the build is incomplete (ie: if there are any opened objects, or any open object members and array elements) then this function will return NULL.

new

builderNew Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> m Builder

Returns: the newly created builder instance

Creates a new JsonBuilder.

You can use this object to generate a JSON tree and obtain the root node.

newImmutable

builderNewImmutable Source #

Arguments

:: (HasCallStack, MonadIO m) 
=> m Builder

Returns: the newly create builder instance

Creates a new, immutable JsonBuilder instance.

It is equivalent to setting the Builder:immutable property set to TRUE at construction time.

Since: 1.2

reset

builderReset Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> m () 

Resets the state of the builder back to its initial state.

setMemberName

builderSetMemberName Source #

Arguments

:: (HasCallStack, MonadIO m, IsBuilder a) 
=> a

builder: a builder

-> Text

memberName: the name of the member

-> m (Maybe Builder)

Returns: the builder instance

Sets the name of the member in an object.

This function must be followed by of these functions:

This function can only be called within an open object.

Properties

immutable

Whether the tree should be immutable when created.

Making the output immutable on creation avoids the expense of traversing it to make it immutable later.

Since: 1.2

constructBuilderImmutable :: (IsBuilder o, MonadIO m) => Bool -> m (GValueConstruct o) Source #

Construct a GValueConstruct with valid value for the “immutable” property. This is rarely needed directly, but it is used by new.

getBuilderImmutable :: (MonadIO m, IsBuilder o) => o -> m Bool Source #

Get the value of the “immutable” property. When overloading is enabled, this is equivalent to

get builder #immutable