Metadata-Version: 2.1
Name: real_json
Version: 1.0.4
Summary: Wrapper class so that you can access `dict` and `list` as easily as in Javascript
Author-email: Harold Alcala <harold.dev@gmail.com>
License: MIT License
        
        Copyright (c) 2023 Harold Alcala
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/haalcala/python_js_json
Keywords: json,js,Javascript,wrapper
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: JavaScript
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Provides-Extra: dev
License-File: LICENSE

# python_js_json
Wrapper class so that you can access `dict` and `list` as easily as in Javascript

## Installation

    python3 -m pip install real_json

## usage

    ```python
    import real_json
    import json

    data = {
        "a": 1,
        "b": 2,
        "c": {
            "d": 3,
            "e": 4,
        },
        "f": [5, 6, 7],
    }
    wrapped = real_json.ify(data)

    # Test attribute access
    assert wrapped.a == 1
    assert wrapped.b == 2
    assert wrapped.c.d == 3
    assert wrapped.c.e == 4
    assert wrapped.f[0] == 5
    assert wrapped.f[1] == 6
    assert wrapped.f[2] == 7
    assert wrapped.f[3] == None
    assert wrapped.g == None
    assert wrapped.c.f == None

    # Test item access
    assert wrapped["a"] == 1
    assert wrapped["b"] == 2
    assert wrapped["c"]["d"] == 3
    assert wrapped["c"]["e"] == 4
    assert wrapped["f"][0] == 5
    assert wrapped["f"][1] == 6
    assert wrapped["f"][2] == 7
    assert wrapped["g"] == None
    assert wrapped["c"]["f"] == None

    # Test set attribute
    wrapped.a = 10
    assert wrapped.a == 10
    wrapped.g = 20
    assert wrapped.g == 20

    # Test set item
    wrapped["b"] = 20
    assert wrapped["b"] == 20
    wrapped["h"] = 30
    assert wrapped["h"] == 30

    # Test str and repr
    assert str(wrapped) == str(data)
    assert repr(wrapped) == repr(data)

    # Test len
    print("wrapped:", wrapped)
    assert len(wrapped) == 6
    assert len(wrapped.c) == 2
    assert len(wrapped.f) == 3

    # Test bool
    assert bool(wrapped) == True
    assert bool(wrapped.g) == True
    assert bool(wrapped.c) == True
    assert bool(wrapped.f)

    json.dump(wrapped.__dict__["_data"], "wrapped.json")

    data = {
        "name": "John",
        "age": 30,
        "cars": [
            {"model": "Ford", "year": 2020},
            {"model": "BMW", "year": 2019}
        ]
    }

    wrapped_data = real_json.ify(data)

    # Accessing values using dot notation
    print(wrapped_data.name)  # Output: "John"
    print(wrapped_data.age)  # Output: 30
    # Output: [{"model": "Ford", "year": 2020}, {"model": "BMW", "year": 2019}]
    print(wrapped_data.cars)

    # Accessing values using square brackets notation
    print(wrapped_data['name'])  # Output: "John"
    print(wrapped_data['age'])  # Output: 30
    # Output: [{"model": "Ford", "year": 2020}, {"model": "BMW", "year": 2019}]
    print(wrapped_data['cars'])

    # Accessing values that are not present in the data
    print(wrapped_data.address)  # Output: None
    print(wrapped_data['address'])  # Output: None

    # Accessing elements of the list using index notation
    print(wrapped_data.cars[0])  # Output: {"model": "Ford", "year": 2020}
    print(wrapped_data['cars'][0])  # Output: {"model": "Ford", "year": 2020}

    # Accessing elements of the list using index notation
    print(wrapped_data.cars[0].model)  # Output: "Ford"
    print(wrapped_data['cars'][0]['model'])  # Output: "Ford"

    # Accessing elements of the list using index notation
    print(wrapped_data.cars[2])  # Output: None
    print(wrapped_data['cars'][2])  # Output: None

    # Accessing elements of the list using index notation
    print(wrapped_data.cars[0].color)  # Output: None
    print(wrapped_data['cars'][0]['color'])  # Output: None

    json.dump(wrapped_data.__dict__["_data"], "data.json")
    ```
