### Struct to JSON Conversion Example Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Demonstrates the conversion of a nested C struct (Student with Hometown) to a JSON object using struct2json API functions. ```C typedef struct { char name[16]; } Hometown; typedef struct { uint8_t id; char name[10]; Hometown hometown; } Student; Student orignal_struct_student = { .id = 24, .name = "armink", .hometown.name = "China", }; Student *struct_student = &orignal_struct_student; /* ������Student JSON���� */ s2j_create_json_obj(json_student); /* ���л���ת����Student�ṹ���idԪ�� */ s2j_json_set_basic_element(json_student, struct_student, int, id); /* ���л�Student�ṹ���nameԪ�� */ s2j_json_set_basic_element(json_student, struct_student, string, name); /* ���л�Student�ṹ����ӽṹ��hometownԪ�� */ s2j_json_set_struct_element(json_hometown, json_student, struct_hometown, struct_student, Hometown, hometown); /* ���л�Hometown�ṹ���nameԪ�� */ s2j_json_set_basic_element(json_hometown, struct_hometown, string, name); ``` -------------------------------- ### Full JSON to C Structure Conversion Example Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Demonstrates a complete workflow for parsing a JSON string and populating nested C structures using the provided utility functions. Includes creating structure objects and extracting basic and nested elements. ```C typedef struct { char name[16]; } Hometown; typedef struct { uint8_t id; char name[10]; Hometown hometown; } Student; char orignal_json_string_student[] = "{\"id\":24, \"name\":\"armink\", \"hometown\":{\"name\":\"China\"}}"; /* ����Student JSON���� */ cJSON *json_student = cJSON_Parse(orignal_json_string_student); /* ������Student�ṹ����� */ s2j_create_struct_obj(struct_student, Student); /* �����л�Student�ṹ���idԪ�� */ s2j_struct_get_basic_element(struct_student, json_student, int, id); /* �����л�Student�ṹ���nameԪ�� */ s2j_struct_get_basic_element(struct_student, json_student, string, name); /* �����л�Student�ṹ����ӽṹ��hometownԪ�� */ s2j_struct_get_struct_element(struct_hometown, struct_student, json_hometown, json_student, Hometown, hometown); /* �����л�Hometown�ṹ���nameԪ�� */ s2j_struct_get_basic_element(struct_hometown, json_hometown, string, name); ``` -------------------------------- ### Get Basic Structure Element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Extracts a basic data type element from a JSON object into a C structure. Specify the target structure, source JSON, element type, and element name. ```C s2j_struct_get_basic_element(to_struct, from_json, type, element) ``` -------------------------------- ### Get Array Structure Element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Extracts an array element from a JSON object into a C structure. Requires the target structure, source JSON, structure type, and element name. ```C s2j_struct_get_array_element(to_struct, from_json, type, element) ``` -------------------------------- ### Get Nested Structure Element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Extracts a nested structure element from a JSON object into a C structure. This function handles hierarchical JSON data. ```C s2j_struct_get_struct_element(child_struct, to_struct, child_json, from_json, type, element) ``` -------------------------------- ### Create Structure Object Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Initializes a C structure object for population from JSON data. Requires the structure pointer and its type. ```C s2j_create_struct_obj(struct_obj, type) ``` -------------------------------- ### Initialize struct2json Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Initializes the struct2json library. It's important to initialize cJSON's memory management before calling this function, or it will use the default malloc and free. ```C void s2j_init(S2jHook *hook) ``` -------------------------------- ### s2j_init Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Initializes the struct2json library. It's recommended to initialize the underlying cJSON library before calling this function. This function allows for custom memory allocation by providing a hook for malloc and free. ```APIDOC ## s2j_init ### Description Initializes the struct2json library. Allows for custom memory allocation hooks. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature ```c void s2j_init(S2jHook *hook) ``` #### Parameters - **hook** (S2jHook *) - Pointer to a structure containing custom memory allocation functions (malloc and free). ``` -------------------------------- ### Create JSON Object Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Creates a JSON object to store the converted struct data. This is a prerequisite for setting elements. ```C s2j_create_json_obj(json_obj) ``` -------------------------------- ### s2j_create_json_obj Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Creates a JSON object. This is a foundational step before populating the JSON with data from a structure. ```APIDOC ## s2j_create_json_obj ### Description Creates a new JSON object. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature ```c void s2j_create_json_obj(json_obj) ``` #### Parameters - **json_obj** (pointer) - A pointer to store the newly created JSON object. ``` -------------------------------- ### Set Basic JSON Element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Converts a basic element (int, string, double) from a C struct to a JSON object. Ensure the type parameter matches the actual data type to avoid conversion errors. ```C s2j_json_set_basic_element(to_json, from_struct, type, element) ``` -------------------------------- ### s2j_create_struct_obj Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Creates a struct object to hold the parsed JSON data. It initializes the structure based on the provided type. ```APIDOC ## s2j_create_struct_obj ### Description Creates a struct object to hold the parsed JSON data. It initializes the structure based on the provided type. ### Function Signature ```c s2j_create_struct_obj(struct_obj, type) ``` ### Parameters #### Path Parameters - **struct_obj** (pointer) - Description of the struct object to be created. - **type** (type) - Description of the struct type. ``` -------------------------------- ### s2j_struct_get_basic_element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Retrieves a basic element (like int, string) from a JSON object and populates the corresponding field in a C struct. ```APIDOC ## s2j_struct_get_basic_element ### Description Retrieves a basic element (like int, string) from a JSON object and populates the corresponding field in a C struct. ### Function Signature ```c s2j_struct_get_basic_element(to_struct, from_json, type, element) ``` ### Parameters #### Path Parameters - **to_struct** (pointer) - The target struct object. - **from_json** (pointer) - The source JSON object. - **type** (type) - The data type of the element (e.g., int, string). - **element** (string) - The name of the element to retrieve. ``` -------------------------------- ### s2j_json_set_basic_element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Sets a basic element (integer, string, or double) in a JSON object from a source structure. Note that the library only supports `int`, `string`, and `double` for basic types. Other C types should be cast to one of these. ```APIDOC ## s2j_json_set_basic_element ### Description Sets a basic element (int, string, double) in a target JSON object from a source structure. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature ```c s2j_json_set_basic_element(to_json, from_struct, type, element) ``` #### Parameters - **to_json** (pointer) - The target JSON object to add the element to. - **from_struct** (pointer) - The source structure containing the data. - **type** (enum/string) - The type of the element to be set (e.g., `int`, `string`, `double`). - **element** (string) - The name of the element in the source structure. ``` -------------------------------- ### Set JSON Array Element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Converts an array element from a C struct to a JSON array. Supports int, string, and double types. The size parameter is crucial for correctly interpreting the array. ```C s2j_json_set_array_element(to_json, from_struct, type, element, size) ``` -------------------------------- ### s2j_struct_get_struct_element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Retrieves a nested struct element from a JSON object and populates the corresponding field in a C struct. This is used for nested structures. ```APIDOC ## s2j_struct_get_struct_element ### Description Retrieves a nested struct element from a JSON object and populates the corresponding field in a C struct. This is used for nested structures. ### Function Signature ```c s2j_struct_get_struct_element(child_struct, to_struct, child_json, from_json, type, element) ``` ### Parameters #### Path Parameters - **child_struct** (pointer) - Pointer to the nested struct object. - **to_struct** (pointer) - The target struct object. - **child_json** (pointer) - The JSON object representing the nested structure. - **from_json** (pointer) - The source JSON object. - **type** (type) - The data type of the nested struct element. Refer to section 1.2.2 for required types. - **element** (string) - The name of the nested struct element to retrieve. ``` -------------------------------- ### s2j_struct_get_array_element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Retrieves an element from a JSON array and populates the corresponding field in a C struct. This is used for array types within the struct. ```APIDOC ## s2j_struct_get_array_element ### Description Retrieves an element from a JSON array and populates the corresponding field in a C struct. This is used for array types within the struct. ### Function Signature ```c s2j_struct_get_array_element(to_struct, from_json, type, element) ``` ### Parameters #### Path Parameters - **to_struct** (pointer) - The target struct object. - **from_json** (pointer) - The source JSON object. - **type** (type) - The data type of the array element. Refer to section 1.2.2 for required types. - **element** (string) - The name of the array element to retrieve. ``` -------------------------------- ### Set Nested Struct JSON Element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Converts a nested struct element from a C struct to a JSON object. This function is used to handle hierarchical data structures. ```C s2j_json_set_struct_element(child_json, to_json, child_struct, from_struct, type, element) ``` -------------------------------- ### s2j_json_set_struct_element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Sets a nested structure element in a JSON object. This allows for hierarchical JSON creation by embedding one JSON object within another, corresponding to nested C structures. ```APIDOC ## s2j_json_set_struct_element ### Description Sets a nested structure element in a target JSON object, allowing for hierarchical JSON creation. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature ```c s2j_json_set_struct_element(child_json, to_json, child_struct, from_struct, type, element) ``` #### Parameters - **child_json** (pointer) - The JSON object that will represent the nested structure. - **to_json** (pointer) - The parent JSON object to which the nested structure will be added. - **child_struct** (pointer) - A pointer to the nested structure within the source structure. - **from_struct** (pointer) - The source structure containing the nested structure. - **type** (enum/string) - The type of the nested structure element. - **element** (string) - The name of the nested structure element in the source structure. ``` -------------------------------- ### s2j_json_set_array_element Source: https://github.com/armink/struct2json/blob/master/docs/zh/api.md Sets an array element in a JSON object from a source structure. This function is used for elements that are arrays within the structure. ```APIDOC ## s2j_json_set_array_element ### Description Sets an array element in a target JSON object from a source structure. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Function Signature ```c s2j_json_set_array_element(to_json, from_struct, type, element, size) ``` #### Parameters - **to_json** (pointer) - The target JSON object. - **from_struct** (pointer) - The source structure. - **type** (enum/string) - The type of the array elements (e.g., `int`, `string`, `double`). - **element** (string) - The name of the array element in the source structure. - **size** (int) - The size of the array. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.