Copyright | Will Thompson and Iñaki García Etxebarria |
---|---|
License | LGPL-2.1 |
Maintainer | Iñaki García Etxebarria |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
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
[methodjson
.Builder.begin_object] or [methodjson
.Builder.begin_array].
For convenience to language bindings, most JsonBuilder
method return the
instance, making it easy to chain function calls.
Using JsonBuilder
``@c 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
- newtype Builder = Builder (ManagedPtr Builder)
- class (GObject o, IsDescendantOf Builder o) => IsBuilder o
- toBuilder :: (MonadIO m, IsBuilder o) => o -> m Builder
- builderAddBooleanValue :: (HasCallStack, MonadIO m, IsBuilder a) => a -> Bool -> m (Maybe Builder)
- builderAddDoubleValue :: (HasCallStack, MonadIO m, IsBuilder a) => a -> Double -> m (Maybe Builder)
- builderAddIntValue :: (HasCallStack, MonadIO m, IsBuilder a) => a -> Int64 -> m (Maybe Builder)
- builderAddNullValue :: (HasCallStack, MonadIO m, IsBuilder a) => a -> m (Maybe Builder)
- builderAddStringValue :: (HasCallStack, MonadIO m, IsBuilder a) => a -> Text -> m (Maybe Builder)
- builderAddValue :: (HasCallStack, MonadIO m, IsBuilder a) => a -> Node -> m (Maybe Builder)
- builderBeginArray :: (HasCallStack, MonadIO m, IsBuilder a) => a -> m (Maybe Builder)
- builderBeginObject :: (HasCallStack, MonadIO m, IsBuilder a) => a -> m (Maybe Builder)
- builderEndArray :: (HasCallStack, MonadIO m, IsBuilder a) => a -> m (Maybe Builder)
- builderEndObject :: (HasCallStack, MonadIO m, IsBuilder a) => a -> m (Maybe Builder)
- builderGetRoot :: (HasCallStack, MonadIO m, IsBuilder a) => a -> m (Maybe Node)
- builderNew :: (HasCallStack, MonadIO m) => m Builder
- builderNewImmutable :: (HasCallStack, MonadIO m) => m Builder
- builderReset :: (HasCallStack, MonadIO m, IsBuilder a) => a -> m ()
- builderSetMemberName :: (HasCallStack, MonadIO m, IsBuilder a) => a -> Text -> m (Maybe Builder)
- constructBuilderImmutable :: (IsBuilder o, MonadIO m) => Bool -> m (GValueConstruct o)
- getBuilderImmutable :: (MonadIO m, IsBuilder o) => o -> m Bool
Exported types
Memory-managed wrapper type.
Instances
Eq Builder Source # | |
GObject Builder Source # | |
Defined in GI.Json.Objects.Builder | |
ManagedPtrNewtype Builder Source # | |
Defined in GI.Json.Objects.Builder toManagedPtr :: Builder -> ManagedPtr Builder # | |
TypedObject Builder Source # | |
Defined in GI.Json.Objects.Builder | |
HasParentTypes Builder Source # | |
Defined in GI.Json.Objects.Builder | |
IsGValue (Maybe Builder) Source # | Convert |
Defined in GI.Json.Objects.Builder | |
type ParentTypes Builder Source # | |
Defined in GI.Json.Objects.Builder |
class (GObject o, IsDescendantOf Builder o) => IsBuilder o Source #
Instances
(GObject o, IsDescendantOf Builder o) => IsBuilder o Source # | |
Defined in GI.Json.Objects.Builder |
Methods
Click to display all available methods, including inherited ones
Methods
addBooleanValue, addDoubleValue, addIntValue, addNullValue, addStringValue, addValue, beginArray, beginObject, bindProperty, bindPropertyFull, endArray, endObject, forceFloating, freezeNotify, getv, isFloating, notify, notifyByPspec, ref, refSink, reset, runDispose, stealData, stealQdata, thawNotify, unref, watchClosure.
Getters
getData, getProperty, getQdata, getRoot.
Setters
addBooleanValue
builderAddBooleanValue Source #
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> Bool |
|
-> m (Maybe Builder) | Returns: the builder instance |
Adds a boolean value to the currently open object member or array.
If called after [methodjson
.Builder.set_member_name], 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: [methodjson
.Builder.add_value]
addDoubleValue
builderAddDoubleValue Source #
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> Double |
|
-> m (Maybe Builder) | Returns: the builder instance |
Adds a floating point value to the currently open object member or array.
If called after [methodjson
.Builder.set_member_name], 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: [methodjson
.Builder.add_value]
addIntValue
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> Int64 |
|
-> m (Maybe Builder) | Returns: the builder instance |
Adds an integer value to the currently open object member or array.
If called after [methodjson
.Builder.set_member_name], 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: [methodjson
.Builder.add_value]
addNullValue
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> m (Maybe Builder) | Returns: the builder instance |
Adds a null value to the currently open object member or array.
If called after [methodjson
.Builder.set_member_name], 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: [methodjson
.Builder.add_value]
addStringValue
builderAddStringValue Source #
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> Text |
|
-> m (Maybe Builder) | Returns: the builder instance |
Adds a boolean value to the currently open object member or array.
If called after [methodjson
.Builder.set_member_name], 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: [methodjson
.Builder.add_value]
addValue
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> Node |
|
-> m (Maybe Builder) | Returns: the builder instance |
Adds a value to the currently open object member or array.
If called after [methodjson
.Builder.set_member_name], 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
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> 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 [methodjson
.Builder.add_value].
Once you added all elements to the array, you must call
[methodjson
.Builder.end_array] to close the array.
beginObject
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> 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 [methodjson
.Builder.set_member_name],
followed by [methodjson
.Builder.add_value].
Once you added all members to the object, you must call [methodjson
.Builder.end_object]
to close the object.
If the builder is in an inconsistent state, this function will return NULL
.
endArray
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> m (Maybe Builder) | Returns: the builder instance |
Closes the array inside the given builder that was opened by the most
recent call to [methodjson
.Builder.begin_array].
This function cannot be called after [methodjson
.Builder.set_member_name].
endObject
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> m (Maybe Builder) | Returns: the builder instance |
Closes the object inside the given builder that was opened by the most
recent call to [methodjson
.Builder.begin_object].
This function cannot be called after [methodjson
.Builder.set_member_name].
getRoot
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> 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
:: (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
:: (HasCallStack, MonadIO m) | |
=> m Builder | Returns: the newly create builder instance |
Creates a new, immutable JsonBuilder
instance.
It is equivalent to setting the [propertyjson
.Builder:immutable] property
set to TRUE
at construction time.
Since: 1.2
reset
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> m () |
Resets the state of the builder back to its initial state.
setMemberName
:: (HasCallStack, MonadIO m, IsBuilder a) | |
=> a |
|
-> Text |
|
-> 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:
- [method
json
.Builder.add_value], to add a scalar value to the member - [method
json
.Builder.begin_object], to add an object to the member - [method
json
.Builder.begin_array], to add an array to the member
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