Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

This library is part of Hat Open project - open-source framework of tools and libraries for developing applications used for remote monitoring, control and management of intelligent electronic devices such as IoT devices, PLCs, industrial automation or home automation systems.

Development of Hat Open and associated repositories is sponsored by Končar - Power Plant and Electric Traction Engineering Inc. (Končar KET - https://www.koncar-ket.hr).

For more information see:

About

JSON data manipulation and validation functions.

  • type definitions:

    Array: typing.Type = typing.List['Data']
    Object: typing.Type = typing.Dict[str, 'Data']
    Data: typing.Type = typing.Union[None, bool, int, float, str, Array, Object]
    """JSON data type identifier."""
    
    Format = enum.Enum('Format', ['JSON', 'YAML'])
    """Encoding format"""
    
    Path: typing.Type = typing.Union[int, str, typing.List['Path']]
    """Data path"""
  • hat.json.equals and hat.json.flatten:

    def equals(a: Data,
               b: Data
               ) -> bool:
        """Equality comparison of json serializable data.
    
        Tests for equality of data according to JSON format. Notably, ``bool``
        values are not considered equal to numeric values in any case. This is
        different from default equality comparison, which considers `False`
        equal to `0` and `0.0`; and `True` equal to `1` and `1.0`.
    
        Example::
    
            assert equals(0, 0.0) is True
            assert equals({'a': 1, 'b': 2}, {'b': 2, 'a': 1}) is True
            assert equals(1, True) is False
    
        """
    
    def flatten(data: Data
                ) -> typing.Iterable[Data]:
        """Flatten JSON data
    
        If `data` is array, this generator recursively yields result of
        `flatten` call with each element of input list. For other `Data` types,
        input data is yielded.
    
        Example::
    
            data = [1, [], [2], {'a': [3]}]
            result = [1, 2, {'a': [3]}]
            assert list(flatten(data)) == result
    
        """
  • hat.json.get and hat.json.set_:

    def get(data: Data,
            path: Path
            ) -> Data:
        """Get data element referenced by path
    
        Example::
    
            data = {'a': [1, 2, [3, 4]]}
            path = ['a', 2, 0]
            assert get(data, path) == 3
    
            data = [1, 2, 3]
            assert get(data, 0) == 1
            assert get(data, 5) is None
    
        """
    
    def set_(data: Data,
             path: Path,
             value: Data
             ) -> Data:
        """Create new data by setting data path element value
    
        Example::
    
            data = [1, {'a': 2, 'b': 3}, 4]
            path = [1, 'b']
            result = set_(data, path, 5)
            assert result == [1, {'a': 2, 'b': 5}, 4]
            assert result is not data
    
            data = [1, 2, 3]
            result = set_(data, 4, 4)
            assert result == [1, 2, 3, None, 4]
    
        """
  • hat.json.diff and hat.json.patch:

    def diff(src: Data,
             dst: Data
             ) -> Data:
        """Generate JSON Patch diff.
    
        Example::
    
            src = [1, {'a': 2}, 3]
            dst = [1, {'a': 4}, 3]
            result = diff(src, dst)
            assert result == [{'op': 'replace', 'path': '/1/a', 'value': 4}]
    
        """
    
    def patch(data: Data,
              diff: Data
              ) -> Data:
        """Apply JSON Patch diff.
    
        Example::
    
            data = [1, {'a': 2}, 3]
            d = [{'op': 'replace', 'path': '/1/a', 'value': 4}]
            result = patch(data, d)
            assert result == [1, {'a': 4}, 3]
    
        """
  • hat.json.encode and hat.json.decode:

    def encode(data: Data,
               format: Format = Format.JSON,
               indent: typing.Optional[int] = None
               ) -> str:
        """Encode JSON data.
    
        Args:
            data: JSON data
            format: encoding format
            indent: indentation size
    
        """
    
    def decode(data_str: str,
               format: Format = Format.JSON
               ) -> Data:
        """Decode JSON data.
    
        Args:
            data_str: encoded JSON data
            format: encoding format
    
        """
  • hat.json.encode_file and hat.json.decode_file:

    def encode_file(data: Data,
                    path: pathlib.PurePath,
                    format: typing.Optional[Format] = None,
                    indent: typing.Optional[int] = 4):
        """Encode JSON data to file.
    
        If `format` is ``None``, encoding format is derived from path suffix.
    
        Args:
            data: JSON data
            path: file path
            format: encoding format
            indent: indentation size
    
        """
    
    def decode_file(path: pathlib.PurePath,
                    format: typing.Optional[Format] = None
                    ) -> Data:
        """Decode JSON data from file.
    
        If `format` is ``None``, encoding format is derived from path suffix.
    
        Args:
            path: file path
            format: encoding format
    
        """
  • hat.json.SchemaRepository:

    class SchemaRepository:
        """JSON Schema repository.
    
        A repository that holds json schemas and enables validation against
        them.
    
        Repository can be initialized with multiple arguments, which can be
        instances of ``pathlib.PurePath``, ``Data`` or ``SchemaRepository``.
    
        If an argument is of type ``pathlib.PurePath``, and path points to file
        with a suffix '.json', '.yml' or '.yaml', json serializable data is
        decoded from the file. Otherwise, it is assumed that path points to a
        directory, which is recursively searched for json and yaml files. All
        decoded schemas are added to the repository. If a schema with the same
        `id` was previosly added, an exception is raised.
    
        If an argument is of type ``Data``, it should be a json serializable
        data representation of a JSON schema. If a schema with the same `id`
        was previosly added, an exception is raised.
    
        If an argument is of type ``SchemaRepository``, its schemas are added
        to the new repository. Previously added schemas with the same `id` are
        replaced.
    
        """
    
        def __init__(self, *args: typing.Union[pathlib.PurePath,
                                               Data,
                                               'SchemaRepository']): ...
    
        def validate(self,
                     schema_id: str,
                     data: Data):
            """Validate data against JSON schema.
    
            Args:
                schema_id: JSON schema identifier
                data: data to be validated
    
            Raises:
                jsonschema.ValidationError
    
            """
    
        def to_json(self) -> Data:
            """Export repository content as json serializable data.
    
            Entire repository content is exported as json serializable data.
            New repository can be created from the exported content by using
            :meth:`SchemaRepository.from_json`.
    
            """
    
        @staticmethod
        def from_json(data: typing.Union[pathlib.PurePath,
                                         Data]
                      ) -> 'SchemaRepository':
            """Create new repository from content exported as json serializable
            data.
    
            Creates a new repository from content of another repository that was
            exported by using :meth:`SchemaRepository.to_json`.
    
            Args:
                data: repository data
    
            """

Metadata

Release files for hat-json 0.4.1.dev20210120

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for hat-json 0.4.1.dev20210120
File Interpreter ABI Platform
hat_json-0.4.1.dev20210120-cp39-none-any.whl CPython 3.9 none any Details
hat_json-0.4.1.dev20210120-cp38-none-any.whl CPython 3.8 none any Details

Total release size: 16.9 kB

Release files / hat_json-0.4.1.dev20210120-cp39-none-any.whl

Download URL hat_json-0.4.1.dev20210120-cp39-none-any.whl
Size 8.4 kB
Tags CPython 3.9
SHA-256 checksum
How to use checksums
0c818fe953736a1bf6e13e78deb094a8e059ccdd710c7864f14153627fba7df2
BLAKE2b-256 checksum
How to use checksums
570763005165cac478ad78f172408c9af51ec6333bc4555e51c7e3dc60919a84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

Release files / hat_json-0.4.1.dev20210120-cp38-none-any.whl

Download URL hat_json-0.4.1.dev20210120-cp38-none-any.whl
Size 8.4 kB
Tags CPython 3.8
SHA-256 checksum
How to use checksums
c01793e89f0112ff4dddbef02f8724c23e0395838eea0b0536773e04a4529e74
BLAKE2b-256 checksum
How to use checksums
2fec8700e1f24a5db37a5e5e1aca743ca1a2e3c123e4023e70123b5a0815b93f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.1

Release history Release notifications | RSS feed

0.6.8

1 release file

0.6.7

1 release file

0.6.6

1 release file

0.6.5

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.29

1 release file

0.5.28

1 release file

0.5.27

1 release file

0.5.26

1 release file

0.5.25

1 release file

0.5.24

1 release file

0.5.23

1 release file

0.5.22

1 release file

0.5.21

1 release file

0.5.20

1 release file

0.5.19

1 release file

0.5.18

1 release file

0.5.17

1 release file

0.5.16

1 release file

0.5.15

1 release file

0.5.14

1 release file

0.5.13

1 release file

0.5.12

1 release file

0.5.11

1 release file

0.5.10

1 release file

0.5.9

1 release file

0.5.8

1 release file

0.5.7

1 release file

0.5.6

1 release file

0.5.5

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page