, "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" }
};
/* //////////////////////////////////////////////////////////////////////////////////////
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;
}</pre>
簡單吧,就只要一百行代碼,還支持詳細的命令行選項支持。
附帶一句:其實jcat同時還可以支持解析xml和plist哦,因為他用了object模塊的多數據格式探測功能,完全可以自動解析不同格式的數據,還能方便擴展自己的數據格式。