void YAJLBool(bool *value, yajl_val node, const char *name) { if (!YAJLTryLoadValue(&node, name)) { return; } *value = YAJL_IS_TRUE(node); }
/* Add YAJL value into YAJL generator handle (JSON object) * * jgen - YAJL generator handle allocated by yajl_gen_alloc() * jval - YAJL value usually returned by yajl_tree_get() */ static yajl_gen_status ovs_yajl_gen_val(yajl_gen jgen, yajl_val jval) { size_t array_len = 0; yajl_val *jvalues = NULL; yajl_val jobj_value = NULL; const char *obj_key = NULL; size_t obj_len = 0; yajl_gen_status yajl_gen_ret = yajl_gen_status_ok; if (jval == NULL) return yajl_gen_generation_complete; if (YAJL_IS_STRING(jval)) OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, YAJL_GET_STRING(jval)); else if (YAJL_IS_DOUBLE(jval)) OVS_YAJL_CALL(yajl_gen_double, jgen, YAJL_GET_DOUBLE(jval)); else if (YAJL_IS_INTEGER(jval)) OVS_YAJL_CALL(yajl_gen_double, jgen, YAJL_GET_INTEGER(jval)); else if (YAJL_IS_TRUE(jval)) OVS_YAJL_CALL(yajl_gen_bool, jgen, 1); else if (YAJL_IS_FALSE(jval)) OVS_YAJL_CALL(yajl_gen_bool, jgen, 0); else if (YAJL_IS_NULL(jval)) OVS_YAJL_CALL(yajl_gen_null, jgen); else if (YAJL_IS_ARRAY(jval)) { /* create new array and add all elements into the array */ array_len = YAJL_GET_ARRAY(jval)->len; jvalues = YAJL_GET_ARRAY(jval)->values; OVS_YAJL_CALL(yajl_gen_array_open, jgen); for (size_t i = 0; i < array_len; i++) OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jvalues[i]); OVS_YAJL_CALL(yajl_gen_array_close, jgen); } else if (YAJL_IS_OBJECT(jval)) { /* create new object and add all elements into the object */ OVS_YAJL_CALL(yajl_gen_map_open, jgen); obj_len = YAJL_GET_OBJECT(jval)->len; for (size_t i = 0; i < obj_len; i++) { obj_key = YAJL_GET_OBJECT(jval)->keys[i]; jobj_value = YAJL_GET_OBJECT(jval)->values[i]; OVS_YAJL_CALL(ovs_yajl_gen_tstring, jgen, obj_key); OVS_YAJL_CALL(ovs_yajl_gen_val, jgen, jobj_value); } OVS_YAJL_CALL(yajl_gen_map_close, jgen); } else { OVS_ERROR("%s() unsupported value type %d (skip)", __FUNCTION__, (int)(jval)->type); goto yajl_gen_failure; } return yajl_gen_status_ok; yajl_gen_failure: OVS_ERROR("%s() error to generate value", __FUNCTION__); return yajl_gen_ret; }
void bson_from_json_type(bson *b, yajl_val value, const char* key) { if( YAJL_IS_STRING( value ) ) { char* string = YAJL_GET_STRING( value ); bson_append_string( b, key, string ); } else if( YAJL_IS_NUMBER( value ) ) { char* string = value->u.number.r; size_t len = strlen( string ); // Hack to detect a double, since 'flags' is always set to double. if( memchr( string, '.', len ) || memchr( string, 'e', len ) || memchr( string, 'E', len ) ) { double number = YAJL_GET_DOUBLE( value ); bson_append_double( b, key, number ); } else { uint64_t number = YAJL_GET_INTEGER( value ); if( number <= INT_MIN && number <= INT_MAX) bson_append_int( b, key, (int)number ); else bson_append_long( b, key, number); } } else if ( YAJL_IS_FALSE( value ) ) { bson_append_bool( b, key, 0 ); } else if ( YAJL_IS_TRUE( value ) ) { bson_append_bool( b, key, 1 ); } else if ( YAJL_IS_ARRAY( value ) ) { bson_append_start_array( b, key ); bson_from_json_array( b, value ); bson_append_finish_array( b ); } else if ( YAJL_IS_OBJECT( value ) ) { bson_append_start_object( b, key ); bson_from_json_object( b, value ); bson_append_finish_object( b ); } }
SEXP ParseValue(yajl_val node, int bigint){ if(YAJL_IS_NULL(node)){ return R_NilValue; } if(YAJL_IS_STRING(node)){ SEXP tmp = PROTECT(allocVector(STRSXP, 1)); SET_STRING_ELT(tmp, 0, mkCharCE(YAJL_GET_STRING(node), CE_UTF8)); UNPROTECT(1); return tmp; } if(YAJL_IS_INTEGER(node)){ long long int val = YAJL_GET_INTEGER(node); /* 2^53 is highest int stored as double without loss */ if(bigint && (val > 9007199254740992 || val < -9007199254740992)){ char buf[32]; #ifdef _WIN32 snprintf(buf, 32, "%I64d", val); #else snprintf(buf, 32, "%lld", val); #endif return mkString(buf); /* see .Machine$integer.max in R */ } else if(val > 2147483647 || val < -2147483647){ return ScalarReal(val); } else { return ScalarInteger(val); } } if(YAJL_IS_DOUBLE(node)){ return(ScalarReal(YAJL_GET_DOUBLE(node))); } if(YAJL_IS_NUMBER(node)){ /* A number that is not int or double (very rare) */ /* This seems to correctly round to Inf/0/-Inf */ return(ScalarReal(YAJL_GET_DOUBLE(node))); } if(YAJL_IS_TRUE(node)){ return(ScalarLogical(1)); } if(YAJL_IS_FALSE(node)){ return(ScalarLogical(0)); } if(YAJL_IS_OBJECT(node)){ return(ParseObject(node, bigint)); } if(YAJL_IS_ARRAY(node)){ return(ParseArray(node, bigint)); } error("Invalid YAJL node type."); }
void message_json_handler_function(yajl_val j, void *context) { state *s = context; // LLog("[message_json_handler_function] will read value type\n"); // LLog("[message_json_handler_function] value is %d\n", (unsigned int)j); // LLog("[message_json_handler_function] value type is %d\n", j->type); if (!YAJL_IS_OBJECT(j)) { LLog("[message_json_handler_function] value was not an object, bailing out\n"); return; } int num_vals = YAJL_GET_OBJECT(j)->len; for (int i = 0; i < num_vals; i++) { const char *name = YAJL_GET_OBJECT(j)->keys[i]; //LLog("[message_json_handler_function] did get name = %s\n", name); yajl_val value = YAJL_GET_OBJECT(j)->values[i]; if (YAJL_IS_INTEGER(value)) { //LLog("[message_json_handler_function] value is integer\n"); state_set_int(s, name, YAJL_GET_INTEGER(value)); } else if (YAJL_IS_DOUBLE(value)) { //LLog("[message_json_handler_function] value is double\n"); state_set_float(s, name, (float)YAJL_GET_DOUBLE(value)); } else if (YAJL_IS_TRUE(value)) { //LLog("[message_json_handler_function] value is true\n"); state_set_int(s, name, 1); } else if (YAJL_IS_FALSE(value)) { //LLog("[message_json_handler_function] value is false\n"); state_set_int(s, name, 0); } else { //LLog("[message_json_handler_function] value is complex\n"); state_set_json(s, name, value); } } }