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

pretty print and ident config? #19

Open
sbant opened this issue Sep 15, 2021 · 2 comments
Open

pretty print and ident config? #19

sbant opened this issue Sep 15, 2021 · 2 comments

Comments

@sbant
Copy link

sbant commented Sep 15, 2021

No description provided.

@Kijewski
Copy link
Owner

Kijewski commented Nov 16, 2021

Pyjson5's parser does not preserve the comments or whitespaces. So the serializer is nearly identical to standard json.dump(s), except that pyjson5's output is HTML-safe and ASCII-only. If you need pretty printed data, then most likely you don't want either, because it hurts readability; and speed is also most likely quite unimportant, because it's not a lot of data to have to serialize. So I guess you could use pyjson5 for parsing, and json for stringifying.

@dg-pb
Copy link

dg-pb commented May 5, 2023

I agree with sbant. Pretty printing is a desirable feature. After all, json5 emphasises on human friendliness, comments, etc. Using this parser a lot of it is lost.

However, the speed is amazing, nothing compares to it. And in some cases both are needed, e.g. real-time speed crucial app that needs to track config changes, which are potentially a manual user input.

dump(load(string)) == string, would be great to have as an option, even if a bit slower.

And another package is unbelievably slow.

Yaml is too slow, json is too raw, II was hoping to find a happy middle. Many are too complex, others at the risk of extinction, etc, etc... Difficult choice to be honest.

Benchmarked this package against a great lot and all looked great - happy middle, but this is a bit of a dealbreaker. For now, I will use abstracted orjson so I can swap later given I find a happy middle I was after. Maybe you know what sort of config parser sublime text uses? That one looked perfect tbh.

Adding benchmarks for reference:

    print(timeit.timeit('json.loads(json_text)', number=N, globals=globals()))
    print(timeit.timeit('ujson.loads(json_text)', number=N, globals=globals()))
    print(timeit.timeit('orjson.loads(json_text)', number=N, globals=globals()))
    print(timeit.timeit('pyjson5.loads(json_text)', number=N, globals=globals()))
    # print(timeit.timeit('yaml.load(json_text, Loader=Loader)', number=N, globals=globals()))
    print(timeit.timeit('yaml.load(json_text, Loader=CLoader)', number=N, globals=globals()))
# 0.27317825198406354
# 0.12556396395666525
# 0.0474954119999893
# 0.12391428096452728
# 42.89380981097929
# 5.733772745996248

    print(timeit.timeit('json.dumps(data)', number=N, globals=globals()))
    print(timeit.timeit('ujson.dumps(data)', number=N, globals=globals()))
    print(timeit.timeit('orjson.dumps(data).decode()', number=N, globals=globals()))
    print(timeit.timeit('pyjson5.dumps(data)', number=N, globals=globals()))
    # print(timeit.timeit('yaml.dump(data, Dumper=Dumper)', number=N, globals=globals()))
    print(timeit.timeit('yaml.dump(data, Dumper=CDumper)', number=N, globals=globals()))
# 0.28529386199079454
# 0.10934980498859659
# 0.03539454500423744
# 0.1855485350242816
# 19.672126159013715
# 5.398652292031329

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

3 participants