halaxa / json-machine

Efficient, easy-to-use, and fast PHP JSON stream parser
Apache License 2.0
1.08k stars 65 forks source link

CLI script for streaming #88

Open pkoppstein opened 2 years ago

pkoppstein commented 2 years ago

@halaxa asked me to start a new thread stemming from the discussion at https://github.com/halaxa/json-machine/issues/73

Hopefully the script described below, which I'll call jm, will provide a basis for discussion or perhaps even the foundation of a script worthy of inclusion in the JSON Machine repository.

The --help option produces quite extensive documentation, so for now I'll just add two points:

  1. The script attempts to make installation as painless as possible for the uninitiated, and I hope that further enhancements in that regard will be added;
  2. Ideally, the script would preserve the precision of all JSON numbers (at least as the default) -- see https://github.com/halaxa/json-machine/issues/87

[ EDIT: The jm script is now in the "jm" repository. ]

halaxa commented 2 years ago

Thank you. I'm already prototyping the CLI. You really put an effort into the help. May I alternatively use some parts of it?

pkoppstein commented 2 years ago

Of course, that's why I've posted here!

halaxa commented 2 years ago

;)

pkoppstein commented 2 years ago

@halaxa -- Assuming (as I do) that the default encoder should be the PassThruDecoder, should there be an option to use the standard one? If so, what should it be called? Since the main point would be to convert JSON numbers to their PHP equivalent, I'm thinking of "--recode" or perhaps "--php". I think any any variation of encode or decode would be too confusing. Your thoughts?

pkoppstein commented 2 years ago

Here's jm Version 0.0.5, which uses the PassThruDecoder to preserve numeric literals. It also has the side-effect of allowing mal-formed JSON, but maybe that's not such a bad thing?

[ EDIT: The version originally referenced (i.e., using the PassThruDecoder) can be viewed here at the jm repository ]

pkoppstein commented 1 year ago

The jm script now has a bunch of options making it quite a handy tool for a variety of tasks. jm --help gives detailed help. Here's the output of jm -h:

Usage: jm [ OPTIONS ]  [ FILEPATH ... ]
or:    jm [-h | --help]
where JSONPOINTER defaults to '' and FILEPATH defaults to stdin, and the other options are:
     -s
     --array
     --bigint_as_string | --recode
     --count | --limit=LIMIT
     --pointer=JSONPOINTER
halaxa commented 1 year ago

Thanks for the hints. The count option is a good idea. The version I'm currently working on will also include tests and offer extensibility to some degree. I hope the PR will land soon. I'll appreciate your feedback then.

pkoppstein commented 1 year ago

I'm adding a --version option to jm and would like to include the JSON Machine version number, but I don't see an easy way to report it reliably. Searching for the version string in CHANGELOG.md doesn't seem either efficient or reliable.

Could perhaps JSON Machine provide a function to report its version number?