2012年1月29日 星期日

JSON-C


JSON-C是一套用C寫的JSON format的parser和generator,可以將JSON字串正確parse,並且存成json_object進行操作(新增/刪除/修改),也可以將json_object轉成JSON字串,個人還蠻推薦的。

官方網站:JSON-C

安裝步驟
brook@vista:~/src$ git clone https://github.com/json-c/json-c.git json-c
Cloning into json-c...
remote: Counting objects: 448, done.
remote: Compressing objects: 100% (169/169), done.
remote: Total 448 (delta 315), reused 405 (delta 274)
Receiving objects: 100% (448/448), 125.68 KiB | 76 KiB/s, done.
Resolving deltas: 100% (315/315), done.
brook@vista:~/src$ cd json-c/
brook@vista:~/src/json-c$ ./autogen.sh
autoreconf: Entering directory `.'
autoreconf: configure.in: not using Gettext
autoreconf: running: aclocal 
autoreconf: configure.in: tracing
autoreconf: running: libtoolize --install --copy
libtoolize: Consider adding `AC_CONFIG_MACRO_DIR([m4])' to configure.in and
libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree.
libtoolize: Consider adding `-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
autoreconf: running: /usr/bin/autoconf
autoreconf: running: /usr/bin/autoheader
autoreconf: running: automake --add-missing --copy --no-force
autoreconf: Leaving directory `.'
brook@vista:~/src/json-c$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
...
brook@vista:~/src/json-c$ make
make  all-am
make[1]: Entering directory `/home/brook/src/json-c'
/bin/bash ./libtool --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I.    -Wall -Wwrite-strings -Werror -std=gnu99 -D_GNU_SOURCE -D_REENTRANT -g -O2 -MT arraylist.lo -MD -MP -MF .deps/arraylist.Tpo -c -o arraylist.lo arraylist.c
...
brook@vista:~/src/json-c$ make check
make  test1 test2 test4 test_parse_int64 test_null test_cast
make[1]: Entering directory `/home/brook/src/json-c'
gcc -DHAVE_CONFIG_H -I.    -Wall -Wwrite-strings -Werror -std=gnu99 -D_GNU_SOURCE -D_REENTRANT -g -O2 -MT test1.o -MD -MP -MF .deps/test1.Tpo -c -o test1.o test1.c
test1.c: In function ‘sort_fn’:
test1.c:14:8: error: assignment discards ‘const’ qualifier from pointer target type [-Werror]
test1.c:15:8: error: assignment discards ‘const’ qualifier from pointer target type [-Werror]
cc1: all warnings being treated as errors

make[1]: *** [test1.o] Error 1
make[1]: Leaving directory `/home/brook/src/json-c'
make: *** [check-am] Error 2
brook@vista:~/src/json-c$ sed -i 's/-Werror //' Makefile
brook@vista:~/src/json-c$ make check
make  test1 test2 test4 test_parse_int64 test_null test_cast
make[1]: Entering directory `/home/brook/src/json-c'
...
brook@vista:~/src/json-c$ ./test1
my_string= 
my_string.to_string()="\t"
my_string=\
my_string.to_string()="\\"
my_string=foo
my_string.to_string()="foo"
my_int=9
my_int.to_string()=9
my_array=
 [0]=1
 [1]=2
 [2]=3
 [3]=null
 [4]=5
...


Parser
可以利用json_tokener_parse()直接將字串轉成json_object,或是利用json_object_from_file()直接將檔案轉成json_object。
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#include "json.h"


int main(int argc, char **argv)
{
  json_object *new_obj;

  MC_SET_DEBUG(1);

  new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
  json_object_put(new_obj);

  return 0;
}


Generator基本上就是用json_object_to_json_string()將json_object轉成字串。

更多的範例可以參考source code裡面的test1.c。