json_t* nonstring(char* arg) { json_t* temp; char* endptr; if (!strcmp(arg, "null") || !strcmp(arg, "n")) {return json_null();} if (!strcmp(arg, "true") || !strcmp(arg, "t")) {return json_true();} if (!strcmp(arg, "false") || !strcmp(arg, "f")) {return json_false();} if (!strcmp(arg, "array") || !strcmp(arg, "[]")) {return json_array();} if (!strcmp(arg, "object") || !strcmp(arg, "{}")) {return json_object();} errno = 0; temp = json_integer(strtol(arg, &endptr, 10)); if (!errno && *endptr=='\0') {return temp;} errno = 0; temp = json_real(strtod(arg, &endptr)); if (!errno && *endptr=='\0') {return temp;} arg_err("parse error: illegal nonstring on arg %i, \"%s\""); return json_null(); }
static json_object_t opensensordata_cache_get(opensensordata_t* osd, const char* name) { int err; char buffer[512]; char filename[512]; if ((osd->cache == NULL) || (strlen(osd->cache) == 0)) { log_err("OpenSensorData: Invalid cache dir."); return json_null(); } opensensordata_get_cache_file(osd, name, filename, 512); json_object_t def = json_load(filename, &err, buffer, 512); if (err) { log_err("%s", buffer); json_unref(def); return json_null(); } else if (!json_isobject(def)) { log_err("OpenSensorData: Invalid definition: %s", filename); json_unref(def); return json_null(); } // Check whether the response was an error object. json_object_t e = json_object_get(def, "error"); if (!json_isnull(e)) { const char* msg = json_object_getstr(def, "msg"); log_err("OpenSensorData: Server returned an error: %s", msg); json_unref(def); return json_null(); } return def; }
json_t* extract(json_t* json, char* key) { int i, s; json_t* temp; switch (json_typeof(json)) { case JSON_OBJECT: temp = json_object_get(json, key); if (temp == NULL) {break;} return temp; case JSON_ARRAY: s = json_array_size(json); if (s == 0) {break;} i = estrtol(key); return json_array_get(json, i % s); case JSON_STRING: case JSON_INTEGER: case JSON_REAL: case JSON_TRUE: case JSON_FALSE: case JSON_NULL: default: break; } json_err("has no elements to extract", json); return json_null(); }
void node_received(char *line) { fprintf(stderr, "app: Received: '%s'\n", line); json_error_t error; json_t *incoming = json_loads(line, 0, &error); const char *command = json_string_value(json_array_get(incoming, 0)); assert(command); json_t *arg = json_array_get(incoming, 1); msg_func_t handler = find_msg_handler(command); if (handler == NULL) { fprintf(stderr, "app: Unknown command received: '%s'", command); exit(1); } else { json_t *response = handler(arg); if (json_array_size(incoming) > 2) { json_t *response_command = json_array_get(incoming, 2); json_t *array = json_array(); json_array_append(array, response_command); if (response) json_array_append_new(array, response); else json_array_append_new(array, json_null()); node_send_json(array); } else { if (response) json_decref(response); } } json_decref(incoming); free(line); }
/* encode_json : encode the exnode in the JSON format n dump it in string * dir : directory name * parent_id : parent directory id * return : JSON dump of exnode * */ char *encode_json(const char *dir, const char *parent_id){ if(dir == NULL){ fprintf(stderr, "Invalid directory name \n"); return NULL; } // create json object json_t *json_exnode = json_object(); json_object_set(json_exnode, "name", json_string(dir)); json_object_set(json_exnode, "created", json_integer(time(NULL))); json_object_set(json_exnode, "modified", json_integer(time(NULL))); json_object_set(json_exnode, "mode", json_string("directory")); json_object_set(json_exnode, "size", json_integer(0)); if (parent_id == NULL) { json_object_set(json_exnode, "parent", json_null()); } else { json_t *pobj = json_object(); json_object_set(pobj, "href", json_string(parent_id)); json_object_set(pobj, "rel", json_string("full")); json_object_set(json_exnode, "parent", pobj); } return json_dumps(json_exnode, JSON_INDENT(1)); }
// sets a json value for the next index int json_array_add_int(json_t *array, int *value, json_context_t *json_context) { int exit_code = 0; json_t *json_integer_value = NULL; check_not_null(array); check_not_null(json_context); if (value != NULL) { json_integer_value = json_integer(*value); check_not_null(json_integer_value); check_result(json_array_append_new(array, json_integer_value), 0); json_integer_value = NULL; } else { check_result(json_array_append(array, json_null()), 0); } goto cleanup; error: exit_code = -1; cleanup: if (json_integer_value != NULL) { json_free(json_integer_value); } return exit_code; }
json_t* extract(json_t* json, char* key) { int i, s; json_t* temp; switch (json_typeof(json)) { case JSON_OBJECT: temp = json_object_get(json, key); if (temp == NULL) {break;} return temp; case JSON_ARRAY: s = json_array_size(json); if (s == 0) {json_err("index out of bounds", json); break;} i = estrtol(key); if ((i < -s) || (i >= s)) {json_err("index out of bounds", json);} // stupid fix for a stupid modulus operation while (i<0) {i+=s;} return json_array_get(json, i % s); case JSON_STRING: case JSON_INTEGER: case JSON_REAL: case JSON_TRUE: case JSON_FALSE: case JSON_NULL: default: break; } json_err("has no elements to extract", json); return json_null(); }
void OUTPUT_FORMATTER::json_finalize_result(bool result) { POOL_MEM string; json_t *msg_obj = json_object(); json_t *error_obj; /* * We mimic json-rpc result and error messages, * To make it easier to implement real json-rpc later on. */ json_object_set(msg_obj, "jsonrpc", json_string("2.0")); json_object_set(msg_obj, "id", json_null()); if (result) { json_object_set(msg_obj, "result", result_array_json); } else { error_obj = json_object(); json_object_set_new(error_obj, "code", json_integer(1)); json_object_set_new(error_obj, "message", json_string("failed")); json_object_set(error_obj, "data", result_array_json); json_object_set_new(msg_obj, "error", error_obj); } string.bsprintf("%s\n", json_dumps(msg_obj, UA_JSON_FLAGS)); send_func(send_ctx, string.c_str()); json_array_clear(result_array_json); json_object_clear(msg_obj); }
json_t *ParameterInfoWriter::write() { json_t *p = json_object(); json_object_set(p, "name", json_string(name.c_str())); json_object_set(p, "type", json_string(fullTypeName.c_str())); json_object_set(p, "hasdefault", attr.hasDefault ? json_true() : json_false()); if (attr.hasDefault) { json_object_set(p, "defaultvalue", json_string(defaultArg.c_str())); } json_object_set(p, "isvarargs", attr.isVarArgs ? json_true() : json_false()); if (templateInfo) { json_object_set(p, "templatetypes", MemberInfoWriter::writeTemplateTypeInfo(templateInfo)); } else { json_object_set(p, "templatetypes", json_null()); } return p; }
Json Json::setArrayMember( const int index, const Json member ) { while ( index >= json_array_size( data ) ) json_array_append( data, json_null() ); json_array_set( data, index, member.data ); return json_array_get( data, index ); }
// sets a json value for a key int json_object_set_object(json_t *object, char *key, json_t *value, json_context_t *json_context) { int exit_code = 0; check_not_null(object); check_not_null(key); check_not_null(json_context); if (value != NULL) { check_result(json_object_set_new(object, key, value), 0); } else { check_result(json_object_set(object, key, json_null()), 0); } goto cleanup; error: exit_code = -1; cleanup: return exit_code; }
Json::Json( json_t *rhd ): data(rhd) { if ( data == NULL ) data = json_null(); else json_incref( data ); }
Json::Json( Json const &rhd ): data(rhd.data) { if ( data == NULL ) data = json_null(); else json_incref( data ); }
//----------------------------------------------------------------// json_t* _luaToJSON ( lua_State* L, int idx ) { switch ( lua_type ( L, idx )) { case LUA_TBOOLEAN: { bool value = lua_toboolean ( L, idx ) == 0 ? false : true; return value ? json_true () : json_false (); } case LUA_TTABLE: { return lua_objlen ( L, idx ) ? _luaToJSONArray ( L, idx ) : _luaToJSONObject ( L, idx ); } case LUA_TSTRING: { cc8* str = lua_tostring ( L, idx ); return json_string ( str ); } case LUA_TNUMBER: { double real = lua_tonumber ( L, idx ); return json_real ( real ); } case LUA_TLIGHTUSERDATA: { return json_null (); } }; return 0; }
// sets a json value for the next index int json_array_add_object(json_t *array, json_t *value, json_context_t *json_context) { int exit_code = 0; check_not_null(array); check_not_null(json_context); if (value != NULL) { check_result(json_array_append_new(array, value), 0); } else { check_result(json_array_append(array, json_null()), 0); } goto cleanup; error: exit_code = -1; cleanup: return exit_code; }
/** * fitness is either log likelihood or sum of square */ void ssm_print_trace(FILE *stream, ssm_theta_t *theta, ssm_nav_t *nav, const double fitness, const int index) { int i; ssm_parameter_t *parameter; #if SSM_JSON json_t *jout = json_object(); #endif for(i=0; i < nav->theta_all->length; i++) { parameter = nav->theta_all->p[i]; #if SSM_JSON json_object_set_new(jout, parameter->name, json_real(parameter->f_inv(gsl_vector_get(theta, i)))); #else fprintf(stream, "%g,", parameter->f_inv(gsl_vector_get(theta, i))); #endif } #if SSM_JSON json_object_set_new(jout, "fitness", isnan(fitness) ? json_null() : json_real(fitness)); json_object_set_new(jout, "index", json_integer(index)); // m ssm_json_dumpf(stream, "trace", jout); #else fprintf(stream, "%g,%d\n", fitness, index); #endif }
json_t* render_string_or_null(CXString s) { const char* str = clang_getCString(s); json_t* js; if (str && strlen(str) > 0) js = json_string(str); else js = json_null(); clang_disposeString(s); return js; }
json_object_t opensensordata_get_group(opensensordata_t* osd, int cache_ok) { if (cache_ok) return opensensordata_cache_get(osd, "group"); log_err("OpenSensorData: get group not yet implemented"); return json_null(); }
json_object_t opensensordata_get_photostream(opensensordata_t* osd, const char* name, int cache_ok) { if (cache_ok) return opensensordata_cache_get(osd, name); log_err("OpenSensorData: get photostream not yet implmented"); return json_null(); }
json_t *bunser(const char *buf, const char *end, int *needed, json_error_t *jerr) { json_int_t ival; switch (buf[0]) { case BSER_INT8: case BSER_INT16: case BSER_INT32: case BSER_INT64: if (!bunser_int(buf, end - buf, needed, &ival)) { snprintf(jerr->text, sizeof(jerr->text), "invalid integer encoding"); return NULL; } return json_integer(ival); case BSER_STRING: { const char *start; json_int_t len; if (!bunser_string(buf, end - buf, needed, &start, &len)) { snprintf(jerr->text, sizeof(jerr->text), "invalid string encoding"); return NULL; } return json_string_binary(start, len); } case BSER_REAL: *needed = sizeof(double) + 1; return json_real(*(double*)(buf+1)); case BSER_TRUE: *needed = 1; return json_true(); case BSER_FALSE: *needed = 1; return json_false(); case BSER_NULL: *needed = 1; return json_null(); case BSER_ARRAY: return bunser_array(buf, end, needed, jerr); case BSER_TEMPLATE: return bunser_template(buf, end, needed, jerr); case BSER_OBJECT: return bunser_object(buf, end, needed, jerr); default: snprintf(jerr->text, sizeof(jerr->text), "invalid bser encoding type %02x", (int)buf[0]); return NULL; } return NULL; }
inline bool isnull(lua_State* L, int idx) { lua_pushvalue(L, idx); // [value] json_null(L); // [value, json.null] auto is = lua_rawequal(L, -1, -2) != 0; lua_pop(L, 2); // [] return is; }
json &json::push_back( void ) { if ( !valid() || is<json_null>() ) set<json_array>(); precondition( is<json_array>(), "not a json array" ); auto &tmp = get<json_array>(); tmp.push_back( json_null() ); return tmp.back(); }
static char *ngx_http_acme_json_request(ngx_conf_t *cf, void *conf, char *url, ngx_http_acme_http_method_t http_method, json_t *request_json, json_t **response_json, ngx_http_acme_slist_t **response_headers) { ngx_str_t response_data; ngx_str_t request_data; char *tmp; json_error_t error; /* Convert request_json to string to provide it to the following method */ request_data = (ngx_str_t)ngx_null_string; if(!json_is_null(request_json)) { tmp = json_dumps(request_json, 0); if(tmp == NULL) { ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Error while creating request string from JSON\n"); return NGX_CONF_ERROR; } else { request_data.data = (u_char *)tmp; request_data.len = ngx_strlen(tmp); } } /* Make request */ if(ngx_http_acme_plain_request(cf, conf, url, http_method, request_data, &response_data, response_headers) != NGX_CONF_OK) { ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Error while making request\n"); return NGX_CONF_ERROR; } /* Now all the returned JSON is in the data variable */ /* * Parsing returned JSON */ /* Begin Jansson part */ if(response_data.len <= 0) { *response_json = json_null(); } else { *response_json = json_loadb((char *) response_data.data, response_data.len, 0, &error); } free(response_data.data); ngx_str_null(&response_data); if(*response_json == NULL) { ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Error parsing JSON: on line %d: %s\n", error.line, error.text); return NGX_CONF_ERROR; } /* End Jansson part */ return NGX_CONF_OK; } /* ngx_http_acme_json_request */
void PUSH(json_t* json) { if (stackpointer >= &stack[STACKDEPTH]) {hard_err("internal error: stack overflow");} if (json == NULL) { arg_err("parse error: bad json on arg %i, \"%s\""); json = json_null(); } *stackpointer++ = json; }
static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error) { json_t *json; switch(lex->token) { case TOKEN_STRING: { json = json_string_nocheck(lex->value.string); break; } case TOKEN_INTEGER: { json = json_integer(lex->value.integer); break; } case TOKEN_REAL: { json = json_real(lex->value.real); break; } case TOKEN_TRUE: json = json_true(); break; case TOKEN_FALSE: json = json_false(); break; case TOKEN_NULL: json = json_null(); break; case '{': json = parse_object(lex, flags, error); break; case '[': json = parse_array(lex, flags, error); break; case TOKEN_INVALID: error_set(error, lex, "invalid token"); return NULL; default: error_set(error, lex, "unexpected token"); return NULL; } if(!json) return NULL; return json; }
//native Handle:json_null(); static cell_t Native_json_null(IPluginContext *pContext, const cell_t *params) { json_t *result = json_null(); Handle_t hndlResult = g_pHandleSys->CreateHandle(htJanssonObject, result, pContext->GetIdentity(), myself->GetIdentity(), NULL); if(hndlResult == BAD_HANDLE) { pContext->ThrowNativeError("Could not create <Null> handle."); } return hndlResult; }
/*! * \brief Get the \ref json_mem block for a pointer allocated via * ast_json_malloc(). * * This function properly handles Jansson singletons (null, true, false), and * \c NULL. * * \param p Pointer, usually to a \c json_t or \ref ast_json. * \return \ref json_mem object with extra allocation info. */ static inline struct json_mem *to_json_mem(void *p) { struct json_mem *mem; /* Avoid ref'ing the singleton values */ if (p == NULL || p == json_null() || p == json_true() || p == json_false()) { return NULL; } mem = (struct json_mem *)((char *) (p) - sizeof(*mem)); ast_assert(mem->magic == JSON_MAGIC); return mem; }
Json::Json( json_type v ): data(NULL) { switch ( v ) { case JSON_ARRAY: data = json_array(); break; case JSON_OBJECT: data = json_object(); break; case JSON_FALSE: data = json_boolean(false); break; case JSON_TRUE: data = json_boolean(true); break; case JSON_INTEGER: data = json_integer(0); break; case JSON_REAL: data = json_real(0); break; case JSON_STRING: data = json_string(""); break; default: data = json_null(); break; } }
//encode, decode bool Json::parse( const std::string &buf ) { json_error_t err; json_t *obj = json_loads( buf.c_str(), JSON_DECODE_ANY, &err ); if ( obj != NULL ) { data = obj; return true; } json_decref( data ); data = json_null(); return false; }
// sets a json value for a key int json_object_set_string(json_t *object, char *key, char *value, json_context_t *json_context) { int exit_code = 0; json_t *json_string_value = NULL; char *value_copied = NULL; check_not_null(object); check_not_null(key); check_not_null(json_context); if (value != NULL) { check_result(malloc_memcpy_string(&value_copied, value), 0); json_string_value = json_string(value_copied); check_not_null(json_string_value); check_result(json_object_set_new(object, key, json_string_value), 0); json_string_value = NULL; check_result( array_add_string( &(json_context->strings), &(json_context->strings_allocated_count), &(json_context->strings_used_count), value_copied), 0); value_copied = NULL; } else { check_result(json_object_set(object, key, json_null()), 0); } goto cleanup; error: exit_code = -1; cleanup: if (json_string_value != NULL) { json_free(json_string_value); } if (value_copied != NULL) { free(value_copied); } return exit_code; }