โซลูชันส่วนหัวเดี่ยวอย่างง่ายสำหรับการแยกวิเคราะห์ JSON ใน C และ C ++
JSON ถูกแยกวิเคราะห์เป็นบัฟเฟอร์การจัดสรรเดี่ยวแบบอ่านอย่างเดียว
คอมไพเลอร์ที่รองรับปัจจุบันคือ GCC, Clang และ MSVC
แพลตฟอร์มที่รองรับปัจจุบันคือ Windows, Mac OS และ Linux
Just #include "json.h" in your code!
แยกวิเคราะห์สตริง JSON เป็น DOM
struct json_value_s * json_parse (
const void * src ,
size_t src_size );src - a utf-8 json string to parse.src_size - the size of src in bytes. Returns a struct json_value_s* pointing the root of the json DOM.
The main struct for interacting with a parsed JSON Document Object Model (DOM) is the struct json_value_s .
struct json_value_s {
void * payload ;
size_t type ;
};payload - a pointer to the contents of the value.type - the type of struct payload points to, one of json_type_e . Note: if type is json_type_true , json_type_false , or json_type_null , payload will be 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 - a utf-8 json string to parse.src_size - the size of src in bytes.flags_bitset - extra parsing flags, a bitset of flags specified in enum json_parse_flags_e .alloc_func_ptr - a callback function to use for doing the single allocation. If NULL, malloc() is used.user_data - user data to be passed as the first argument to alloc_func_ptr .result - the result of the parsing. หากเกิดข้อผิดพลาดในการแยกวิเคราะห์สิ่งนี้จะมีข้อผิดพลาดประเภทใดและที่ไหนในแหล่งที่มา สามารถเป็นโมฆะ Returns a struct json_value_s* pointing the root of the json DOM.
The extra parsing flags that can be specified to json_parse_ex() are as follows:
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 - the default, no special behaviour is enabled.json_parse_flags_allow_trailing_comma - allow trailing commas in objects and arrays. For example, both [true,] and {"a" : null,} would be allowed with this option on.json_parse_flags_allow_unquoted_keys - allow unquoted keys for objects. For example, {a : null} would be allowed with this option on.json_parse_flags_allow_global_object - allow a global unbracketed object. For example, a : null, b : true, c : {} would be allowed with this option on.json_parse_flags_allow_equals_in_object - allow objects to use '=' as well as ':' between key/value pairs. For example, {"a" = null, "b" : true} would be allowed with this option on.json_parse_flags_allow_no_commas - allow that objects don't have to have comma separators between key/value pairs. For example, {"a" : null "b" : true} would be allowed with this option on.json_parse_flags_allow_c_style_comments - allow c-style comments ( // or /* */ ) to be ignored in the input JSON file.json_parse_flags_deprecated - a deprecated option.json_parse_flags_allow_location_information - allow location information to be tracked for where values are in the input JSON. มีประโยชน์สำหรับการแจ้งเตือนผู้ใช้ข้อผิดพลาดด้วยข้อมูลตำแหน่งที่แม่นยำเกี่ยวกับแหล่งกำเนิดดั้งเดิม When this option is enabled, all json_value_s* 's can be casted to json_value_ex_s* , and the json_string_s* of json_object_element_s* 's name member can be casted to json_string_ex_s* to retrieve specific locations on all the values and keys. หมายเหตุตัวเลือกนี้จะเพิ่มงบประมาณหน่วยความจำที่จำเป็นสำหรับ DOM ที่ใช้ในการบันทึก JSONjson_parse_flags_allow_single_quoted_strings - allows strings to be in 'single quotes' .json_parse_flags_allow_hexadecimal_numbers - allows hexadecimal numbers to be used 0x42 .json_parse_flags_allow_leading_plus_sign - allows a leading '+' sign on numbers +42 .json_parse_flags_allow_leading_or_trailing_decimal_point - allows decimal points to be lead or trailed by 0 digits .42 or 42. .json_parse_flags_allow_inf_and_nan - allows using infinity and NaN identifiers Infinity or NaN .json_parse_flags_allow_multi_line_strings - allows strings to span multiple lines.json_parse_flags_allow_simplified_json - allow simplified JSON to be parsed. Simplified JSON เป็นการเปิดใช้งานชุดของตัวเลือกการแยกวิเคราะห์อื่น ๆ ดูบล็อก Bitsquid แนะนำสิ่งนี้ที่นี่json_parse_flags_allow_json5 - allow JSON5 to be parsed. JSON5 เป็นการเปิดใช้งานชุดตัวเลือกการแยกวิเคราะห์อื่น ๆ ดูเว็บไซต์ที่กำหนดส่วนขยายนี้ที่นี่ json_parseLets say we had the JSON string '{"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 - returns a value as a string, or null if it wasn't a string.json_value_as_number - returns a value as a number, or null if it wasn't a number.json_value_as_object - returns a value as an object, or null if it wasn't an object.json_value_as_array - returns a value as an array, or null if it wasn't an array.json_value_is_true - returns non-zero is a value was true, zero otherwise.json_value_is_false - returns non-zero is a value was false, zero otherwise.json_value_is_null - returns non-zero is a value was null, zero otherwise.มาดูตัวอย่างเดียวกันจากด้านบน แต่ใช้ตัวทำซ้ำผู้ช่วยเหล่านี้แทน:
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 มีความสุขมากขึ้น
If you want to extract a value from a DOM into a new allocation then json_extract_value and json_extract_value_ex are you friends. These functions let you take any value and its subtree from a DOM and clone it into a new allocation - either a single malloc or a user-provided allocation region.
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/