實現json解析工具jcat

來源:互聯網
上載者:User

標籤:

jcat是一個shell下的解析json的工具,具有以下功能:

  1. 支援指定路徑解析,列印指定路徑下的所有對象資訊
  2. 支援宏路徑,可以級聯宏替換
  3. 使用tbox進行了跨平台支援,先行編譯版本直接可以在./tbox/tool目錄下找到,因為新版tbox makefile架構就是採用jcat來解析*.pkg/manifest.json資訊清單檔

使用方式拿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":      "http://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"        }    }}

上述manifest.json中,以$開頭的字串均為宏路徑,例如:$.compiler.default,用來引用其他地方的配置資料,減小配置冗餘

執行jcat, 擷取 .compiler.mac.x64.debug 路徑的內容

./tool/jcat/jcat --filter=.compiler.mac.x64.debug ./pkg/polarssl.pkg/manifest.json

返回結果如下:

{"incpath":"","incflags":"","libs":"polarssl","libflags":"","libpath":""}

是不是很方便?解析過程可以遞迴處理替換宏路徑,返回真實的資料。其他詳細使用方式,可以通過如下命令擷取:

./tool/jcat/jcat --help

看了使用過程,是不是覺得實現這樣一個jcat很複雜呢,其實非常簡單,只要使用TBOX的object庫,可以非常方便的實現它,下面就曬下jcat的代碼吧:

#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\":      31415926\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_object_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(option, "filter"), tb_true);                    // dump 資料對象,這裡主要為了過濾 字串內容的 ""                    // 否則直接使用tb_object_dump會更簡單,只需一行代碼                    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] = ‘\0‘;                            }                            // 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;}

簡單吧,就只要一百行代碼,還支援詳細的命令列選項支援。

附帶一句:其實jcat同時還可以支援解析xml和plist哦,因為他用了object模組的多資料格式探測功能,完全可以自動解析不同格式的資料,還能方便擴充自己的資料格式。

  • TBOX項目詳情
  • TBOX項目源碼
  • TBOX項目文檔

實現json解析工具jcat

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.