justpy-org / justpy

An object oriented high-level Python Web Framework that requires no frontend programming
https://justpy.io
Apache License 2.0
1.22k stars 96 forks source link

Consider alternative JSON/JS decoding library #509

Closed hroemer closed 2 years ago

hroemer commented 2 years ago

Motivation

An unrelated package issue (#496) steered me into looking into the current usage of the demjson3 package.

At first sight it seemed that parsing JSON was the requirement for this package. It actually turned out that the main requirement is to handle syntax for setting options of the included JavaScript libraries (e.g. HighCharts), which are not in strict JSON but rather arbitrary JavaScript objects:

{
  "chart" : {
    "renderTo": "container",
    "type":"bar"
  }
}

vs.

{
  chart: {
    renderTo: 'container',
    type: 'bar'
  }
}

demjson3 is a fork of demjson for Python3 that meets the above requirement. The project seems to be maintained by only one contributor currently and might impose future maintainability problems.

Alternatives

The stdlib json module cannot handle JavaScript syntax, e.g. unquoted keys will cause an exception. The entire removal of this dependency is therefore not feasible.

The packages PyYAML and hjson both provide handling of JavaScript syntax and offer faster decoding performance compared to demjson3.

Comparison

demjson3 3.0.5 PyYAML 6.0 hjson 3.1.0
supports JSON
supports JS
supports missing separation spaces
native code ✅ (optionally)
maintenance status 👍 👍
decoding time (1.000 runs) 1.87s 1.36s 0.12s
decoding speed improvement 1.0x ~1.4x ~16.0x
dist (sdist, wheel) 132.4 kB 125.0 kB, ~700 kB 40.5 kB, 54.0 kB

hjson seems to be a good fit.

What is your opinion on giving it a try?

WolfgangFahl commented 2 years ago

I am in favor of this - i think we just need a few unit tests checking the compatibility and we should be ready for a change.