Skip to content

Commit

Permalink
ujson: Document module.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensvalk committed Nov 29, 2022
1 parent 9608a16 commit 56ab509
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/main/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ on your hub. Check `pybricks.com/install`_ to learn how.
micropython/micropython
micropython/uerrno
micropython/uio
micropython/ujson
micropython/umath
micropython/urandom
micropython/uselect
Expand Down
13 changes: 13 additions & 0 deletions doc/main/micropython/ujson.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
:mod:`ujson` -- JSON encoding and decoding
===========================================

.. automodule:: ujson
:no-members:

.. autofunction:: ujson.dump

.. autofunction:: ujson.dumps

.. autofunction:: ujson.load

.. autofunction:: ujson.loads
81 changes: 81 additions & 0 deletions src/ujson/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2022 The Pybricks Authors
#
# Documentation adapted from:
# https://raw.githubusercontent.com/micropython/micropython/master/docs/library/json.rst
# Copyright (c) 2014-2021, Damien P. George, Paul Sokolovsky, and contributors

"""
Convert between Python objects and the JSON data format.
"""

from typing import IO, Any, Tuple

import json

json.dump


def dump(object: Any, stream: IO, separators: Tuple[str, str] = (", ", ": ")):
"""
dump(object, stream, separators=(", ", ": "))
Serializes an object to a JSON string and write it to a stream.
Arguments:
obj: Object to serialize.
stream: Stream to write the output to.
separators (tuple): An ``(item_separator, key_separator)`` tuple to
specify how elements should be separated.
"""


def dumps(object: Any, separators: Tuple[str, str] = (", ", ": ")) -> str:
"""
dumps(object, separators=(", ", ": "))
Serializes an object to JSON and return it as a string
Arguments:
obj: Object to serialize.
separators (tuple): An ``(item_separator, key_separator)`` tuple to
specify how elements should be separated.
Return:
The JSON string.
"""


def load(stream: IO) -> Any:
"""
load(stream)
Parses the stream to interpret and deserialize the JSON data to a
MicroPython object.
Parsing continues until end-of-file is encountered. A ``ValueError`` is
raised if the data in stream is not correctly formed.
Arguments:
stream: Stream from which to read the JSON string.
Returns:
The deserialized MicroPython object.
"""


def loads(string) -> Any:
"""
loads(string)
Parses the string to interpret and deserialize the JSON data to a
MicroPython object.
A ``ValueError`` is raised if the string is not correctly formed.
Arguments:
string (str): JSON string to decode.
Returns:
The deserialized MicroPython object.
"""

0 comments on commit 56ab509

Please sign in to comment.