pyjson5 icon indicating copy to clipboard operation
pyjson5 copied to clipboard

pretty print and ident config?

Open sbant opened this issue 3 years ago • 2 comments

sbant avatar Sep 15 '21 06:09 sbant

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.

Kijewski avatar Nov 16 '21 17:11 Kijewski

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

dg-pb avatar May 05 '23 08:05 dg-pb