簡單的單個標頭解決方案,用於在C和C ++中解析JSON。
JSON被解析為僅讀取的單個分配緩衝區。
當前受支持的編譯器是GCC,Clang和MSVC。
當前受支持的平台是Windows,Mac OS和Linux。
只需在您的代碼中#include "json.h" !
將JSON字符串解析到DOM中。
struct json_value_s * json_parse (
const void * src ,
size_t src_size );src -UTF -8 JSON字符串進行解析。src_size字節中的src大小。返回一個指向JSON DOM的根的struct json_value_s* 。
與解析的JSON文檔對像模型(DOM)進行交互的主要結構是struct json_value_s 。
struct json_value_s {
void * payload ;
size_t type ;
};payload - 指向值內容的指針。type - json_type_e之一的結構payload點的類型。注意:如果類型是json_type_true , json_type_false或json_type_null ,則有效載荷將為null。將JSON字符串擴展到DOM。
struct json_value_s * json_parse_ex (
const void * src ,
size_t src_size ,
size_t flags_bitset ,
void * ( * alloc_func_ptr )( void * , size_t ),
void * user_data ,
struct json_parse_result_s * result );src -UTF -8 JSON字符串進行解析。src_size字節中的src大小。flags_bitset額外的解析標誌, enum json_parse_flags_e中指定的標誌的位置。alloc_func_ptr用於執行單個分配的回調函數。如果null,則使用malloc() 。user_data將以第一個參數傳遞給alloc_func_ptr用戶數據。result - 解析的結果。如果發生解析錯誤,這將包含哪種類型的錯誤以及在源中發生的何處。可以是零的。返回一個指向JSON DOM的根的struct json_value_s* 。
可以指定到json_parse_ex()額外解析標誌如下:
enum json_parse_flags_e {
json_parse_flags_default = 0 ,
json_parse_flags_allow_trailing_comma = 0x1 ,
json_parse_flags_allow_unquoted_keys = 0x2 ,
json_parse_flags_allow_global_object = 0x4 ,
json_parse_flags_allow_equals_in_object = 0x8 ,
json_parse_flags_allow_no_commas = 0x10 ,
json_parse_flags_allow_c_style_comments = 0x20 ,
json_parse_flags_deprecated = 0x40 ,
json_parse_flags_allow_location_information = 0x80 ,
json_parse_flags_allow_single_quoted_strings = 0x100 ,
json_parse_flags_allow_hexadecimal_numbers = 0x200 ,
json_parse_flags_allow_leading_plus_sign = 0x400 ,
json_parse_flags_allow_leading_or_trailing_decimal_point = 0x800 ,
json_parse_flags_allow_inf_and_nan = 0x1000 ,
json_parse_flags_allow_multi_line_strings = 0x2000 ,
json_parse_flags_allow_simplified_json =
( json_parse_flags_allow_trailing_comma |
json_parse_flags_allow_unquoted_keys |
json_parse_flags_allow_global_object |
json_parse_flags_allow_equals_in_object |
json_parse_flags_allow_no_commas ),
json_parse_flags_allow_json5 =
( json_parse_flags_allow_trailing_comma |
json_parse_flags_allow_unquoted_keys |
json_parse_flags_allow_c_style_comments |
json_parse_flags_allow_single_quoted_strings |
json_parse_flags_allow_hexadecimal_numbers |
json_parse_flags_allow_leading_plus_sign |
json_parse_flags_allow_leading_or_trailing_decimal_point |
json_parse_flags_allow_inf_and_nan |
json_parse_flags_allow_multi_line_strings )
};json_parse_flags_default默認,未啟用特殊行為。json_parse_flags_allow_trailing_comma允許對象和數組中的逗號。例如,在此選項上,允許[true,]和{"a" : null,} 。json_parse_flags_allow_unquoted_keys允許對象的無引用鍵。例如, {a : null}將在此選項上允許。json_parse_flags_allow_global_object允許全局未支撐對象。例如, a : null, b : true, c : {}將在此選項上允許。json_parse_flags_allow_equals_in_object允許對象使用'='以及':'鍵/值對之間。例如, {"a" = null, "b" : true}在此選項上允許。json_parse_flags_allow_no_commas允許對像在密鑰/值對之間不需要逗號分隔符。例如, {"a" : null "b" : true}在此選項上允許。json_parse_flags_allow_c_style_comments允許在輸入JSON文件中忽略C -Style註釋( //或/* */ )。json_parse_flags_deprecated不推薦的選項。json_parse_flags_allow_location_information允許在輸入json中的值跟踪位置信息。非常有助於提醒用戶到具有與原始源有關的精確位置信息的錯誤。當啟用此選項時,所有json_value_s*的s可以投放到json_value_ex_s* ,並且可以將json_object_element_s* *的JSON_STRING_S**的json_string_s*命名為json_string_ex_s* ,以在所有值和鑰匙上檢索特定位置。請注意,此選項將增加用於記錄JSON的DOM所需的內存預算。json_parse_flags_allow_single_quoted_strings允許字符串在'single quotes'中。json_parse_flags_allow_hexadecimal_numbers允許使用六核數字0x42 。json_parse_flags_allow_leading_plus_sign允許數字+42上的領先' +'符號。json_parse_flags_allow_leading_or_trailing_decimal_point允許小數點以0位數為.42或42. 。json_parse_flags_allow_inf_and_nan允許使用Infinity和NAN標識符Infinity或NaN 。json_parse_flags_allow_multi_line_strings允許字符串跨越多行。json_parse_flags_allow_simplified_json允許解析簡化的JSON。簡化的JSON是一組其他解析選項的啟用。請參閱Bitsquid博客在此處介紹此內容。json_parse_flags_allow_json5允許JSON5解析。 JSON5是一組其他解析選項的支持。請參閱此處定義此擴展名的網站。 json_parse解析假設我們有JSON字符串'{“ A”:true,“ b”:[false,null,“ foo”]}' 。要進入分析的JSON的每個部分:我們要做:
const char json [] = "{"a" : true, "b" : [false, null, "foo"]}" ;
struct json_value_s * root = json_parse ( json , strlen ( json ));
assert ( root -> type == json_type_object );
struct json_object_s * object = ( struct json_object_s * ) root -> payload ;
assert ( object -> length == 2 );
struct json_object_element_s * a = object -> start ;
struct json_string_s * a_name = a -> name ;
assert ( 0 == strcmp ( a_name -> string , "a" ));
assert ( a_name -> string_size == strlen ( "a" ));
struct json_value_s * a_value = a -> value ;
assert ( a_value -> type == json_type_true );
assert ( a_value -> payload == NULL );
struct json_object_element_s * b = a -> next ;
assert ( b -> next == NULL );
struct json_string_s * b_name = b -> name ;
assert ( 0 == strcmp ( b_name -> string , "b" ));
assert ( b_name -> string_size == strlen ( "b" ));
struct json_value_s * b_value = b -> value ;
assert ( b_value -> type == json_type_array );
struct json_array_s * array = ( struct json_array_s * ) b_value -> payload ;
assert ( array -> length == 3 );
struct json_array_element_s * b_1st = array -> start ;
struct json_value_s * b_1st_value = b_1st -> value ;
assert ( b_1st_value -> type == json_type_false );
assert ( b_1st_value -> payload == NULL );
struct json_array_element_s * b_2nd = b_1st -> next ;
struct json_value_s * b_2nd_value = b_2nd -> value ;
assert ( b_2nd_value -> type == json_type_null );
assert ( b_2nd_value -> payload == NULL );
struct json_array_element_s * b_3rd = b_2nd -> next ;
assert ( b_3rd -> next == NULL );
struct json_value_s * b_3rd_value = b_3rd -> value ;
assert ( b_3rd_value -> type == json_type_string );
struct json_string_s * string = ( struct json_string_s * ) b_3rd_value -> payload ;
assert ( 0 == strcmp ( string -> string , "foo" ));
assert ( string -> string_size == strlen ( "foo" ));
/* Don't forget to free the one allocation! */
free ( root );除了使其通過生產的JSON DOM迭代更好的迭代外,沒有其他功能可以提供其他目的:
json_value_as_string將值返回為字符串,如果不是字符串,則返回值。json_value_as_number返回一個數字值,如果不是數字,則返回null。json_value_as_object返回一個值作為對象,如果不是對象,則返回值。json_value_as_array返回值為數組的值,如果不是數組,則返回值。json_value_is_true返回非零是一個值,否則為零。json_value_is_false返回non -Zero是一個值是錯誤的,否則為零。json_value_is_null返回non -Zero是一個值,否則為零。讓我們從上面查看相同的示例,但使用這些輔助迭代器:
const char json [] = "{"a" : true, "b" : [false, null, "foo"]}" ;
struct json_value_s * root = json_parse ( json , strlen ( json ));
struct json_object_s * object = json_value_as_object ( root );
assert ( object != NULL );
assert ( object -> length == 2 );
struct json_object_element_s * a = object -> start ;
struct json_string_s * a_name = a -> name ;
assert ( 0 == strcmp ( a_name -> string , "a" ));
assert ( a_name -> string_size == strlen ( "a" ));
struct json_value_s * a_value = a -> value ;
assert ( json_value_is_true ( a_value ));
struct json_object_element_s * b = a -> next ;
assert ( b -> next == NULL );
struct json_string_s * b_name = b -> name ;
assert ( 0 == strcmp ( b_name -> string , "b" ));
assert ( b_name -> string_size == strlen ( "b" ));
struct json_array_s * array = json_value_as_array ( b -> value );
assert ( array -> length == 3 );
struct json_array_element_s * b_1st = array -> start ;
struct json_value_s * b_1st_value = b_1st -> value ;
assert ( json_value_is_false ( b_1st_value ));
struct json_array_element_s * b_2nd = b_1st -> next ;
struct json_value_s * b_2nd_value = b_2nd -> value ;
assert ( json_value_is_null ( b_2nd_value ));
struct json_array_element_s * b_3rd = b_2nd -> next ;
assert ( b_3rd -> next == NULL );
struct json_string_s * string = json_value_as_string ( b_3rd -> value );
assert ( string != NULL );
assert ( 0 == strcmp ( string -> string , "foo" ));
assert ( string -> string_size == strlen ( "foo" ));
/* Don't forget to free the one allocation! */
free ( root );如您所見,它使通過DOM迭代更加愉快。
如果要從DOM提取一個值為新的分配中的值,則您的朋友是json_extract_value和json_extract_value_ex 。這些功能使您可以從DOM中獲取任何值及其子樹,然後將其克隆到新的分配中 - 單個malloc或用戶提供的分配區域。
const char json [] = "{"foo" : { "bar" : [123, false, null, true], "haz" : "haha" }}" ;
struct json_value_s * root = json_parse ( json , strlen ( json ));
assert ( root );
struct json_value_s * foo = json_value_as_object ( root ) -> start -> value ;
assert ( foo );
struct json_value_s * extracted = json_extract_value ( foo );
/* We can free root now because we've got a new allocation for extracted! */
free ( root );
assert ( json_value_as_object ( extracted ));
/* Don't forget to free the one allocation! */
free ( extracted );JSON_PARSE函數一次調用Malloc,然後將單個單個分配切片以支持您可以想像的所有怪異而奇妙的JSON結構!
數據的結構始終是JSON結構(編碼原始JSON的結構),然後是數據。
這是發佈到公共領域的免費且無限制的軟件。
任何人都可以免費複製,修改,發布,使用,編譯,出售或分發此軟件,無論是源代碼表單還是作為二進制編譯的二進制,出於任何目的,商業或非商業目的以及任何方式。
在承認版權法的司法管轄區中,該軟件的作者或作者將軟件中的任何和所有版權興趣獻給公共領域。我們將這種奉獻精神獻給了整個公眾的利益,並損害了我們的繼承人和繼承人。我們打算將這種奉獻精神奉獻給根據版權法的永久性和未來權利的永久性行為。
該軟件是“原樣”提供的,沒有任何形式的明示或暗示保證,包括但不限於適銷性,特定目的的適用性和非侵權的保證。在任何情況下,作者均不應對任何索賠,損害賠償或其他責任責任,無論是在合同,侵權或其他方面的訴訟中,與軟件,使用或軟件中的使用或其他交易有關。
有關更多信息,請參閱http://unlicense.org/