Jcat is a tool for parsing JSON under the shell, with the following features:
- Supports specifying path resolution to print all object information under the specified path
- Supports macro paths, which can be replaced by cascading macros
- Cross-platform support with Tbox, precompiled versions can be found directly in the./tbox/tool directory, as the new Tbox makefile architecture uses Jcat to parse *.pkg/manifest.json manifest files
Examples of how to use Polarssl.pkg/manifest.json:
{"format": {"name": "The Tboox Package format", "Version": "v1.0.1", "website": "Http://www.tboox.org"}, "package": {"name": "The Polarssl Library", "website": "HT Tp://www.polarssl.org "}," compiler ": {" default ": {" Debug ": { "Libs": "Polarssl", "Libpath": "", "Incpath": "", "Libflags" : "", "Incflags": ""}, "release": "$.compiler.default.debug"} , "Linux": {"x64": "$.compiler.default"}, "Mac": {"x86": "$.compiler.default", "x64": "$.compiler.default"}, "Msvc": {"x86": "$.compiler.default"}, "MinGW": {"x86": "$.compiler.default"}, " Cygwin ": { "x86": "$.compiler.default"}, "ios": {"armv7": "$.compiler.default" , "armv7s": "$.compiler.default", "arm64": "$.compiler.default"}, "Android": { "ARMv5TE": "$.compiler.default", "ARMv6": "$.compiler.default"}}}
In the above Manifest.json, the string starting with $ is a macro path, for example: $.compiler.default, which refers to configuration data elsewhere, reducing configuration redundancy
Execute jcat to get the contents of the. Compiler.mac.x64.debug path
./tool/jcat/jcat--filter=.compiler.mac.x64.debug./pkg/polarssl.pkg/manifest.json
The returned results are as follows:
{"Incpath": "", "Incflags": "", "Libs": "Polarssl", "Libflags": "", "Libpath": ""}
Isn't it convenient? The parsing process can recursively process the replacement macro path and return the actual data. Other detailed usage methods can be obtained through the following commands:
./tool/jcat/jcat--help
See the use of the process, do not feel that the implementation of such a jcat is very complex, in fact very simple, as long as the use of Tbox object library, can be very convenient to implement it, the following Sun Jcat code bar:
#include "tbox/tbox.h" static tb_option_item_t g_options[] = {' F ', ' filter ', tb_option_mode_key_val , Tb_option_type_cstr, "The JSON filter\n" ". e.g\n" "\ n" "file:\n" "{\ n" "\ "string\": \ "Hello world!\" \ n "", \ "com.xxx.xxx\": \ "Hello world\" \ n "", \ "Integer\": 314159 26\n "", \ "array\": \ n "" [\ n "" \ "Hello world!\" \ n "", 31415926\n "" , 3.1415926\n "", false\n "", true\n "", {\ "string\": \ "Hello world!\"}\n " "]\n" ", \" macro\ ": \" $.array[2]\ "\ n" ", \" macro2\ ": \" $.com\\\\.xxx\\\\.xxx\ "\ n" ", \" macro3\ ": \" $.macro\ "\ n" ", \" macro4\ ": \" $.array\ "\ n" "}\n" "\ n" "Filter:\n" "1. \ ". string\": Hello world!\n "" 2. \ ". Array[1]\": 31415926\n "" 3. \ ". Array[5].string\ ": Hello world!\n" "4. \ ". com\\.xxx\\.xxx\": Hello world\n "" 5. \ ". Macro\": 3.1415926\n "" 6. \ ". Macro2\": Hello world\n "" 7. \ ". macro3\": 3.1415926\n "" 8. \ ". Macro4[0]\": \ "Hello world!\" \ n "}, {' H '," Help ", Tb_option_mode_key, Tb_option_type _bool, "Display this Help and exit"}, {'-', ' file ', Tb_option_mode_val, Tb_option_type_cstr, "The JSON file"}};/*/////////////////////////////////////////////////////////////////////////////////// * Main */tb_int_t main (tb_int_t argc, tb_char_t** argv) {//init Tbox if (!tb_init (tb_null, tb_null, 0)) return 0; init option tb_option_ref_t option = Tb_option_init ("Jcat", "Cat the JSON file", g_options); if (option) {//Done option if (tb_option_done (option, Argc-1, &argv[1])) {Done File if (tb_option_find (option, "file")) {//Load object Tb_o bject_ref_t root = Tb_object_read_from_url (tb_option_item_cstr (option, "file"); if (root) {//Done filter tb_object_ref_t object = root; if (tb_option_find (option, "Filter")) object = Tb_object_seek (Root, Tb_option_item_cstr (o Ption, "filter"), tb_true); Dump data object, where the main purpose is to filter the contents of the string ""//otherwise direct use of tb_object_dump would be simpler, just one line of code if (object) {//Done tb_char_t info[8192] = {0}; tb_long_t size = Tb_object_writ_to_data (object, (tb_byte_t*) info, sizeof (info), Tb_object_format_json | Tb_object_format_deflate); if (Size > 0) {//Strip string: "" tb_char_t* show = info; if (info[0] = = ' \ ' && info[size-1] = = ' \ ' ') {show++; Info[size-1] = ' + '; }//Trace tb_printf ("%s\n", show); }}//Exit object Tb_object_exit (root); }} else tb_option_help (option); } else tb_option_help (option); Exit option tb_option_exit (option); }//Exit Tbox tb_exit (); OK return 0;}
As simple as 100 lines of code, support for verbose command line options is also supported.
A: In fact, Jcat can also support parsing XML and plist Oh, because he used the object module of the majority of the data format detection function, can automatically parse different formats, but also easy to expand their data format.
- Tbox Project Details
- Tbox Project Source Code
- Tbox Project Documentation
Implementing the JSON parsing tool Jcat