Now that WITH_YAJL can never be set, we can remove some more code. Signed-off-by: Ján Tomko <jtomko@xxxxxxxxxx> --- src/util/virjson.c | 416 --------------------------------------------- 1 file changed, 416 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 7a22c54f03..98011759fb 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -34,10 +34,6 @@ #if WITH_JSON_C # include <json.h> -#elif WITH_YAJL -# include <yajl/yajl_gen.h> -# include <yajl/yajl_parse.h> - #endif /* XXX fixme */ @@ -1563,418 +1559,6 @@ virJSONValueToBuffer(virJSONValue *object, return ret; } -#elif WITH_YAJL -static int -virJSONParserInsertValue(virJSONParser *parser, - virJSONValue **value) -{ - if (!parser->head) { - parser->head = g_steal_pointer(value); - } else { - virJSONParserState *state; - if (!parser->nstate) { - VIR_DEBUG("got a value to insert without a container"); - return -1; - } - - state = &parser->state[parser->nstate-1]; - - switch (state->value->type) { - case VIR_JSON_TYPE_OBJECT: { - if (!state->key) { - VIR_DEBUG("missing key when inserting object value"); - return -1; - } - - if (virJSONValueObjectAppend(state->value, - state->key, - value) < 0) - return -1; - - VIR_FREE(state->key); - } break; - - case VIR_JSON_TYPE_ARRAY: { - if (state->key) { - VIR_DEBUG("unexpected key when inserting array value"); - return -1; - } - - if (virJSONValueArrayAppend(state->value, - value) < 0) - return -1; - } break; - - default: - VIR_DEBUG("unexpected value type, not a container"); - return -1; - } - } - - return 0; -} - - -static int -virJSONParserHandleNull(void *ctx) -{ - virJSONParser *parser = ctx; - g_autoptr(virJSONValue) value = virJSONValueNewNull(); - - VIR_DEBUG("parser=%p", parser); - - if (virJSONParserInsertValue(parser, &value) < 0) - return 0; - - return 1; -} - - -static int -virJSONParserHandleBoolean(void *ctx, - int boolean_) -{ - virJSONParser *parser = ctx; - g_autoptr(virJSONValue) value = virJSONValueNewBoolean(boolean_); - - VIR_DEBUG("parser=%p boolean=%d", parser, boolean_); - - if (virJSONParserInsertValue(parser, &value) < 0) - return 0; - - return 1; -} - - -static int -virJSONParserHandleNumber(void *ctx, - const char *s, - size_t l) -{ - virJSONParser *parser = ctx; - g_autoptr(virJSONValue) value = virJSONValueNewNumber(g_strndup(s, l)); - - VIR_DEBUG("parser=%p str=%s", parser, value->data.number); - - if (virJSONParserInsertValue(parser, &value) < 0) - return 0; - - return 1; -} - - -static int -virJSONParserHandleString(void *ctx, - const unsigned char *stringVal, - size_t stringLen) -{ - virJSONParser *parser = ctx; - g_autoptr(virJSONValue) value = virJSONValueNewString(g_strndup((const char *)stringVal, stringLen)); - - VIR_DEBUG("parser=%p str=%p", parser, (const char *)stringVal); - - if (virJSONParserInsertValue(parser, &value) < 0) - return 0; - - return 1; -} - - -static int -virJSONParserHandleMapKey(void *ctx, - const unsigned char *stringVal, - size_t stringLen) -{ - virJSONParser *parser = ctx; - virJSONParserState *state; - - VIR_DEBUG("parser=%p key=%p", parser, (const char *)stringVal); - - if (!parser->nstate) - return 0; - - state = &parser->state[parser->nstate-1]; - if (state->key) - return 0; - state->key = g_strndup((const char *)stringVal, stringLen); - return 1; -} - - -static int -virJSONParserHandleStartMap(void *ctx) -{ - virJSONParser *parser = ctx; - g_autoptr(virJSONValue) value = virJSONValueNewObject(); - virJSONValue *tmp = value; - - VIR_DEBUG("parser=%p", parser); - - if (virJSONParserInsertValue(parser, &value) < 0) - return 0; - - VIR_REALLOC_N(parser->state, parser->nstate + 1); - - parser->state[parser->nstate].value = tmp; - parser->state[parser->nstate].key = NULL; - parser->nstate++; - - return 1; -} - - -static int -virJSONParserHandleEndMap(void *ctx) -{ - virJSONParser *parser = ctx; - virJSONParserState *state; - - VIR_DEBUG("parser=%p", parser); - - if (!parser->nstate) - return 0; - - state = &(parser->state[parser->nstate-1]); - if (state->key) { - VIR_FREE(state->key); - return 0; - } - - VIR_DELETE_ELEMENT(parser->state, parser->nstate - 1, parser->nstate); - - return 1; -} - - -static int -virJSONParserHandleStartArray(void *ctx) -{ - virJSONParser *parser = ctx; - g_autoptr(virJSONValue) value = virJSONValueNewArray(); - virJSONValue *tmp = value; - - VIR_DEBUG("parser=%p", parser); - - if (virJSONParserInsertValue(parser, &value) < 0) - return 0; - - VIR_REALLOC_N(parser->state, parser->nstate + 1); - - parser->state[parser->nstate].value = tmp; - parser->state[parser->nstate].key = NULL; - parser->nstate++; - - return 1; -} - - -static int -virJSONParserHandleEndArray(void *ctx) -{ - virJSONParser *parser = ctx; - virJSONParserState *state; - - VIR_DEBUG("parser=%p", parser); - - if (!(parser->nstate - parser->wrap)) - return 0; - - state = &(parser->state[parser->nstate-1]); - if (state->key) { - VIR_FREE(state->key); - return 0; - } - - VIR_DELETE_ELEMENT(parser->state, parser->nstate - 1, parser->nstate); - - return 1; -} - - -static const yajl_callbacks parserCallbacks = { - virJSONParserHandleNull, - virJSONParserHandleBoolean, - NULL, - NULL, - virJSONParserHandleNumber, - virJSONParserHandleString, - virJSONParserHandleStartMap, - virJSONParserHandleMapKey, - virJSONParserHandleEndMap, - virJSONParserHandleStartArray, - virJSONParserHandleEndArray -}; - - -/* XXX add an incremental streaming parser - yajl trivially supports it */ -virJSONValue * -virJSONValueFromString(const char *jsonstring) -{ - yajl_handle hand; - virJSONParser parser = { NULL, NULL, 0, 0 }; - virJSONValue *ret = NULL; - int rc; - size_t len = strlen(jsonstring); - - VIR_DEBUG("string=%s", jsonstring); - - hand = yajl_alloc(&parserCallbacks, NULL, &parser); - if (!hand) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to create JSON parser")); - return NULL; - } - - /* Yajl 2 is nice enough to default to rejecting trailing garbage. */ - rc = yajl_parse(hand, (const unsigned char *)jsonstring, len); - if (rc != yajl_status_ok || - yajl_complete_parse(hand) != yajl_status_ok) { - unsigned char *errstr = yajl_get_error(hand, 1, - (const unsigned char*)jsonstring, - strlen(jsonstring)); - - virReportError(VIR_ERR_INTERNAL_ERROR, - _("cannot parse json %1$s: %2$s"), - jsonstring, (const char*) errstr); - yajl_free_error(hand, errstr); - virJSONValueFree(parser.head); - goto cleanup; - } - - if (parser.nstate != 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("cannot parse json %1$s: unterminated string/map/array"), - jsonstring); - virJSONValueFree(parser.head); - } else { - ret = parser.head; - } - - cleanup: - yajl_free(hand); - - if (parser.nstate) { - size_t i; - for (i = 0; i < parser.nstate; i++) - VIR_FREE(parser.state[i].key); - VIR_FREE(parser.state); - } - - VIR_DEBUG("result=%p", ret); - - return ret; -} - - -static int -virJSONValueToStringOne(virJSONValue *object, - yajl_gen g) -{ - size_t i; - - VIR_DEBUG("object=%p type=%d gen=%p", object, object->type, g); - - switch (object->type) { - case VIR_JSON_TYPE_OBJECT: - if (yajl_gen_map_open(g) != yajl_gen_status_ok) - return -1; - for (i = 0; i < object->data.object.npairs; i++) { - if (yajl_gen_string(g, - (unsigned char *)object->data.object.pairs[i].key, - strlen(object->data.object.pairs[i].key)) - != yajl_gen_status_ok) - return -1; - if (virJSONValueToStringOne(object->data.object.pairs[i].value, g) < 0) - return -1; - } - if (yajl_gen_map_close(g) != yajl_gen_status_ok) - return -1; - break; - case VIR_JSON_TYPE_ARRAY: - if (yajl_gen_array_open(g) != yajl_gen_status_ok) - return -1; - for (i = 0; i < object->data.array.nvalues; i++) { - if (virJSONValueToStringOne(object->data.array.values[i], g) < 0) - return -1; - } - if (yajl_gen_array_close(g) != yajl_gen_status_ok) - return -1; - break; - - case VIR_JSON_TYPE_STRING: - if (yajl_gen_string(g, (unsigned char *)object->data.string, - strlen(object->data.string)) != yajl_gen_status_ok) - return -1; - break; - - case VIR_JSON_TYPE_NUMBER: - if (yajl_gen_number(g, object->data.number, - strlen(object->data.number)) != yajl_gen_status_ok) - return -1; - break; - - case VIR_JSON_TYPE_BOOLEAN: - if (yajl_gen_bool(g, object->data.boolean) != yajl_gen_status_ok) - return -1; - break; - - case VIR_JSON_TYPE_NULL: - if (yajl_gen_null(g) != yajl_gen_status_ok) - return -1; - break; - - default: - return -1; - } - - return 0; -} - - -int -virJSONValueToBuffer(virJSONValue *object, - virBuffer *buf, - bool pretty) -{ - yajl_gen g; - const unsigned char *str; - size_t len; - int ret = -1; - - VIR_DEBUG("object=%p", object); - - g = yajl_gen_alloc(NULL); - if (!g) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Unable to create JSON formatter")); - goto cleanup; - } - yajl_gen_config(g, yajl_gen_beautify, pretty ? 1 : 0); - yajl_gen_config(g, yajl_gen_indent_string, pretty ? " " : " "); - yajl_gen_config(g, yajl_gen_validate_utf8, 1); - - if (virJSONValueToStringOne(object, g) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("failed to convert virJSONValue to yajl data")); - goto cleanup; - } - - if (yajl_gen_get_buf(g, &str, &len) != yajl_gen_status_ok) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("failed to format JSON")); - goto cleanup; - } - - virBufferAdd(buf, (const char *) str, len); - ret = 0; - - cleanup: - yajl_gen_free(g); - - return ret; -} - - #else virJSONValue * virJSONValueFromString(const char *jsonstring G_GNUC_UNUSED) -- 2.45.2