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

new methods to emit JSON #20

Open
wisotzky opened this issue May 26, 2018 · 1 comment
Open

new methods to emit JSON #20

wisotzky opened this issue May 26, 2018 · 1 comment

Comments

@wisotzky
Copy link

Would be good to have a new method to natively create Python JSON values (dict):

Example:

>>> data = jxmlease.parse('<a>123</a>')
>>> json = data.emit_json()
>>> print json
{u'a': u'123'}
>>> print json['a']
123

While the current implementation of prettyprint is almost doing the same, the intend would not really be printing but using it with other Python libraries like deepdiff.

Example:

>>> import deepdiff
>>> d1 = jxmlease.parse('<a>123</a>')
>>> d2 = jxmlease.parse('<a>456</a>')
>>> deepdiff.DeepDiff(d1, d2)
{'values_changed': {"root['a']": {'new_value': XMLCDATANode(xml_attrs=OrderedDict(), value=u'456'), 'old_value': XMLCDATANode(xml_attrs=OrderedDict(), value=u'123')}}}
>>>
>>> deepdiff.DeepDiff(d1.emit_json(), d2.emit_json())
{'values_changed': {"root['a']": {'new_value': u'456', 'old_value': u'123'}}}

In consequence the output of deepdiff would be very much more streamlined.

@wisotzky
Copy link
Author

wisotzky commented May 27, 2018

After short code reading, solution (workaround) might be to set the attribute currdepth to a non-zero value. Unclear, if this workaround is official - as this attribute is not really defined in the official API description.

Example:

>>> x = jxmlease.parse("<a/>")
>>> x.prettyprint(currdepth=1)
{u'a': u''}
>>> x.prettyprint(currdepth=1)['a']
u''

While this was not supported:

>>> y=x.prettyprint()['a']
{u'a': u''}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'

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

No branches or pull requests

1 participant