Metadata-Version: 2.3
Name: corelibs-json
Version: 1.0.0
Summary: CoreLibs Json utilities
Author: Clemens Schwaighofer
Author-email: Clemens Schwaighofer <clemens.schwaighofer@omc.com>
Requires-Dist: jmespath>=1.1.0
Requires-Dist: jsonpath-ng>=1.7.0
Requires-Python: >=3.13
Description-Content-Type: text/markdown

# CoreLibs Python Json support

This is part of the Python CoreLibs

## Overview

Various support for JSON like jmes path search and support function for json dump

## Install

```sh
uv add --index opj-pypi=https://git.egplusww.jp/api/packages/PyPI/pypi/simple/ corelibs-json
```

## Usage

The following modules exist

- corelibs_json.jmespath_support
- corelibs_json.json_support

### jmespath_support usage

jmespath search support methods

```py
from corelibs_json.jmespath_support import jmespath_search
```

#### jmespath_search

Use jmespath to search in a dictionary

```py
def jmespath_search(search_data: dict[Any, Any] | list[Any], search_params: str) -> Any:
```

Will throw ValueError if an error is encountered

Example:

See: <https://jmespath.org/>

### json_support usage

Various support methods for json dumping

```py
from corelibs_json.json_support import DateTimeEncoder, default_isoformat, json_dumps, modify_with_jsonpath
```

#### DateTimeEncoder (Class)

Can be used as the "cls" argument in "json.dumps", makes sure a date is in the isoformat so it does not trip the json encoding

Example:

```py
import json
from corelibs_json.json_support import DateTimeEncoder

out = json.dumps(incoming, cls=DateTimeEncoder)
```

#### default_isoformat (method)

Can be used as the "default" arugment in "json.dumps",  makes sure a date is in the isoformat so it does not trip the json encoding

Example:

```py
import json
from corelibs_json.json_support import default_isoformat

out = json.dumps(incoming, default=default_isoformat)
```

#### json_dumps

Dump data in a safe format with ensure ascii and default everything to string

```py
def json_dumps(data: Any) -> str:
```

#### modify_with_jsonpath

Search in a dicitonary and replace data with jsonpath_ng

```py
def modify_with_jsonpath(data: dict[Any, Any], path: str, new_value: Any) -> dict[Any, Any]:
```

Example:

```py
from corelibs_json.json_support import modify_with_jsonpath

data = {
    'bar': {
        'a': 1,
        'b': 'c'
    },
}
print(f"OUT: {modify_with_jsonpath(__data, 'bar.a', 42)}")
```

Output form above will be:

```txt
{
    'bar': {
        'a': 42,
        'b': 'c'
    },
}
```

## Development

### UV setup

uv must be [installed](https://docs.astral.sh/uv/getting-started/installation/)

### Python venv setup

After clone, run the command below to install all dependenciss

```sh
uv sync
```

### Build and Publish

```sh
uv build
uv publish --index opj-pypi --token <gitea token>
```

### Python tests

All python tests are the tests/ folder. They are structured by the source folder layout

run them with

```sh
uv run pytest
```

Get a coverate report

```sh
uv run pytest --cov=<project>
uv run pytest --cov=<project> --cov-report=term-missing
```

### Other tests

In the test-run folder usage and run tests are located, runt them below

```sh
uv run test-run/<script>
```
