Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/orjsonl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,33 @@
append,
extend,
)
from orjson import (
OPT_NAIVE_UTC,
OPT_NON_STR_KEYS,
OPT_OMIT_MICROSECONDS,
OPT_PASSTHROUGH_DATACLASS,
OPT_PASSTHROUGH_DATETIME,
OPT_PASSTHROUGH_SUBCLASS,
OPT_SERIALIZE_NUMPY,
OPT_SORT_KEYS,
OPT_STRICT_INTEGER,
OPT_UTC_Z,
)

__all__ = (
stream,
load,
save,
append,
extend,
OPT_NAIVE_UTC,
OPT_NON_STR_KEYS,
OPT_OMIT_MICROSECONDS,
OPT_PASSTHROUGH_DATACLASS,
OPT_PASSTHROUGH_DATETIME,
OPT_PASSTHROUGH_SUBCLASS,
OPT_SERIALIZE_NUMPY,
OPT_SORT_KEYS,
OPT_STRICT_INTEGER,
OPT_UTC_Z,
)
9 changes: 6 additions & 3 deletions src/orjsonl/orjsonl.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def save(
compression_threads (int, optional): An optional integer passed to xopen.xopen() as the 'threads' argument that specifies the number of threads that should be used for compression. Defaults to None.
compression_format (str, optional): An optional string passed to xopen.xopen() as the 'format' argument that overrides the autodetection of the file's compression format based on its extension. Possible values are 'gz', 'xz', 'bz2' and 'zst'. Defaults to None."""

option &= ~orjson.OPT_INDENT_2
option |= orjson.OPT_APPEND_NEWLINE
with xopen(path, 'wb', compresslevel=compression_level, threads=compression_threads, format=compression_format) as writer:
for item in data:
writer.write(orjson.dumps(item, default=default, option=option))
writer.write(b'\n')


def append(
Expand All @@ -99,12 +100,13 @@ def append(
compression_threads (int, optional): An optional integer passed to xopen.xopen() as the 'threads' argument that specifies the number of threads that should be used for compression. Defaults to None.
compression_format (str, optional): An optional string passed to xopen.xopen() as the 'format' argument that overrides the autodetection of the file's compression format based on its extension or content. Possible values are 'gz', 'xz', 'bz2' and 'zst'. Defaults to None."""

option &= ~orjson.OPT_INDENT_2
option |= orjson.OPT_APPEND_NEWLINE
with xopen(path, 'ab', compresslevel=compression_level, threads=compression_threads, format=compression_format) as writer:
if not newline:
writer.write(b'\n')

writer.write(orjson.dumps(data, default=default, option=option))
writer.write(b'\n')


def extend(
Expand All @@ -129,10 +131,11 @@ def extend(
compression_threads (int, optional): An optional integer passed to xopen.xopen() as the 'threads' argument that specifies the number of threads that should be used for compression. Defaults to None.
compression_format (str, optional): An optional string passed to xopen.xopen() as the 'format' argument that overrides the autodetection of the file's compression format based on its extension or content. Possible values are 'gz', 'xz', 'bz2' and 'zst'. Defaults to None."""

option &= ~orjson.OPT_INDENT_2
option |= orjson.OPT_APPEND_NEWLINE
with xopen(path, 'ab', compresslevel=compression_level, threads=compression_threads, format=compression_format) as writer:
if not newline:
writer.write(b'\n')

for item in data:
writer.write(orjson.dumps(item, default=default, option=option))
writer.write(b'\n')