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 |
JsonParser
provides an object for parsing a JSON data stream, either
inside a file or inside a static buffer.
Using JsonParser
The JsonParser
API is fairly simple:
``@c gboolean parse_json (const char *filename) { g_autoptr(JsonParser) parser = json_parser_new (); g_autoptr(GError) error = NULL
json_parser_load_from_file (parser, filename, &error); if (error != NULL) { g_critical ("Unable to parse '%s': %s", filename, error->message); return FALSE; }
g_autoptr(JsonNode) root = json_parser_get_root (parser);
// manipulate the object tree from the root node
return TRUE
}
`
By default, the entire process of loading the data and parsing it is
synchronous; the [method@Json.Parser.load_from_stream_async()] API will
load the data asynchronously, but parse it in the main context as the
signals of the parser must be emitted in the same thread. If you do
not use signals, and you wish to also parse the JSON data without blocking,
you should use a GTask
and the synchronous @JsonParser` API inside the
task itself.
Synopsis
- newtype Parser = Parser (ManagedPtr Parser)
- class (GObject o, IsDescendantOf Parser o) => IsParser o
- toParser :: (MonadIO m, IsParser o) => o -> m Parser
- parserGetCurrentLine :: (HasCallStack, MonadIO m, IsParser a) => a -> m Word32
- parserGetCurrentPos :: (HasCallStack, MonadIO m, IsParser a) => a -> m Word32
- parserGetRoot :: (HasCallStack, MonadIO m, IsParser a) => a -> m (Maybe Node)
- parserHasAssignment :: (HasCallStack, MonadIO m, IsParser a) => a -> m (Bool, Text)
- parserLoadFromData :: (HasCallStack, MonadIO m, IsParser a) => a -> Text -> Int64 -> m ()
- parserLoadFromFile :: (HasCallStack, MonadIO m, IsParser a) => a -> [Char] -> m ()
- parserLoadFromMappedFile :: (HasCallStack, MonadIO m, IsParser a) => a -> [Char] -> m ()
- parserLoadFromStream :: (HasCallStack, MonadIO m, IsParser a, IsInputStream b, IsCancellable c) => a -> b -> Maybe c -> m ()
- parserLoadFromStreamAsync :: (HasCallStack, MonadIO m, IsParser a, IsInputStream b, IsCancellable c) => a -> b -> Maybe c -> Maybe AsyncReadyCallback -> m ()
- parserLoadFromStreamFinish :: (HasCallStack, MonadIO m, IsParser a, IsAsyncResult b) => a -> b -> m ()
- parserNew :: (HasCallStack, MonadIO m) => m Parser
- parserNewImmutable :: (HasCallStack, MonadIO m) => m Parser
- parserStealRoot :: (HasCallStack, MonadIO m, IsParser a) => a -> m (Maybe Node)
- constructParserImmutable :: (IsParser o, MonadIO m) => Bool -> m (GValueConstruct o)
- getParserImmutable :: (MonadIO m, IsParser o) => o -> m Bool
- type ParserArrayElementCallback = Array -> Int32 -> IO ()
- afterParserArrayElement :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayElementCallback) -> m SignalHandlerId
- onParserArrayElement :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayElementCallback) -> m SignalHandlerId
- type ParserArrayEndCallback = Array -> IO ()
- afterParserArrayEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayEndCallback) -> m SignalHandlerId
- onParserArrayEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayEndCallback) -> m SignalHandlerId
- type ParserArrayStartCallback = IO ()
- afterParserArrayStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayStartCallback) -> m SignalHandlerId
- onParserArrayStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayStartCallback) -> m SignalHandlerId
- type ParserErrorCallback = Ptr () -> IO ()
- afterParserError :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserErrorCallback) -> m SignalHandlerId
- onParserError :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserErrorCallback) -> m SignalHandlerId
- type ParserObjectEndCallback = Object -> IO ()
- afterParserObjectEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectEndCallback) -> m SignalHandlerId
- onParserObjectEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectEndCallback) -> m SignalHandlerId
- type ParserObjectMemberCallback = Object -> Text -> IO ()
- afterParserObjectMember :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectMemberCallback) -> m SignalHandlerId
- onParserObjectMember :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectMemberCallback) -> m SignalHandlerId
- type ParserObjectStartCallback = IO ()
- afterParserObjectStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectStartCallback) -> m SignalHandlerId
- onParserObjectStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectStartCallback) -> m SignalHandlerId
- type ParserParseEndCallback = IO ()
- afterParserParseEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserParseEndCallback) -> m SignalHandlerId
- onParserParseEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserParseEndCallback) -> m SignalHandlerId
- type ParserParseStartCallback = IO ()
- afterParserParseStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserParseStartCallback) -> m SignalHandlerId
- onParserParseStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserParseStartCallback) -> m SignalHandlerId
Exported types
Memory-managed wrapper type.
Instances
Eq Parser Source # | |
GObject Parser Source # | |
Defined in GI.Json.Objects.Parser | |
ManagedPtrNewtype Parser Source # | |
Defined in GI.Json.Objects.Parser toManagedPtr :: Parser -> ManagedPtr Parser # | |
TypedObject Parser Source # | |
Defined in GI.Json.Objects.Parser | |
HasParentTypes Parser Source # | |
Defined in GI.Json.Objects.Parser | |
IsGValue (Maybe Parser) Source # | Convert |
Defined in GI.Json.Objects.Parser | |
type ParentTypes Parser Source # | |
Defined in GI.Json.Objects.Parser |
class (GObject o, IsDescendantOf Parser o) => IsParser o Source #
Instances
(GObject o, IsDescendantOf Parser o) => IsParser o Source # | |
Defined in GI.Json.Objects.Parser |
Methods
Click to display all available methods, including inherited ones
Methods
bindProperty, bindPropertyFull, forceFloating, freezeNotify, getv, hasAssignment, isFloating, loadFromData, loadFromFile, loadFromMappedFile, loadFromStream, loadFromStreamAsync, loadFromStreamFinish, notify, notifyByPspec, ref, refSink, runDispose, stealData, stealQdata, stealRoot, thawNotify, unref, watchClosure.
Getters
getCurrentLine, getCurrentPos, getData, getProperty, getQdata, getRoot.
Setters
getCurrentLine
:: (HasCallStack, MonadIO m, IsParser a) | |
=> a |
|
-> m Word32 | Returns: the currently parsed line, or 0. |
Retrieves the line currently parsed, starting from 1.
This function has defined behaviour only while parsing; calling this function from outside the signal handlers emitted by the parser will yield 0.
getCurrentPos
:: (HasCallStack, MonadIO m, IsParser a) | |
=> a |
|
-> m Word32 | Returns: the position in the current line, or 0. |
Retrieves the current position inside the current line, starting from 0.
This function has defined behaviour only while parsing; calling this function from outside the signal handlers emitted by the parser will yield 0.
getRoot
:: (HasCallStack, MonadIO m, IsParser a) | |
=> a |
|
-> m (Maybe Node) | Returns: the root node. |
Retrieves the top level node from the parsed JSON stream.
If the parser input was an empty string, or if parsing failed, the root
will be NULL
. It will also be NULL
if it has been stolen using
[methodjson
.Parser.steal_root].
hasAssignment
:: (HasCallStack, MonadIO m, IsParser a) | |
=> a |
|
-> m (Bool, Text) | Returns: |
A JSON data stream might sometimes contain an assignment, like:
``
var _json_data = { "member_name" : [ ...
`@
even though it would technically constitute a violation of the RFC.
JsonParser
will ignore the left hand identifier and parse the right
hand value of the assignment. @JsonParser` will record, though, the
existence of the assignment in the data stream and the variable name
used.
Since: 0.4
loadFromData
:: (HasCallStack, MonadIO m, IsParser a) | |
=> a |
|
-> Text |
|
-> Int64 |
|
-> m () | (Can throw |
Loads a JSON stream from a buffer and parses it.
You can call this function multiple times with the same parser, but the contents of the parser will be destroyed each time.
loadFromFile
:: (HasCallStack, MonadIO m, IsParser a) | |
=> a |
|
-> [Char] |
|
-> m () | (Can throw |
Loads a JSON stream from the content of filename
and parses it.
If the file is large or shared between processes,
[methodjson
.Parser.load_from_mapped_file] may be a more efficient
way to load it.
See also: [methodjson
.Parser.load_from_data]
loadFromMappedFile
parserLoadFromMappedFile Source #
:: (HasCallStack, MonadIO m, IsParser a) | |
=> a |
|
-> [Char] |
|
-> m () | (Can throw |
Loads a JSON stream from the content of filename
and parses it.
Unlike [methodjson
.Parser.load_from_file], filename
will be memory
mapped as read-only and parsed. filename
will be unmapped before this
function returns.
If mapping or reading the file fails, a G_FILE_ERROR
will be returned.
Since: 1.6
loadFromStream
:: (HasCallStack, MonadIO m, IsParser a, IsInputStream b, IsCancellable c) | |
=> a |
|
-> b |
|
-> Maybe c |
|
-> m () | (Can throw |
Loads the contents of an input stream and parses them.
If cancellable
is not NULL
, then the operation can be cancelled by
triggering the cancellable object from another thread. If the
operation was cancelled, G_IO_ERROR_CANCELLED
will be set
on the given error
.
Since: 0.12
loadFromStreamAsync
parserLoadFromStreamAsync Source #
:: (HasCallStack, MonadIO m, IsParser a, IsInputStream b, IsCancellable c) | |
=> a |
|
-> b |
|
-> Maybe c |
|
-> Maybe AsyncReadyCallback |
|
-> m () |
Asynchronously reads the contents of a stream.
For more details, see [methodjson
.Parser.load_from_stream], which is the
synchronous version of this call.
When the operation is finished, callback
will be called. You should
then call [methodjson
.Parser.load_from_stream_finish] to get the result
of the operation.
Since: 0.12
loadFromStreamFinish
parserLoadFromStreamFinish Source #
:: (HasCallStack, MonadIO m, IsParser a, IsAsyncResult b) | |
=> a |
|
-> b |
|
-> m () | (Can throw |
Finishes an asynchronous stream loading started with
[methodjson
.Parser.load_from_stream_async].
Since: 0.12
new
:: (HasCallStack, MonadIO m) | |
=> m Parser | Returns: the newly created parser |
Creates a new JSON parser.
You can use the JsonParser
to load a JSON stream from either a file or a
buffer and then walk the hierarchy using the data types API.
newImmutable
:: (HasCallStack, MonadIO m) | |
=> m Parser | Returns: the newly created parser |
Creates a new parser instance with its [propertyjson
.Parser:immutable]
property set to TRUE
to create immutable output trees.
Since: 1.2
stealRoot
:: (HasCallStack, MonadIO m, IsParser a) | |
=> a |
|
-> m (Maybe Node) | Returns: the root node |
Steals the top level node from the parsed JSON stream.
This will be NULL
in the same situations as [methodjson
.Parser.get_root]
return NULL
.
Since: 1.4
Properties
immutable
Whether the tree built by the parser 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
constructParserImmutable :: (IsParser 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
.
getParserImmutable :: (MonadIO m, IsParser o) => o -> m Bool Source #
Get the value of the “immutable
” property.
When overloading is enabled, this is equivalent to
get
parser #immutable
Signals
arrayElement
type ParserArrayElementCallback Source #
The ::array-element
signal is emitted each time a parser
has successfully parsed a single element of a JSON array.
afterParserArrayElement :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayElementCallback) -> m SignalHandlerId Source #
Connect a signal handler for the arrayElement signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #arrayElement callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserArrayElement :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayElementCallback) -> m SignalHandlerId Source #
Connect a signal handler for the arrayElement signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #arrayElement callback
arrayEnd
type ParserArrayEndCallback Source #
The ::array-end
signal is emitted each time a parser
has successfully parsed an entire JSON array.
afterParserArrayEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayEndCallback) -> m SignalHandlerId Source #
Connect a signal handler for the arrayEnd signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #arrayEnd callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserArrayEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayEndCallback) -> m SignalHandlerId Source #
Connect a signal handler for the arrayEnd signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #arrayEnd callback
arrayStart
type ParserArrayStartCallback = IO () Source #
The ::array-start
signal is emitted each time a parser
starts parsing a JSON array.
afterParserArrayStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayStartCallback) -> m SignalHandlerId Source #
Connect a signal handler for the arrayStart signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #arrayStart callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserArrayStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserArrayStartCallback) -> m SignalHandlerId Source #
Connect a signal handler for the arrayStart signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #arrayStart callback
error
type ParserErrorCallback Source #
The ::error
signal is emitted each time a parser encounters
an error in a JSON stream.
afterParserError :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserErrorCallback) -> m SignalHandlerId Source #
Connect a signal handler for the error signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #error callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserError :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserErrorCallback) -> m SignalHandlerId Source #
Connect a signal handler for the error signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #error callback
objectEnd
type ParserObjectEndCallback Source #
The ::object-end
signal is emitted each time a parser
has successfully parsed an entire JSON object.
afterParserObjectEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectEndCallback) -> m SignalHandlerId Source #
Connect a signal handler for the objectEnd signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #objectEnd callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserObjectEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectEndCallback) -> m SignalHandlerId Source #
Connect a signal handler for the objectEnd signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #objectEnd callback
objectMember
type ParserObjectMemberCallback Source #
= Object |
|
-> Text |
|
-> IO () |
The ::object-member
signal is emitted each time a parser
has successfully parsed a single member of a JSON object
afterParserObjectMember :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectMemberCallback) -> m SignalHandlerId Source #
Connect a signal handler for the objectMember signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #objectMember callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserObjectMember :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectMemberCallback) -> m SignalHandlerId Source #
Connect a signal handler for the objectMember signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #objectMember callback
objectStart
type ParserObjectStartCallback = IO () Source #
This signal is emitted each time a parser starts parsing a JSON object.
afterParserObjectStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectStartCallback) -> m SignalHandlerId Source #
Connect a signal handler for the objectStart signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #objectStart callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserObjectStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserObjectStartCallback) -> m SignalHandlerId Source #
Connect a signal handler for the objectStart signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #objectStart callback
parseEnd
type ParserParseEndCallback = IO () Source #
This signal is emitted when a parser successfully finished parsing a JSON data stream
afterParserParseEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserParseEndCallback) -> m SignalHandlerId Source #
Connect a signal handler for the parseEnd signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #parseEnd callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserParseEnd :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserParseEndCallback) -> m SignalHandlerId Source #
Connect a signal handler for the parseEnd signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #parseEnd callback
parseStart
type ParserParseStartCallback = IO () Source #
This signal is emitted when a parser starts parsing a JSON data stream.
afterParserParseStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserParseStartCallback) -> m SignalHandlerId Source #
Connect a signal handler for the parseStart signal, to be run after the default handler. When overloading is enabled, this is equivalent to
after
parser #parseStart callback
By default the object invoking the signal is not passed to the callback.
If you need to access it, you can use the implit ?self
parameter.
Note that this requires activating the ImplicitParams
GHC extension.
onParserParseStart :: (IsParser a, MonadIO m) => a -> ((?self :: a) => ParserParseStartCallback) -> m SignalHandlerId Source #
Connect a signal handler for the parseStart signal, to be run before the default handler. When overloading is enabled, this is equivalent to
on
parser #parseStart callback