dump() function

rapidjson.dump(obj, stream, *, skipkeys=False, ensure_ascii=True, write_mode=WM_COMPACT, indent=4, default=None, sort_keys=False, number_mode=None, datetime_mode=None, uuid_mode=None, bytes_mode=BM_UTF8, iterable_mode=IM_ANY_ITERABLE, mapping_mode=MM_ANY_MAPPING, chunk_size=65536, allow_nan=True)

Encode given Python obj instance into a JSON stream.

Parameters
  • obj – the value to be serialized

  • stream – a file-like instance

  • skipkeys (bool) – whether skip invalid dict keys

  • ensure_ascii (bool) – whether the output should contain only ASCII characters

  • write_mode (int) – enable particular pretty print behaviors

  • indent – indentation width or string to produce pretty printed JSON

  • default (callable) – a function that gets called for objects that can’t otherwise be serialized

  • sort_keys (bool) – whether dictionary keys should be sorted alphabetically

  • number_mode (int) – enable particular behaviors in handling numbers

  • datetime_mode (int) – how should datetime, time and date instances be handled

  • uuid_mode (int) – how should UUID instances be handled

  • bytes_mode (int) – how should bytes instances be handled

  • iterable_mode (int) – how should iterable values be handled

  • mapping_mode (int) – how should mapping values be handled

  • chunk_size (int) – write the stream in chunks of this size at a time

  • allow_nan (bool) – compatibility flag equivalent to number_mode=NM_NAN

The function has the same behaviour as dumps(), except that it requires an additional mandatory parameter, a file-like writable stream instance:

>>> stream = io.BytesIO()
>>> dump('bar', stream)
>>> stream.getvalue()
b'"bar"'

The target may also be a text stream1:

>>> stream = io.StringIO()
>>> dump(r'¯\_(ツ)_/¯', stream)
>>> stream.getvalue() == dumps(r'¯\_(ツ)_/¯')
True

chunk_size

The chunk_size argument determines the size of the buffer used to feed the stream: the greater the value, the fewer calls will be made to its write() method.

Consult the dumps() documentation for details on all other arguments.

1

A text stream is recognized by checking the presence of an encoding member attribute on the instance.