Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unintuitive .to_json() behavior #85

Closed
kafonek opened this issue Nov 1, 2022 · 1 comment · Fixed by #102
Closed

Unintuitive .to_json() behavior #85

kafonek opened this issue Nov 1, 2022 · 1 comment · Fixed by #102

Comments

@kafonek
Copy link
Contributor

kafonek commented Nov 1, 2022

The behavior for the .to_json() method on YText, YArray, and YMap is not consistent with each other. YText returns a json serialized string. The other two return Python objects. I suggest these methods return JSON strings, and a separate .to_py() method be added to return the Python objects.

import y_py as Y

ydoc = Y.YDoc()
with ydoc.begin_transaction() as txn:
    ytext = txn.get_text('text')
    ytext.extend(txn, "foo")
    
    yarray = txn.get_array('array')
    yarray.append(txn, 'bar')
    
    ymap = txn.get_map('map')
    ymap.set(txn, 'key', 'value')
    
ytext.to_json(), yarray.to_json(), ymap.to_json()
>>> ('"foo"', ['bar'], {'key': 'value'})

import json
json.loads(ytext.to_json())
>>> 'foo'

json.loads(yarray.to_json())
>>> TypeError: the JSON object must be str, bytes or bytearray, not list
@Waidhoferj
Copy link
Collaborator

Yeah this is definitely misleading, I'll take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants