這裏增長一項無聊的對比圖,看客自斟。
===================================
【rui_maciel/mjson】
Last Update:2013-05-15(最新版本爲 mjson-1.5 發佈日期爲 2012-08-22)
description
M's JSON parser is a small JSON parser written in ISO C which enables the user to handle information described by the JSON data interchange format.
mjson 解析器是一個使用 ISO C 實現的小型 JSON 解析器。
M's JSON parser is a library entirely written in ISO C which offers a way to access data that has been encoded according to the JSON document format. This is accomplished by offering programmers an easy and convenient way to parse any given JSON document and, based on the information which is extracted from these documents, creating a document tree structure that reflects exactly the contents of each JSON document.
After a JSON document is parsed, all a programmer needs to do to access the information stored in a JSON document is to traverse the tree structure which was created by the parser and access any attribute associated with any JSON value.
Once this information is accessed, the memory which has been allocated to the JSON document tree structure can be safely freed.
做者提供的測試用例:examples(存在一些問題,須要自行調整代碼)
一句話總結:
- 該庫能夠很方便的集成到其餘項目中,支持跨平臺;
- 該庫支持 SAX-like 解析;支持從文本文件中按行獲取數據進行解析;
- 支持 UTF-8;
- 支持 pretty 格式和 raw 格式的 json 數據相互轉換;
- 針對 json 數據中特定節點數據的搜索功能基本不可用(這個比較噁心)
===================================
【william/libjson】
Last Update:2013-09-27
description
json.c is a JSON C library that supports path autovivification and conversion. Autovivified and converted paths greatly simplify manipulation of JSON trees, allowing one to discard most bothersome boilerplate code.
json.c 做爲 JSON 的 C 庫實現,支持 path autovivification 和 conversion 功能。這兩項功能極大的簡化了針對 JSON 樹結構上的各類操做,容許你在代碼編寫過程當中避免大量讓人討厭的「樣板」代碼。
Because "JSON schema" is something of an oxymoron, the library makes the presumption that you mean what you say; that the schema is whatever the code does. If you attempt to write through a non-existent or type-incompatible path, the library fixes the tree to accomodate the request, rather than punt back to the caller. Likewise, a read through a non-existent or type-incompatible path returns a sane, default value—specifically 0 or an empty string.
由於「JSON schema」是一個容易讓人搞不清楚的東西,因此在該庫的實現中做了以下假定:你怎樣描述就是怎樣的結果;代碼的動做決定 schema 的模樣。若是你企圖在一個不存在,或者類型不兼容的 json 路徑上寫穿(能夠理解爲強迫寫),該庫會按照你的 request 對樹結構進行相應的修正,而不是什麼都不幹就返回到調用者處。一樣地,對一個不存在的,或者類型不兼容的 json 路徑進行讀穿,將會獲得恆定不變的默認值 - 通常是 0 或者空字串。
In addition, a stack interface allows changing the current root. This makes descending up and down the tree more convenient, and eases abstraction as code which reads or writes subtrees need not be concerned with the path to that particular subtree.
另外,經過 stack interface 能夠方便的更改當前的 root 位置。這也使得在樹結構上進行上下移動變得更加容易,並提供了更爲簡單的代碼級抽象 -- 在讀或者寫子樹結構時無需關心與該子樹對應的路徑。
Both the parser and composer are restartable and operate over a series of caller provided input and output buffers. This is intended to ease integration with networked I/O environments, particularly non-blocking environments. JSON data can be both parsed and composed byte-by-byte without any intermediate, internal buffers. No callback schemes are used as half measures, so the user code needn't arbitrarily split blocks of code between different halves of a parse or compose operation.
解析器和生成器均具備 restartable 的特色,能夠同時爲不止一個調用者提供輸入輸出緩衝。這個設計的目的是爲了簡化在網絡 I/O 環境應用時的集成工做,特別是用於非阻塞環境下。JSON 數據能夠按逐字節方式解析和生成,而且無需任何中間或者內部緩衝。回調處理被設計成不會在 JSON 數據解析和生成過程當中同時觸發,故用戶代碼塊的執行不會所以出現從解析部分變換到生成部分的過程,反之亦然。
json.c is implemented in a single source file, along with a companion header which defines the API. The only dependency is llrb.h.
該 json 庫主要由 json.c 和 json.h 構成,惟一的外部依賴是 llrb.h 文件。
usage
用法
The API is broken up into three main sections. The grouping of declarations in the header reflects this, and makes for relatively easy perusal.
API 主要分紅 3 個主要部分。能夠從頭文件中的分組聲明看出。
The core API consists of routines to construct a JSON context object, and to parse and compose JSON data.
核心 API 包括:構造 JSON 上下文對象的 API、解析 JSON 數據的 API、生成 JSON 數據的 API。
The second set consists of routines to manipulate JSON value objects directly, although this is mostly used internally.
第二部分包括:直接操做 JSON value 對象的 API(大部分狀況下僅在庫內部調用)。
The third consists of routines which access and manipulate the JSON tree through a path expression. Objects are accessed with a dot ("."), arrays indexed by square brackets ("[", "]"). String interpolation is accomplished using the special character "$" and index interpolation using "#", along with their respective arguments (char pointer or int) in the passed variable argument list. The syntax uses the standard backslash escaping protocol.
第三部分包括:經過路徑表達式訪問和操做 JSON 樹結構的 API。object 的訪問經過點操做符(「.」);array 的索引經過中括號實現(「[」,「]」);字符串內的插值操做使用特殊字符「$」;array 索引的插值操做使用特殊字符「#」。最後兩個插值操做須要同時提供相應的參數列表(字符串指針或者整形變量)。總體的語法規則使用了標準的反斜槓轉義協議。
The core API returns errors directly, while most of the remainder signal errors using longjmp(). The macro pair json_enter()/json_leave() are analogous to try/finally. However, thrown errors need not be caught; aren't thrown unless an exception context is provided; consistency is maintained if errors are ignored; and a sane and safe value always returned when reading a value. json.c error codes are negative numbers in the range JSON_EBASE to JSON_ELAST, and communicated along with system error codes through a single int value. POSIX guarantees system codes to be positive.
在設計上,核心 API 會直接返回錯誤信息,而其餘大部分 API 是採用 longjmp() 方式來通知錯誤的發生。json_enter()/json_leave() 這對宏功能上相似與 try/finally 。然而,拋出的錯誤是不須要進行捕獲的;僅在具備異常上下文的時候拋出異常;在錯誤信息被忽略的狀況下可以保證數據的一致性;在讀值操做中總能保證返回一個不會變化的安全值。json.c 中的錯誤碼是範圍在 JSON_EBASE 到 JSON_ELAST 之間的負數。
A small regression utility can be built from the source. Defining JSON_MAIN will expose the main() definition, and until documentation can be written usage can be gleaned from the implementation of the utility commands. The utility requires libffi in order to dynamically construct calls with interpolating path expressions, useful for exercising path evaluation. However, the include path for the libffi header is hit-and-miss. And a bug report has been filed with Apple because clang chokes on their broken ffi.h header.
小型的迴歸測試類應用能夠直接在該源文件的基礎上進行。
一句話總結:
庫自己支持的功能絕對有亮點,但因爲原做者對 C99 標準貫徹的很是堅定,因此將上述代碼移植到不支持 C99 標準的 VS 上很是困難。
===================================
【Jansson】
Last Update:2013-09-23
Description:
Jansson is a C library for encoding, decoding and manipulating JSON data. It features:
Jansson 以 C 庫的形式提供了針對 JSON 數據編碼、解碼和操縱的能力。
- Simple and intuitive API and data model 提供簡單直觀的 API 以及數據模型
- Comprehensive documentation 全面的文檔
- No dependencies on other libraries 無第三方庫依賴
- Full Unicode support (UTF-8) 對 Unicode 的徹底支持(UTF-8 等)
- Extensive test suite 完整的測試集
- Jansson is licensed under the MIT license. 以 MIT 許可證發佈
===================================
【vincenthz/libjson】
Last Update:2013-09-23(上一個 stable 版本爲 v0.8,發佈於 2010-09-04)
Description:
libjson is a small C library and small codebase that packs an efficient parser and a configurable printer. libjson is covered by the LGPLv2 license, or at your option the LGPLv3 license.
一個小巧的 C 庫,封裝了高效的解析器和可配置的 printer 用於輸出。
libjson is a simple library without any dependancies to parse and pretty print the json format (RFC4627).
無第三方庫依賴。
Here's the feature of libjson:
- Interruptible parser: get the JSON data to the parser any way you want; by appending char by char, or string chunks, the input reading is completely left to the caller.
- No object model integrated: easy integration with any model by the means of a simple callback.
- Small codebase: handcoded parser and efficient factorisation make the code smalls.
- Fast: use efficient code, and small parsing tables to not do any extra work and remains as fast and efficient as possible.
- Full JSON support: tested through a small and precise testsuite.
- No native conversion: callback only string of data and leave the actual representation of data to the caller
- Supports putting limits on nesting level. security against DoS over very deep data.
- Supports putting limits on data (string/int/float) size. security against DoS over very large data object.
- Optionally support YAML/python comments and C comments.
- Supports projects-specific allocation functions to integrate completely with projects
- jsonlint utility provided with the library to verify, or reformat json stream. also useful as example on how to use the library.
libjson parser is an interruptible handcoded state parse. the parser takes character or string as input. Since it's interruptible, it's up to the user to feed the stream to the parser, which permits complete flexibility as to whether the data is coming from a pipe, a network socket, a file on disk, a serial line, or crafted by the user.
該 libjson 庫中提供的 json 解析器支持解析過程可中斷的特性,即解析器既能夠按字符處理,也能夠按字串處理。換句話說,使用者能夠爲該解析器指定任意的、待解析的、數據流的源,如從 pipe、網絡socket、磁盤文件、串行線路,以及使用者精心設計的什麼東東。
The parser doesn't create an object tree for you, but each time it comes up with an element in this data, it just callback to the user with the type found and for some type, the data associated with it. It can be compared to the SAX way of XML, hence it's called SAJ (Simple API for JSon).
該解析器不會爲你建立 object 樹結構,而是經過回調函數每次向你提供一個屬於該樹中的元素,包括元素的類型,以及可能存在的數據值。這種能力與 XML 中的 SAX 相似,因此能夠稱做 SAJ(Simple API for JSon)。
The parser doesn't convert number to any native C format, but instead callback with a string that is a valid JSon number. JSon number can be of any size, so that's up to user to decide whetever or not, the number can map to native C type int32_t, int64_t, or a complex integer type. As well the user has a choice to refuse the integer at the callback stage if the length is not appropriate.
該解析器沒有對 json 中的 number 類型值進行本地 C 格式的轉換,而是在回調函數中以字串的形式將 number 類型值返回。json 中的 number 類型沒有對數據值的範圍作限制,因此 number 類型值可否被正確映射到本地 C 類型,如 int32_t、int64_t,或更復雜的整數類型,由用戶本身來肯定。因此,這也就讓用戶有機會在調用回調函數處理時,經過斷定當前 number 數值可否被正確處理,決定是拒絕仍是接收該數據結果。
The parser optionally allows YAML and/or C comments to be ignored if the config structure is set accordingly, otherwise a JSON_ERROR_COMMENT_NOT_ALLOWED is returned.
一句話總結:
沒有提供搜索特定值的接口。
===================================
【jsoncpp-src-0.5.0.tar.gz】 php
Last Update: 2012-12-20 -> 2013-09-23
Description: html
jsoncpp is an implementation of a JSON (http://json.org) reader and writer in C++. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
反面觀點: node
Posted by OpenID User — 2012-03-14 It's really good apart from the lack of UNICODE support, I'd have to change everything to TCHAR and std::wstring :(
Posted by Sergey Kolomenkin — 2011-02-09 It corrupts heap memory! Developers don't correct even critical bugs for a months (see bug list).
Posted by hypernewbie — 2010-06-16 absolutely horrendous. making a Json::Value global would result in a pure virtual function call crash upon exit. If you try asInt() when the value is a string, you'd get a huge assertion fail. Random access violations everywhere.
網友經驗: python
1.《json筆記-jsoncpp一個全局對象的bug》,指出因爲全局對象構造和析構時可能引起崩潰的問題。2.《C++ Json》,文檔整理的比較乾淨,對 Value 值類型詳細進行了代碼舉例。
3.《JsonCpp使用優化》,從 編譯、使用方式、代碼,以及編譯成哪一種庫幾方面說明優化問題。
4.《JsonCpp簡介》,能夠看下工程組織方式,和測試例子。 linux
實踐經驗: git
1.解壓後的包內容以下: github
2.查看 README 文件,並按其中的要求下載並安裝配置 scons-local 和 python 。其中 scons-local 解壓後,令 scons.py 與 README 位於同級目錄。以下圖: shell
3.執行工程配置和編譯命令: express
python scons.py platform=PLTFRM [TARGET]
其中
where PLTFRM may be one of:
suncc Sun C++ (Solaris)
vacpp Visual Age C++ (AIX)
mingw
msvc6 Microsoft Visual Studio 6 service pack 5-6
msvc70 Microsoft Visual Studio 2002
msvc71 Microsoft Visual Studio 2003
msvc80 Microsoft Visual Studio 2005
linux-gcc Gnu C++ (linux, also reported to work for Mac OS X)
and TARGET may be:
check: build library and run unit tests.
另外,若是你要編譯 VS2008 或 VS2010 的庫,則須要在 Sconstruct 文件中作相應的修改,很簡單的。
4.執行命令後的結果,以下圖: json
==================================
【json-parser-master.zip】
Last Update: 2012-12-09 -> 2013-10-09
README.md
Very low footprint JSON parser written in portable ANSI C. BSD licensed with no dependencies (i.e. just drop the C file into your project) Never recurses or allocates more memory than it needs Very simple API with operator sugar for C++
我的總結:
1.目錄結構
2.提供的 API
json_parse 封裝了對 json_parse_ex 的調用
json_value * json_parse(const json_char * json);
json_parse_ex 封裝了對 new_value 和 json_value_free 的調用
json_value * json_parse_ex(json_settings * settings,
const json_char * json, char * error);
new_value 封裝了對 json_alloc 的調用
static int new_value(json_state * state, json_value ** top,
json_value ** root, json_value ** alloc, json_type type);
釋放json值
void json_value_free(json_value *);
內存分配處理
static void * json_alloc(json_state * state, unsigned long size, int zero);
==================================
【rapidjson-0.11.zip】
Last Update: 2012-11-16 -> 無更新
Description
Rapidjson is an attempt to create the fastest JSON parser and generator.
- Small but complete. Supports both SAX and DOM style API. SAX parser only a few hundred lines of code.
- Fast. In the order of magnitude of strlen(). Optionally supports SSE2/SSE4.2 for acceleration.
- Self-contained. Minimal dependency on standard libraries. No BOOST, not even STL.
- Compact. Each JSON value is 16 or 20 bytes for 32 or 64-bit machines respectively (excluding text string storage). With the custom memory allocator, parser allocates memory compactly during parsing.
- Full RFC4627 compliance. Supports UTF-8, UTF-16 and UTF-32.
- Support both in-situ parsing (directly decode strings into the source JSON text) and non-destructive parsing (decode strings into new buffers).
- Parse number to int/unsigned/int64_t/uint64_t/double depending on input
- Support custom memory allocation. Also, the default memory pool allocator can also be supplied with a user buffer (such as a buffer allocated on user's heap or programme stack) to minimize allocation.
其餘網友的點評:《推薦一款cpp解析json工具-rapidjson》
其中給出的結論以下。
優勢:
- 依賴庫不多;
- 輕量級;
- 對於 Dom 模型層級關係表述的很清楚。
缺點:
- 只支持標準的 json 格式,一些非標準的 json 格式不支持;
- 缺乏一些比較通用的接口,再解析的時候須要本身再封裝一層,不然代碼量將會很大。
庫做者同時提供了比較詳細的用戶手冊供參考。
最後引用一段用戶手冊中的說明:
rapidjson is a header-only library. That means, the only thing to be done is to copy rapidjson/include/rapidjson and its sub-directories to your project or other include paths.
==================================
【
json-c-0.10.tar.gz
】
Last Update: 2012-05-30 -> 2013-09-11
Overview
JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects.
==================================
【cJSONFiles.zip】
Last Update: 2011-10-10 -> 2013-08-19
Description
An ultra-lightweight, portable, single-file, simple-as-can-be ANSI-C compliant JSON parser, under MIT license.
反面觀點:
Posted by zhou meng — 2010-04-13 Lightweight, easy to use and well documented. Good work! Only not support wide byte like japanese,chinese
Posted by Otto Linnemann — 2010-01-24 Lightweight, easy to use and well documented. Good work! Only memory management should be improved!
==================================
【ninja9578/libjson_7.6.1.zip】
Last Update: 2012-06-25 -> 沒有更新
Description
A JSON reader and writer which is super-effiecient and usually runs circles around other JSON libraries. It's highly customizable to optimize for your particular project, and very lightweight. For Windows, OSX, or Linux. Works in any language.
Features
- Lazy or aggressive JSON Parsing
- 100% JSON compliant
- Language independent C interface
- C++ interface
- Test Suite and Example Projects
- C and Bash style comment support
- Automated install via make
- cplusplus.com - style documentation
- Streaming ability
- Security to prevent various forms of Denial of Service
反面意見:
Posted by hu junjie — 2011-07-14
many bugs..
1:
case JSON_TEXT('\v'): //vertical tab res += JSON_TEXT("\\v"); break; case JSON_TEXT('\''): //apostrophe res += JSON_TEXT("\\\'"); break; in rfc4627, '\v', '\'' NOT NEED escape, if do this, other jsonlib will crash, ex: jsoncpp..
2:
json SHOULD transfer in utf8, if define UNICODE, will transfer unicode, if not define UNICODE, libjson can not parse string contant \uxxyy when xx not equ 0
3:
in code JSONWork.cpp *runner++ = (*++p == JSON_TEXT('\"')) ? JSON_TEXT('\1') : *p; //an escaped quote will reak havoc will all of my searching functions, so change it into an illegal character in JSON for convertion later on but iif the node no fetch yet, will not convertion later on, for example: const char *str = "{ \"mt\":\"\\\"str\\\"\" }" // str={"mt":"\"str\""} JSONNode obj = libjson::parse(str); json_string objstr = obj.write(); printf("%s\n", objstr.c_str()); WILL OUTPUT {"mt","\1str\1"}
=============================================
另外,有網友專門對 C 代碼的 JSON 解析庫進行了比較,參考文章:《四種json c parser的兼容性比較》。
其給出的結論以下:
- 從各個經過的文件數量看, json-c 兼容性是最好的;
- mjson 和 json-parser 只有一兩個文件,適合放到單個模塊裏用;
- json-c 採用 autoconf , 適合庫形式;
- yajl 採用 cmake , 測試作的比較好;
- mjson 和 yajl 都支持 sax 事件解析方法。