shell下的解析json的工具:jcat

碼頭工人 9年前發布 | 28K 次閱讀 jcat JSON開發包

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

  1. 支持指定路徑解析,打印指定路徑下的所有對象信息
  2. 支持宏路徑,可以級聯宏替換
  3. 使用tbox進行了跨平臺支持,預編譯版本直接可以在./tbox/tool目錄下找到,因為新版tbox makefile架構就是采用jcat來解析*.pkg/manifest.json清單文件
  4. </ol>

    使用方式拿polarssl.pkg/manifest.json的舉例:

    {
        "format":
        {
            "name":         "The TBOOX Package Format"
        ,   "version":      "v1.0.1"
        ,   "website":      "
    

    , "package": { "name": "The PolarSSL Library" , "website": "

    , "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"
        }
    }
    

    }</pre>

    上述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" }

    };

    /* //////////////////////////////////////////////////////////////////////////////////////

 本文由用戶 碼頭工人 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!