View Source jsonpull (jsonpull v0.1.1)

These functions both read and transform JSON data into Erlang types.

The main reading functions return {ok, {Value, Rest}} or {error, Atom}.

They sit a level above the jsonpull_read module, so they also skip whitespace.

Summary

Functions

Try to read an array from the JSON. If it's there, the atom 'begin_array' will be returned.

Try to read a boolean from the JSON. If it's there, it will be returned as the atom 'true' or 'false'.

Try to read the next array element from the JSON. If there is an element, it will be represented as the atom 'element'

Try to read a string from the JSON as an already existing atom. This function will not parse escape sequences.

See also: string/1.

Try to read a float from the JSON. This function will discriminate against integers and only return floats.

See also: integer/1, number/1.

Try to read an integer from the JSON. This function will discriminate against floats and only return integers.

See also: float/1, number/1.

Try to read a string from the JSON as an iolist. This is like reading a string, except escape sequences are handled differently.

Try to read the next object key from the JSON. If there is a key, it will be returned as a binary string.

Try to read a null from the JSON. If it's there, it will be returned as the atom 'null'.
Try to read a number from the JSON. If it's there, it will be returned as the original binary string version.

See also: float/1, integer/1.

Try to read an object from the JSON. If there is an object, the atom 'begin_object' will be returned.

Skips over the next value in the provided JSON binary.
Try to read a string from the JSON. If it's there, it will be returned as a binary string with all escape sequences processed.

See also: iolist/1.

Strips the quotes from a raw JSON string.

See also: unescape_and_strip/1.

Turn a raw JSON string into a properly translated Erlang iolist.

Functions

-spec array(JSON :: binary()) -> {ok, {begin_array, Rest :: binary()}} | {error, not_array}.

Try to read an array from the JSON. If it's there, the atom 'begin_array' will be returned.

Structured types are handled differently in pull parsers. For more info, read the Overview.
-spec boolean(JSON :: binary()) -> {ok, {Value :: boolean(), Rest :: binary()}} | {error, not_boolean}.
Try to read a boolean from the JSON. If it's there, it will be returned as the atom 'true' or 'false'.
-spec element(JSON :: binary()) ->
           {ok, {element, Rest :: binary()}} | {ok, {end_array, Rest :: binary()}}.

Try to read the next array element from the JSON. If there is an element, it will be represented as the atom 'element'

If the array is ending, the atom 'end_array' will be returned.

Structured types are handled differently in pull parsers. For more info, read the Overview.
-spec existing_atom(JSON :: binary()) ->
                 {ok, {Value :: atom(), Rest :: binary()}} |
                 {error, not_string | unterminated_string | not_existing_atom}.
Try to read a string from the JSON as an already existing atom. This function will not parse escape sequences.

See also: string/1.

-spec float(JSON :: binary()) -> {ok, {Value :: float(), Rest :: binary()}} | {error, not_float}.
Try to read a float from the JSON. This function will discriminate against integers and only return floats.

See also: integer/1, number/1.

-spec integer(JSON :: binary()) -> {ok, {Value :: integer(), Rest :: binary()}} | {error, not_integer}.
Try to read an integer from the JSON. This function will discriminate against floats and only return integers.

See also: float/1, number/1.

-spec iolist(JSON :: binary()) ->
          {ok, {Value :: iolist(), Rest :: binary()}} |
          {error, not_string | unterminated_string | invalid_escape_sequence}.

Try to read a string from the JSON as an iolist. This is like reading a string, except escape sequences are handled differently.

This may be more efficient in some cases.

See also: string/1.

-spec key(JSON :: binary()) ->
       {ok, {Key :: binary(), Rest :: binary()}} |
       {ok, {end_object, Rest :: binary()}} |
       {error,
        missing_colon_after_key | unterminated_key | key_not_string | unterminated_string |
        invalid_escape_sequence}.

Try to read the next object key from the JSON. If there is a key, it will be returned as a binary string.

If the object is ending, the atom 'end_object' will be returned.

Structured types are handled differently in pull parsers. For more info, read the Overview.
-spec null(JSON :: binary()) -> {ok, {Value :: null, Rest :: binary()}} | {error, not_null}.
Try to read a null from the JSON. If it's there, it will be returned as the atom 'null'.
-spec number(JSON :: binary()) -> {ok, {Value :: binary(), Rest :: binary()}} | {error, not_number}.
Try to read a number from the JSON. If it's there, it will be returned as the original binary string version.

See also: float/1, integer/1.

-spec object(JSON :: binary()) -> {ok, {begin_object, Rest :: binary()}} | {error, not_object}.

Try to read an object from the JSON. If there is an object, the atom 'begin_object' will be returned.

Structured types are handled differently in pull parsers. For more info, read the Overview.
-spec skip_value(JSONIn :: binary()) -> JSONOut :: binary() | {error, atom()}.
Skips over the next value in the provided JSON binary.
-spec string(JSON :: binary()) ->
          {ok, {Value :: binary(), Rest :: binary()}} |
          {error, not_string | unterminated_string | invalid_escape_sequence}.
Try to read a string from the JSON. If it's there, it will be returned as a binary string with all escape sequences processed.

See also: iolist/1.

Strips the quotes from a raw JSON string.

See also: unescape_and_strip/1.

Link to this function

unescape_and_strip(String)

View Source
-spec unescape_and_strip(String :: binary()) ->
                      {ok, iolist()} | unterminated_string | invalid_escape_sequence.

Turn a raw JSON string into a properly translated Erlang iolist.

Note: You may need to use unicode:characters_to_binary/list afterwards if you don't want an iolist.

Using iolist_to_binary will only work if there are no weird unicode escape sequences!