View Source JsonStreamEncoder (json_stream_encoder v0.2.0)

Primary API for encoding streaming JSON documents.

Link to this section Summary

Functions

Encode an array into the stream, using the contents of the function to set elements.

End an array you previously started encoding with ary_start/1.

Start encoding an array. Always pair with ary_end/1.

Encode a key for an object.

Encode a key-value pair for an object.

Create a new streamer without indentation.

Create a new streamer with the given indentation settings.

Encode an object into the stream, using the contents of the function to set key/value pairs.

End an object you previously started encoding with obj_start/1.

Start encoding an object. Always pair with obj_end/1.

Encode a value.

Link to this section Types

Specs

encodeable() :: Jason.Encoder.t()

Specs

indentation() :: boolean() | binary()

Specs

io_stream() :: IO.device()

Specs

key() :: atom() | binary() | number() | nonempty_charlist() | boolean()

Specs

streamer()

Link to this section Functions

Specs

ary(streamer(), (streamer() -> streamer())) :: streamer()

Encode an array into the stream, using the contents of the function to set elements.

examples

Examples

ary(streamer, fn(stream) ->
  stream |> val(1) |> val("2")
end)
#=> [1,"2"]

Specs

ary_end(streamer()) :: streamer()

End an array you previously started encoding with ary_start/1.

Specs

ary_start(streamer()) :: streamer()

Start encoding an array. Always pair with ary_end/1.

More often you will want to use ary/2.

examples

Examples

streamer |> ary_start() |> val(1) |> val("2") |> ary_end()
#=> [1,"2"]

Specs

key(streamer(), key()) :: streamer()

Encode a key for an object.

Always follow this with a val/2, obj/2, or ary/2 - otherwise your JSON won't make any sense.

Specs

kv(streamer(), key(), encodeable()) :: streamer()

Encode a key-value pair for an object.

You should really only use this if your value is simple, otherwise use key/2 in combination with other functions.

Specs

new(io_stream()) :: streamer()

Create a new streamer without indentation.

Link to this function

new(io_stream, indent_value)

View Source

Specs

new(io_stream(), indentation()) :: streamer()

Create a new streamer with the given indentation settings.

Pass false for no indentation, true for default indentation (two spaces), or a binary representing the desired indentation string.

Specs

obj(streamer(), (streamer() -> streamer())) :: streamer()

Encode an object into the stream, using the contents of the function to set key/value pairs.

examples

Examples

obj(streamer, fn(stream) ->
  stream |> kv(1,"2")
end)
#=> {"1": "2"}

Specs

obj_end(streamer()) :: streamer()

End an object you previously started encoding with obj_start/1.

Specs

obj_start(streamer()) :: streamer()

Start encoding an object. Always pair with obj_end/1.

More often you will want to use obj/2.

examples

Examples

streamer |> obj_start() |> kv(1,"2") |> obj_end()
#=> {"1": "2"}

Specs

val(streamer(), encodeable()) :: streamer()

Encode a value.

Usable anywhere you would expect a JSON value:

  • The root of a document
  • After a key
  • As an array element