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

dicttoxml adds 'b...' #126

Closed
Jeroendevr opened this issue May 5, 2022 · 4 comments
Closed

dicttoxml adds 'b...' #126

Jeroendevr opened this issue May 5, 2022 · 4 comments

Comments

@Jeroendevr
Copy link
Contributor

Jeroendevr commented May 5, 2022

Describe the bug
Using the function json2xml.dicttoxml.dicttoxml

To Reproduce
Steps to reproduce the behavior:

  1. Run :
from json2xml.dicttoxml import dicttoxml
obj = {'bike': 'blue'}
xml = dicttoxml(obj=obj ,root=False)
print(xml)
  1. See:
b'<bike type="str">blue</bike>'

Expected behavior

<bike type="str">blue</bike>

Screenshots
NA

Desktop (please complete the following information):

  • OS: [macOS]
  • Python 3.9.12
  • json2xml 3.18.0

Additional context
It seems to happen around here:

# dicttoxml.py
# line 445
    if root:
        output.append('<?xml version="1.0" encoding="UTF-8" ?>')
        output_elem = convert(
            obj, ids, attr_type, item_func, cdata, item_wrap, parent=custom_root
        )
        output.append(f"<{custom_root}{namespacestr}>{output_elem}</{custom_root}>")
    else:
-->     output.append( #adds ' '
            convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent="")
        )

--> return "".join(output).encode("utf-8") #adds b 
@vinitkumar
Copy link
Owner

The method 'dicttoxml' returns bytes.
You can always decode it to string >>> "".join(output).encode("utf-8").decode("utf-8")

I am not sure if I can do the change without breaking compatibility for other usage of this library.

@Jeroendevr
Copy link
Contributor Author

Jeroendevr commented May 11, 2022

Ah good to know. What would be a usage to have the function return it as bytes?
Thanks also for the solution to fix it in my situation.

@vinitkumar
Copy link
Owner

I think it was probably to not get on the encoding and let user have a say in the decoding it.

@Jeroendevr
Copy link
Contributor Author

I agree, thanks for the explanation

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

2 participants