简单的单个标头解决方案,用于在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/