pyjson2dataclass


Namepyjson2dataclass JSON
Version 1.4.3 PyPI version JSON
download
home_pageNone
SummaryParse json to dataclass
upload_time2024-12-17 16:57:17
maintainerNone
docs_urlNone
authoryinpeach
requires_python>=3.9
licenseNone
keywords dataclass json yinpeach
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Parse json to python dataclass

## Installion

```sh
pip3 install pyjson2dataclass
```

## Usage

### json2dataclass

```txt
usage: json2dataclass [-h] [-d SAVE_DIR] [-i [INPUT ...]] [-p] [-f]

options:
  -h, --help            show this help message and exit
  -d SAVE_DIR, --save-dir SAVE_DIR
                        specify the directory parsed dataclass file save to
  -i [INPUT ...], --input [INPUT ...]
                        specify the input json file path
  -p, --pascal          use pascalName for parsed dataclass filename
  -f, --force           enable overwrite exists dataclass

```

```sh
json2dataclass -i <json file1> <json file2> ... -d dataclass
```
What above did is parse json file passed by `-i` to dataclass python file and save them to directory 'dataclass'

### json2dataclass_test

```txt
usage: json2dataclass_test [-h] [-d DATACLASS_DIR] [-j JSON_DIR]

options:
  -h, --help            show this help message and exit
  -d DATACLASS_DIR, --dataclass-dir DATACLASS_DIR
                        specify parsed dataclass directory to be test
  -j JSON_DIR, --json-dir JSON_DIR
                        specify json directory to be test
```

```sh
json2dataclass_test -j json -d dataclass
```

We can use 'json2dataclass_test' to check dataclass python quickly



## Examples

```python
from pyjson2dataclass import parse
import json


filepath = 'json/example1.json'

parse_text = parse('Example', json.load(open(filepath)))
open('example.py', 'w').write(parse_text)
```

### [json/example1.json]
```json
{
    "pascalName1": {
        "pascalName2": [
            1,
            2
        ]
    },
    "pascalName3": [
        {
            "pascalName4": [
                {
                    "pascalName5": {}
                }
            ]
        }
    ]
}
```
### [example1.py]
```python
from typing import Dict, List, Any, Optional
from dataclasses import dataclass


@dataclass
class PascalName4Item:
    pascal_name5: dict

    def __init__(self, data: dict | None):
        if not data:
            return None
        self.pascal_name5 = dict(data["pascalName5"])


@dataclass
class PascalName1:
    pascal_name2: List[int]

    def __init__(self, data: dict | None):
        if not data:
            return None
        self.pascal_name2 = [(int(i0)) for i0 in data["pascalName2"]]


@dataclass
class PascalName3Item:
    pascal_name4: List[PascalName4Item]

    def __init__(self, data: dict | None):
        if not data:
            return None
        self.pascal_name4 = [(PascalName4Item(i0)) for i0 in data["pascalName4"]]


@dataclass
class Example1:
    pascal_name1: PascalName1
    pascal_name3: List[PascalName3Item]

    def __init__(self, data: dict | None):
        if not data:
            return None
        self.pascal_name1 = PascalName1(data["pascalName1"])
        self.pascal_name3 = [(PascalName3Item(i0)) for i0 in data["pascalName3"]]



```

## Use example.py
### Run
```python
import sys
sys.path.append('./dataclass')

from example1 import Example1
import json

filepath = 'json/example1.json'

example = Example1(json.load(open(filepath)))
print(example)
print(example.pascal_name1)
print(example.pascal_name3)
```
### Output
```txt
Example1(pascal_name1=PascalName1(pascal_name2=[1, 2]), pascal_name3=[PascalName3Item(pascal_name4=[PascalName4Item(pascal_name5={})])])
PascalName1(pascal_name2=[1, 2])
[PascalName3Item(pascal_name4=[PascalName4Item(pascal_name5={})])]
```

## Back to HomePage [https://github.com/verssionhack/pyjson2dataclass]

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pyjson2dataclass",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "dataclass, json, yinpeach",
    "author": "yinpeach",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/82/13/2b40bbde501d621a3ebdd0e79df3cb598d92ae46c8c4d16c810ea4ad970c/pyjson2dataclass-1.4.3.tar.gz",
    "platform": null,
    "description": "# Parse json to python dataclass\n\n## Installion\n\n```sh\npip3 install pyjson2dataclass\n```\n\n## Usage\n\n### json2dataclass\n\n```txt\nusage: json2dataclass [-h] [-d SAVE_DIR] [-i [INPUT ...]] [-p] [-f]\n\noptions:\n  -h, --help            show this help message and exit\n  -d SAVE_DIR, --save-dir SAVE_DIR\n                        specify the directory parsed dataclass file save to\n  -i [INPUT ...], --input [INPUT ...]\n                        specify the input json file path\n  -p, --pascal          use pascalName for parsed dataclass filename\n  -f, --force           enable overwrite exists dataclass\n\n```\n\n```sh\njson2dataclass -i <json file1> <json file2> ... -d dataclass\n```\nWhat above did is parse json file passed by `-i` to dataclass python file and save them to directory 'dataclass'\n\n### json2dataclass_test\n\n```txt\nusage: json2dataclass_test [-h] [-d DATACLASS_DIR] [-j JSON_DIR]\n\noptions:\n  -h, --help            show this help message and exit\n  -d DATACLASS_DIR, --dataclass-dir DATACLASS_DIR\n                        specify parsed dataclass directory to be test\n  -j JSON_DIR, --json-dir JSON_DIR\n                        specify json directory to be test\n```\n\n```sh\njson2dataclass_test -j json -d dataclass\n```\n\nWe can use 'json2dataclass_test' to check dataclass python quickly\n\n\n\n## Examples\n\n```python\nfrom pyjson2dataclass import parse\nimport json\n\n\nfilepath = 'json/example1.json'\n\nparse_text = parse('Example', json.load(open(filepath)))\nopen('example.py', 'w').write(parse_text)\n```\n\n### [json/example1.json]\n```json\n{\n    \"pascalName1\": {\n        \"pascalName2\": [\n            1,\n            2\n        ]\n    },\n    \"pascalName3\": [\n        {\n            \"pascalName4\": [\n                {\n                    \"pascalName5\": {}\n                }\n            ]\n        }\n    ]\n}\n```\n### [example1.py]\n```python\nfrom typing import Dict, List, Any, Optional\nfrom dataclasses import dataclass\n\n\n@dataclass\nclass PascalName4Item:\n    pascal_name5: dict\n\n    def __init__(self, data: dict | None):\n        if not data:\n            return None\n        self.pascal_name5 = dict(data[\"pascalName5\"])\n\n\n@dataclass\nclass PascalName1:\n    pascal_name2: List[int]\n\n    def __init__(self, data: dict | None):\n        if not data:\n            return None\n        self.pascal_name2 = [(int(i0)) for i0 in data[\"pascalName2\"]]\n\n\n@dataclass\nclass PascalName3Item:\n    pascal_name4: List[PascalName4Item]\n\n    def __init__(self, data: dict | None):\n        if not data:\n            return None\n        self.pascal_name4 = [(PascalName4Item(i0)) for i0 in data[\"pascalName4\"]]\n\n\n@dataclass\nclass Example1:\n    pascal_name1: PascalName1\n    pascal_name3: List[PascalName3Item]\n\n    def __init__(self, data: dict | None):\n        if not data:\n            return None\n        self.pascal_name1 = PascalName1(data[\"pascalName1\"])\n        self.pascal_name3 = [(PascalName3Item(i0)) for i0 in data[\"pascalName3\"]]\n\n\n\n```\n\n## Use example.py\n### Run\n```python\nimport sys\nsys.path.append('./dataclass')\n\nfrom example1 import Example1\nimport json\n\nfilepath = 'json/example1.json'\n\nexample = Example1(json.load(open(filepath)))\nprint(example)\nprint(example.pascal_name1)\nprint(example.pascal_name3)\n```\n### Output\n```txt\nExample1(pascal_name1=PascalName1(pascal_name2=[1, 2]), pascal_name3=[PascalName3Item(pascal_name4=[PascalName4Item(pascal_name5={})])])\nPascalName1(pascal_name2=[1, 2])\n[PascalName3Item(pascal_name4=[PascalName4Item(pascal_name5={})])]\n```\n\n## Back to HomePage [https://github.com/verssionhack/pyjson2dataclass]\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Parse json to dataclass",
    "version": "1.4.3",
    "project_urls": {
        "Documentation": "https://github.com/verssionhack/pyjson2dataclass/README.md",
        "Homepage": "https://github.com/verssionhack",
        "Repository": "https://github.com/verssionhack/pyjson2dataclass.git"
    },
    "split_keywords": [
        "dataclass",
        " json",
        " yinpeach"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "94f150fbe9bf2ca83841f98c694f9b4b4fc3700afcdea334b6a9e5b9b8b38eb6",
                "md5": "acc7171c70ccd46ca613b90b8ac08a18",
                "sha256": "2154fa4e2a4d0b1652147acb7d848c0a7f2b89f79db238cecd7cb4831810ef05"
            },
            "downloads": -1,
            "filename": "pyjson2dataclass-1.4.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "acc7171c70ccd46ca613b90b8ac08a18",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 7813,
            "upload_time": "2024-12-17T16:57:15",
            "upload_time_iso_8601": "2024-12-17T16:57:15.231793Z",
            "url": "https://files.pythonhosted.org/packages/94/f1/50fbe9bf2ca83841f98c694f9b4b4fc3700afcdea334b6a9e5b9b8b38eb6/pyjson2dataclass-1.4.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "82132b40bbde501d621a3ebdd0e79df3cb598d92ae46c8c4d16c810ea4ad970c",
                "md5": "ed8467b9daa19ab380fb4431c1b23376",
                "sha256": "f161dad5aa24c8e8d5ffe0c1197aa19ab3b6abb3fa85bdeb9b2c2b794f009dcb"
            },
            "downloads": -1,
            "filename": "pyjson2dataclass-1.4.3.tar.gz",
            "has_sig": false,
            "md5_digest": "ed8467b9daa19ab380fb4431c1b23376",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 6798,
            "upload_time": "2024-12-17T16:57:17",
            "upload_time_iso_8601": "2024-12-17T16:57:17.621470Z",
            "url": "https://files.pythonhosted.org/packages/82/13/2b40bbde501d621a3ebdd0e79df3cb598d92ae46c8c4d16c810ea4ad970c/pyjson2dataclass-1.4.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-17 16:57:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "verssionhack",
    "github_project": "pyjson2dataclass",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pyjson2dataclass"
}
        
Elapsed time: 0.42199s