json_t *py_variable_to_json( apr_pool_t *mp, PyObject *obj ) { json_writer_t *writer; json_t *json = NULL; apr_pool_t *tmp_mp; if ( PyDict_CheckExact( obj ) || PyList_CheckExact( obj ) || PyTuple_CheckExact( obj ) ) { apr_pool_create( &tmp_mp, NULL ); writer = json_writer_create( tmp_mp, mp ); py_variable_to_json_internal( obj, writer ); json = writer->json; apr_pool_destroy( tmp_mp ); } else { fprintf( stderr, "Must be an object, list or tuple.\n" ); } return json; }
json_t *perl_variable_to_json( apr_pool_t *mp, SV *input ) { json_writer_t *writer; json_t *json = NULL; apr_pool_t *tmp_mp; if ( perl_variable_can_be_json( input ) ) { apr_pool_create( &tmp_mp, NULL ); /* * Create a writer, using tmp_mp but give it the passed in mp to allocate * the JSON out of. This allows us to check to destroy tmp_mp and destroy * the writer but keep the JSON around. */ writer = json_writer_create( tmp_mp, mp ); perl_variable_to_json_internal( input, writer ); json = writer->json; apr_pool_destroy( tmp_mp ); } return json; }
int getprop_main(const struct cmd_getprop_info* info) { const char* format = info->getprop.format; const char* format_not_found = info->getprop.format_not_found; bool null = info->getprop.null; if (null && format == NULL && format_not_found == NULL) usage_error("must supply --format or " "--format-not-found or both if using -0"); find_symbol_in_libc("__system_property_foreach", &property_foreach); if (property_foreach == NULL) property_foreach = compat_property_foreach; dbg("using %s for property enumeration", property_foreach == compat_property_foreach ? "compat_property_foreach" : "__system_property_foreach"); int exit_status = 0; struct json_writer* writer = NULL; if (format == NULL && format_not_found == NULL) { writer = json_writer_create(xstdout); json_begin_object(writer); } const char** properties = ARGV_CONCAT(info->properties); bool first = true; char sep = null ? '\0' : '\n'; if (*properties == NULL) { struct property_vector* pv = find_all_properties(); char prev_name[PROP_NAME_MAX]; for (size_t i = 0; i < pv->size; ++i) { char name[PROP_NAME_MAX]; char value[PROP_VALUE_MAX]; (void) __system_property_read(pv->props[i], name, value); if (i > 0 && strcmp(name, prev_name) == 0) continue; if (writer != NULL) { json_begin_field(writer, name); json_emit_string(writer, value); } else { output_property(&first, sep, format, name, value); } strcpy(prev_name, name); } } else { size_t nproperties = argv_count(properties); qsort(properties, nproperties, sizeof (properties[0]), property_argv_compare); const char* property; const char* prev_property = NULL; while ((property = *properties++)) { if (prev_property != NULL && !strcmp(prev_property, property)) continue; if (writer != NULL) json_begin_field(writer, property); const prop_info* pi = __system_property_find(property); if (pi) { char value[PROP_VALUE_MAX]; __system_property_read(pi, NULL, value); if (writer != NULL) json_emit_string(writer, value); else if (format != NULL) output_property(&first, sep, format, property, value); } else { if (writer != NULL) json_emit_null(writer); else if (format_not_found != NULL) output_property(&first, sep, format_not_found, property, NULL); exit_status = 4; } prev_property = property; } } if (writer == NULL && !first && !null) xputc(sep, xstdout); if (writer != NULL) json_end_object(writer); xflush(xstdout); return exit_status; }