ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
288 stars 40 forks source link

Error: got an unexpected keyword argument 'origin' #190

Open JobaDiniz opened 10 months ago

JobaDiniz commented 10 months ago

I come from dotnet - new to python - and I was struggling to find the best way to serialize/deserialize python classes, until I found this library. It should be simple as this library does it, so thank you for that.

Now into my issue, I'm trying to deserialize the following JSON:

{
  "id": "28aafb64-e081-4b98-8e3d-56d0376ea078",
  "data": {
    "tab": {
      "active": true,
      "audible": false,
      "autoDiscardable": true,
      "discarded": false,
      "favIconUrl": "https://opensource-demo.orangehrmlive.com/web/dist/favicon.ico?v=1689053487449",
      "groupId": -1,
      "height": 911,
      "highlighted": true,
      "id": 1980783810,
      "incognito": false,
      "index": 1,
      "mutedInfo": {
        "muted": false
      },
      "pinned": false,
      "selected": true,
      "status": "complete",
      "title": "OrangeHRM",
      "url": "https://opensource-demo.orangehrmlive.com/web/index.php/auth/login",
      "width": 1920,
      "windowId": 1980783809
    }
  }
}

Into these classes:

class BrowserTab:
    def __init__(self, id: Optional[str], index: int, active: bool, url: Optional[str], title: Optional[str]) -> None:
        self.id = id
        self.index = index
        self.active = active
        self.url = url
        self.title = title

class ConnectionResponse:
    def __init__(self, tab: BrowserTab, pageAnalysis: object) -> None:
        self.tab = tab
        self.pageAnalysis = pageAnalysis

TResponse = TypeVar('TResponse')
class Response(Generic[TResponse]):
    def __init__(self, id: str, error: object, data: TResponse) -> None:
        self.id = id
        self.error = error
        self.data = data

The code to deserialize is: jsons.loads(response, commands.Response[commands.ConnectionResponse])

This gives me an error:

jsons.exceptions.DeserializationError: Could not deserialize value "{'id': '28aafb64-e081-4b98-8e3d-56d0376ea078', 'data': {'tab': {'active': True, 'audible': False, 'autoDiscardable': True, 'discarded': False, 'favIconUrl': 'https://opensource-demo.orangehrmlive.com/web/dist/favicon.ico?v=1689053487449', 'groupId': -1, 'height': 911, 'highlighted': True, 'id': 1980783810, 'incognito': False, 'index': 1, 'mutedInfo': {'muted': False}, 'pinned': False, 'selected': True, 'status': 'complete', 'title': 'OrangeHRM', 'url': 'https://opensource-demo.orangehrmlive.com/web/index.php/auth/login', 'width': 1920, 'windowId': 1980783809}}}" into "server.commands.Response". 
Response.__init__() got an unexpected keyword argument 'origin'

Where does this "origin" coming from?

UPDATE Also, I don't know the difference between jsons.load and jsons.loads. So when trying load (singular), I got different error:

raise DeserializationError(message, json_obj, cls) from err
jsons.exceptions.DeserializationError: Could not deserialize value "{"id":"ca9a6637-243f-435f-920f-f163d52cc426","data":{"tab":{"active":true,"audible":false,"autoDiscardable":true,"discarded":false,"favIconUrl":"https://opensource-demo.orangehrmlive.com/web/dist/favicon.ico?v=1689053487449","groupId":-1,"height":911,"highlighted":true,"id":1980784105,"incognito":false,"index":1,"mutedInfo":{"muted":false},"pinned":false,"selected":true,"status":"complete","title":"OrangeHRM","url":"https://opensource-demo.orangehrmlive.com/web/index.php/auth/login","width":1920,"windowId":1980783809}}}" into "server.commands.Response". 
string indices must be integers, not 'str'