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
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
- 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 gvalueGType_ :: IO GType gvalueSet_ :: Ptr GValue -> Maybe Builder -> IO () gvalueGet_ :: Ptr GValue -> IO (Maybe Builder) | |
type ParentTypes Builder Source # | |
Defined in GI.Json.Objects.Builder type ParentTypes Builder = '[Object] |
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 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 #
:: (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 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
:: (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 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
:: (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 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 #
:: (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 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
:: (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 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
:: (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 builderAddValue
.
Once you added all elements to the array, you must call
builderEndArray
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 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
:: (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 builderBeginArray
.
This function cannot be called after builderSetMemberName
.
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 builderBeginObject
.
This function cannot be called after builderSetMemberName
.
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 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:
builderAddValue
, to add a scalar value to the memberbuilderBeginObject
, to add an object to the memberbuilderBeginArray
, 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