__all__ = ["Document", "ReaderFlags", "WriterFlags"]
import enum
from cyyjson import Document
[docs]class ReaderFlags(enum.IntFlag):
"""
Flags that can be passed into JSON reading functions to control parsing
behaviour.
"""
#: Stop when done instead of issues an error if there's additional content
#: after a JSON document. This option may be used to parse small pieces of
#: JSON in larger data, such as NDJSON.
STOP_WHEN_DONE = 0x02
#: Allow single trailing comma at the end of an object or array, such as
#: [1,2,3,] {"a":1,"b":2,}.
ALLOW_TRAILING_COMMAS = 0x04
#: Allow C-style single line and multiple line comments.
ALLOW_COMMENTS = 0x08
#: Allow inf/nan number and literal, case-insensitive, such as 1e999, NaN,
#: inf, -Infinity
ALLOW_INF_AND_NAN = 0x10
#: Read number as raw string. inf/nan
#: literal is also read as raw with `ALLOW_INF_AND_NAN` flag.
NUMBERS_AS_RAW = 0x20
[docs]class WriterFlags(enum.IntFlag):
"""
Flags that can be passed into JSON writing functions to control writing
behaviour.
"""
#: Write the JSON with 4-space indents and newlines.
PRETTY = 0x01
#: Write JSON pretty with 2 space indent. This flag will override
#: the PRETTY flag.
PRETTY_TWO_SPACES = 0x40
#: Escapes unicode as \uXXXXX so that all output is ASCII.
ESCAPE_UNICODE = 0x02
#: Escapes / as \/.
ESCAPE_SLASHES = 0x04
#: Writes Infinity and NaN.
ALLOW_INF_AND_NAN = 0x08
#: Writes Infinity and NaN as `null` instead of raising an error.
INF_AND_NAN_AS_NULL = 0x10
def load(
fp,
*,
cls=None,
object_hook=None,
parse_float=None,
parse_int=None,
parse_constant=None,
object_pairs_hook=None,
**kw
):
return Document(fp.read()).as_obj
def loads(
s,
*,
cls=None,
object_hook=None,
parse_float=None,
parse_int=None,
parse_constant=None,
object_pairs_hook=None,
**kw
):
return Document(s).as_obj
def dumps(
obj,
*,
skipkeys=False,
ensure_ascii=True,
check_circular=True,
allow_nan=True,
cls=None,
indent=None,
separators=None,
default=None,
sort_keys=False,
**kw
):
return Document(obj).dumps()
def dump(
obj,
fp,
*,
skipkeys=False,
ensure_ascii=True,
check_circular=True,
allow_nan=True,
cls=None,
indent=None,
separators=None,
default=None,
sort_keys=False,
**kw
):
fp.write(Document(obj).dumps())