y-crdt/ypy

Unintuitive .to_json() behavior

kafonek opened this issue · 1 comments

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

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