/*
-
Tests if casting within the json_object_get_* functions work correctly.
-
Also checks the json_object_get_type and json_object_is_type functions.
*/
//gcc -o test test_cast.c -I /usr/local/include/json-c/ -L /usr/local/bin/ -l json-c
//FOR ARM
// C R O S S C O M P I L E g c c − o c p u i d g e t s . c / / − L / u s r / l o c a l / j s o n − c − 0.9 a r m / l i b / / / {CROSS_COMPILE}gcc -o cpuid gets.c //-L /usr/local/json-c-0.9arm/lib/ // CROSSCOMPILEgcc−ocpuidgets.c//−L/usr/local/json−c−0.9arm/lib///{CROSS_COMPILE}gcc -ljson -I /usr/local/json-c-13arm/include/json/ -L /usr/local/json-c-13arm/lib/ -o test test2.c#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include “config.h”
#include “json_inttypes.h”
#include “json_object.h”
#include “json_tokener.h”
#include “json_util.h”
static int getit(struct json_object *new_obj, const char *field);
static int checktype_header(void);
static int checktype(struct json_object *new_obj, const char *field);
int main(int argc, char **argv)
{
const char input = “{\n
“string_of_char”: “testjson”,\n
“string_of_digits”: “123”,\n
“regular_number”: 222,\n
“decimal_number”: 99.55,\n
“boolean_true”: true,\n
“boolean_false”: false,\n
“big_number”: 2147483649,\n
“a_null”: null,\n
}”;
/ Note: 2147483649 = INT_MAX + 2 */
//申请容器
struct json_object *new_obj;
//json 字符放入容器
new_obj = json_tokener_parse(input);
// printf(“Parsed input: %s\n”, input);
// printf(“Result is %s\n”, (new_obj == NULL) ? “NULL (error!)” : “not NULL”);
//注入字符失败
if (!new_obj)
return 1; // oops, we failed.
//
char *t_1 = "string_of_digits";
//按项反转获取
//1.new_obj 字符数组
//2.The second param name
//getit(new_obj, "string_of_digits");
/* \"string_of_char\": \"testjson\",\n\ */
getit(new_obj, "string_of_char");
getit(new_obj, t_1);
getit(new_obj, "regular_number");
getit(new_obj, "decimal_number");
getit(new_obj, "boolean_true");
getit(new_obj, "boolean_false");
getit(new_obj, "big_number");
getit(new_obj, "a_null");
// Now check the behaviour of the json_object_is_type() function.
// 检测行为的 json_object_is_type 功能
// char *t_2 = “string_of_digits”;
// printf("\n================================\n");
// checktype_header(); //检测类型
// checktype(new_obj, NULL); //空
//checktype(new_obj, “string_of_digits”);
// checktype(new_obj, t_2);
// checktype(new_obj, “regular_number”);
// checktype(new_obj, “decimal_number”);
// checktype(new_obj, “boolean_true”);
// checktype(new_obj, “boolean_false”);
// checktype(new_obj, “big_number”);
// checktype(new_obj, “a_null”);
json_object_put(new_obj);
return 0;
}
static int getit(struct json_object *new_obj, const char *field)
{
struct json_object *o = NULL;
if (!json_object_object_get_ex(new_obj, field, &o))
printf(“Field %s does not exist\n”, field);
enum json_type o_type = json_object_get_type(o);
//手动注入类型 并输出
// printf(“new_obj.%s json_object_get_type()=%s\n”, field,
// json_type_to_name(o_type)); //打印类型
printf(“new_obj.%s json_object_get_int()=%d\n”, field,
json_object_get_int(o)); //输出 int 类型
// printf(“new_obj.%s json_object_get_int64()=%” PRId64 “\n”, field,
// json_object_get_int64(o)); //输出 int64 类型
// printf(“new_obj.%s json_object_get_boolean()=%d\n”, field,
// json_object_get_boolean(o));
// printf(“new_obj.%s json_object_get_double()=%f\n”, field,
// json_object_get_double(o));
return 0;
}
static int checktype_header()
{
// printf(“json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n”,
// json_type_to_name(json_type_null),
// json_type_to_name(json_type_boolean),
// json_type_to_name(json_type_double),
// json_type_to_name(json_type_int),
// json_type_to_name(json_type_object),
// json_type_to_name(json_type_array),
// json_type_to_name(json_type_string));
return 0;
}
static int checktype(struct json_object *new_obj, const char *field)
{
struct json_object *o = new_obj;
if (field && !json_object_object_get_ex(new_obj, field, &o))
printf(“Field %s does not exist\n”, field);
// printf(“new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n”,
// field ? “.” : " ", field ? field : “”,
// json_object_is_type(o, json_type_null),
// json_object_is_type(o, json_type_boolean),
// json_object_is_type(o, json_type_double),
// json_object_is_type(o, json_type_int),
// json_object_is_type(o, json_type_object),
// json_object_is_type(o, json_type_array),
// json_object_is_type(o, json_type_string));
return 0;