ujson: Document module.

This commit is contained in:
Laurens Valk
2022-11-29 16:00:00 +01:00
parent 9608a16d95
commit 56ab509630
3 changed files with 95 additions and 0 deletions
+1
View File
@@ -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
+13
View File
@@ -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
View File
@@ -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.
"""