py2json


Namepy2json JSON
Version 0.1.28 PyPI version JSON
download
home_page
SummaryTools for json serialization of python objects
upload_time2023-08-16 23:26:34
maintainer
docs_urlNone
author
requires_python
licensemit
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Tools for json serialization of python objects

# py2json

Here we tackle the problem of serializing a python object into a json. 

Json is a convenient choice for web request responses or working with mongoDB for instance. 

It is usually understood that we serialize an object to be able to deserialize it to recover the original object: Implicit in this is some definition of equality, which is not as trivial as it may seem. Usually **some** aspects of the deserialized object will be different, so we need to be clear on what should be the same.

For example, we probably don't care if the address of the deserialized object is different. But we probably care that it's key attributes are the same.

What should guide us in deciding what aspects of an object should be recovered? 

Behavior. 

The only value of an object is behavior that will ensue. This may be the behavior of all or some of the methods of a serialized instance, or the behavior of some other functions that will depend on the deserialized object. 

Our approach to converting a python object to a json will touch on some i2i cornerstones that are more general: Conversion and contextualization. 


# Behavior equivalence: What do we need an object to have?

Say we are given the code below.

```python
def func(obj):
    return obj.a + obj.b

class A:
    e = 2
    def __init__(self, a=0, b=0, c=1, d=10):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        
    def target_func(self, x=3):
        t = func(self)
        tt = self.other_method(t)
        return x * tt / self.e
    
    def other_method(self, x=1):
        return self.c * x
```

Which we use to make the following object
```python
obj = A(a=2, b=3)
```


Say we want to json-serialize this so that a deserialized object `dobj` is such that for all valid `obj`, resulting `dobj`, and valid `x` input:

```
obj.target_func(x) == A.target_func(obj, x) == A.target_func(dobj, x)
```
The first equality is just a reminder of a python equivalence. 
The second equality is really what we're after. 

When this is true, we'll say that `obj` and `dobj` are equivalent on `A.target_func` -- or just "equivalent" when the function(s) it should be equivalent is clear. 

To satisfy this equality we need `dobj` to:
- Contain all the attributes it needs to be able to compute the `A.target_func` function -- which means all the expressions contained in that function or, recursively, any functions it calls. 
- Such that the values of a same attribute of `obj` and `dobj` are equivalent (over the functions in the call try of the target function that involve these attributes.

Let's have a manual look at it. 
First, you need to compute `func(self)`, which will require the attributes `a` and `b`. 
Secondly, you'll meed to computer `other_method`, which uses attribute `c`. 
Finally, the last expression, `x * tt / self.e` uses the attribute `e`. 

So what we need to make sure we serialize the attributes: `{'a', 'b', 'c', 'e'}`. 

That wasn't too hard. But it could get convoluted. Either way, we really should use computers for such boring tasks!

That's something `py2json` would like to help you with.
            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "py2json",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/0d/1d/65bf4cf4d18e4420d97e9fdef32b1a78b44a862b614fdaaad67132cc9007/py2json-0.1.28.tar.gz",
    "platform": null,
    "description": "Tools for json serialization of python objects\n\n# py2json\n\nHere we tackle the problem of serializing a python object into a json. \n\nJson is a convenient choice for web request responses or working with mongoDB for instance. \n\nIt is usually understood that we serialize an object to be able to deserialize it to recover the original object: Implicit in this is some definition of equality, which is not as trivial as it may seem. Usually **some** aspects of the deserialized object will be different, so we need to be clear on what should be the same.\n\nFor example, we probably don't care if the address of the deserialized object is different. But we probably care that it's key attributes are the same.\n\nWhat should guide us in deciding what aspects of an object should be recovered? \n\nBehavior. \n\nThe only value of an object is behavior that will ensue. This may be the behavior of all or some of the methods of a serialized instance, or the behavior of some other functions that will depend on the deserialized object. \n\nOur approach to converting a python object to a json will touch on some i2i cornerstones that are more general: Conversion and contextualization. \n\n\n# Behavior equivalence: What do we need an object to have?\n\nSay we are given the code below.\n\n```python\ndef func(obj):\n    return obj.a + obj.b\n\nclass A:\n    e = 2\n    def __init__(self, a=0, b=0, c=1, d=10):\n        self.a = a\n        self.b = b\n        self.c = c\n        self.d = d\n        \n    def target_func(self, x=3):\n        t = func(self)\n        tt = self.other_method(t)\n        return x * tt / self.e\n    \n    def other_method(self, x=1):\n        return self.c * x\n```\n\nWhich we use to make the following object\n```python\nobj = A(a=2, b=3)\n```\n\n\nSay we want to json-serialize this so that a deserialized object `dobj` is such that for all valid `obj`, resulting `dobj`, and valid `x` input:\n\n```\nobj.target_func(x) == A.target_func(obj, x) == A.target_func(dobj, x)\n```\nThe first equality is just a reminder of a python equivalence. \nThe second equality is really what we're after. \n\nWhen this is true, we'll say that `obj` and `dobj` are equivalent on `A.target_func` -- or just \"equivalent\" when the function(s) it should be equivalent is clear. \n\nTo satisfy this equality we need `dobj` to:\n- Contain all the attributes it needs to be able to compute the `A.target_func` function -- which means all the expressions contained in that function or, recursively, any functions it calls. \n- Such that the values of a same attribute of `obj` and `dobj` are equivalent (over the functions in the call try of the target function that involve these attributes.\n\nLet's have a manual look at it. \nFirst, you need to compute `func(self)`, which will require the attributes `a` and `b`. \nSecondly, you'll meed to computer `other_method`, which uses attribute `c`. \nFinally, the last expression, `x * tt / self.e` uses the attribute `e`. \n\nSo what we need to make sure we serialize the attributes: `{'a', 'b', 'c', 'e'}`. \n\nThat wasn't too hard. But it could get convoluted. Either way, we really should use computers for such boring tasks!\n\nThat's something `py2json` would like to help you with.",
    "bugtrack_url": null,
    "license": "mit",
    "summary": "Tools for json serialization of python objects",
    "version": "0.1.28",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0d1d65bf4cf4d18e4420d97e9fdef32b1a78b44a862b614fdaaad67132cc9007",
                "md5": "5d180e69dabaddb640749386e52c7fea",
                "sha256": "e9ea3a3d0af599ab23adbf3a23f142e41bab093640edc497d0803e0d9b1eed6b"
            },
            "downloads": -1,
            "filename": "py2json-0.1.28.tar.gz",
            "has_sig": false,
            "md5_digest": "5d180e69dabaddb640749386e52c7fea",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 21556,
            "upload_time": "2023-08-16T23:26:34",
            "upload_time_iso_8601": "2023-08-16T23:26:34.212559Z",
            "url": "https://files.pythonhosted.org/packages/0d/1d/65bf4cf4d18e4420d97e9fdef32b1a78b44a862b614fdaaad67132cc9007/py2json-0.1.28.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-16 23:26:34",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "py2json"
}
        
Elapsed time: 0.10208s