c0fec0de / anytree

Python tree data library
Apache License 2.0
947 stars 133 forks source link

JSON-Encoding for anytree.exporter does not support cyrillic #148

Closed IDrei closed 3 years ago

IDrei commented 3 years ago

Hello! Please, help me!!! I've built tree in Python using Anytree. Then I've exported it to JSON. After import tree from file I've got normal symbols. The problem is that JSON-file include 'bad' symbols instead of normal cyrillic. Here is a sample of code:

import smart_open
import json, pprint
from anytree import AnyNode, RenderTree
from anytree.exporter import JsonExporter
from anytree.importer import JsonImporter
json_file = r'd:\temp\test.json'
#building tree
root = AnyNode(uk = 'узел-0', parent=None)
b = AnyNode(uk = 'узел-01', parent=root)
a = AnyNode(uk = 'узел-011',parent=b)
g = AnyNode(uk = 'узел-021', parent = root)
print()
print('Tree for export:')
print(RenderTree(root).by_attr('uk'))
#export
exporter = JsonExporter(indent=2, sort_keys=True)
data = exporter.export(root)
with open(json_file, 'w', encoding='utf-8') as outfile:
    json.dump(data, outfile, ensure_ascii=False)
#import
importer = JsonImporter()
with open(json_file, encoding='utf-8') as json_file:
    data = json.load(json_file)
root = importer.import_(data)
print()
print('Imported tree:')
print(RenderTree(root).by_attr('uk'))
#print bad json
print()
print('data in JSON-file:')
pprint.pprint(data)

The result is:

Tree for export: узел-0 ├── узел-01 │ └── узел-011 └── узел-021

Imported tree: узел-0 ├── узел-01 │ └── узел-011 └── узел-021

data in JSON-file: ('{\n' ' "children": [\n' ' {\n' ' "children": [\n' ' {\n' ' "uk":"\u0443\u0437\u0435\u043b-011"\n' ' }\n' ' ],\n' ' "uk": "\u0443\u0437\u0435\u043b-01"\n' ' },\n' ' {\n' ' "uk": "\u0443\u0437\u0435\u043b-021"\n' ' }\n' ' ],\n' ' "uk": "\u0443\u0437\u0435\u043b-0"\n' '}')

While writing cyrillic to JSON is ok.

I've already asked on stackoverflow, but noone helped ( https://stackoverflow.com/questions/64914065/json-encoding-for-anytree-exporter-does-not-support-cyrillic )

IDrei commented 3 years ago

The answer is:

JsonExporter(indent=2, sort_keys=True, ensure_ascii=False)