karupanerura / p5-JSON5

The JSON5 implementation for Perl 5
Other
2 stars 0 forks source link

encoding #1

Open gonzus opened 4 years ago

gonzus commented 4 years ago

Is there any reason why there is no support for encode?

jforget commented 1 month ago

Direct answer

Use JSON::PP::encode_json. Module JSON::PP is a prereq for JSON5, so you do not need to install it, you do not need to add a use JSON::PP into you program. Example:

use JSON5;
my %chem_elem = (H => 1, He => 2, Li => 3);
print JSON::PP::encode_json(\%chem_elem), "\n";

Answer about the context

As mentioned in JSON5.org, JSON5 is aimed at human-written files, such as config files. In another website (sorry, I do not remember which website), I have read that JSON v4 is still relevant for machine-written files and that any program dumping data as JSON should use the stricter v4 version.

See also this comparison.

Module JSON5 deals with human-to-machine communication, not machine-to-machine or machine-to-human communication. So the module does not support encode. It is lighter, shorter and it follows the KISS principle (keep it simple, stupid) and Occam's Razor (Entities must not be multiplied beyond necessity).

J Forget