virjson.c 27.0 KB
Newer Older
1
/*
2
 * virjson.c: JSON object parsing/formatting
3
 *
4
 * Copyright (C) 2009-2010, 2012-2013 Red Hat, Inc.
5
 * Copyright (C) 2009 Daniel P. Berrange
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 *
 */


#include <config.h>

26
#include "virjson.h"
27
#include "viralloc.h"
28
#include "virerror.h"
29
#include "virlog.h"
30
#include "virstring.h"
31
#include "virutil.h"
32

33
#if WITH_YAJL
34 35
# include <yajl/yajl_gen.h>
# include <yajl/yajl_parse.h>
36

37
# ifdef WITH_YAJL2
38 39 40 41 42
#  define yajl_size_t size_t
# else
#  define yajl_size_t unsigned int
# endif

43
#endif
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

/* XXX fixme */
#define VIR_FROM_THIS VIR_FROM_NONE


typedef struct _virJSONParserState virJSONParserState;
typedef virJSONParserState *virJSONParserStatePtr;
struct _virJSONParserState {
    virJSONValuePtr value;
    char *key;
};

typedef struct _virJSONParser virJSONParser;
typedef virJSONParser *virJSONParserPtr;
struct _virJSONParser {
    virJSONValuePtr head;
    virJSONParserStatePtr state;
    unsigned int nstate;
};


void virJSONValueFree(virJSONValuePtr value)
{
67
    size_t i;
E
Eric Blake 已提交
68
    if (!value || value->protect)
69 70
        return;

E
Eric Blake 已提交
71
    switch ((virJSONType) value->type) {
72
    case VIR_JSON_TYPE_OBJECT:
73
        for (i = 0; i < value->data.object.npairs; i++) {
74 75 76 77 78 79
            VIR_FREE(value->data.object.pairs[i].key);
            virJSONValueFree(value->data.object.pairs[i].value);
        }
        VIR_FREE(value->data.object.pairs);
        break;
    case VIR_JSON_TYPE_ARRAY:
80
        for (i = 0; i < value->data.array.nvalues; i++)
81 82 83 84 85 86 87 88 89
            virJSONValueFree(value->data.array.values[i]);
        VIR_FREE(value->data.array.values);
        break;
    case VIR_JSON_TYPE_STRING:
        VIR_FREE(value->data.string);
        break;
    case VIR_JSON_TYPE_NUMBER:
        VIR_FREE(value->data.number);
        break;
E
Eric Blake 已提交
90 91 92
    case VIR_JSON_TYPE_BOOLEAN:
    case VIR_JSON_TYPE_NULL:
        break;
93
    }
94 95

    VIR_FREE(value);
96 97 98 99 100 101 102 103 104 105 106 107 108 109
}


virJSONValuePtr virJSONValueNewString(const char *data)
{
    virJSONValuePtr val;

    if (!data)
        return virJSONValueNewNull();

    if (VIR_ALLOC(val) < 0)
        return NULL;

    val->type = VIR_JSON_TYPE_STRING;
110
    if (VIR_STRDUP(val->data.string, data) < 0) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        VIR_FREE(val);
        return NULL;
    }

    return val;
}

virJSONValuePtr virJSONValueNewStringLen(const char *data, size_t length)
{
    virJSONValuePtr val;

    if (!data)
        return virJSONValueNewNull();

    if (VIR_ALLOC(val) < 0)
        return NULL;

    val->type = VIR_JSON_TYPE_STRING;
129
    if (VIR_STRNDUP(val->data.string, data, length) < 0) {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        VIR_FREE(val);
        return NULL;
    }

    return val;
}

static virJSONValuePtr virJSONValueNewNumber(const char *data)
{
    virJSONValuePtr val;

    if (VIR_ALLOC(val) < 0)
        return NULL;

    val->type = VIR_JSON_TYPE_NUMBER;
145
    if (VIR_STRDUP(val->data.number, data) < 0) {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        VIR_FREE(val);
        return NULL;
    }

    return val;
}

virJSONValuePtr virJSONValueNewNumberInt(int data)
{
    virJSONValuePtr val = NULL;
    char *str;
    if (virAsprintf(&str, "%i", data) < 0)
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


virJSONValuePtr virJSONValueNewNumberUint(unsigned int data)
{
    virJSONValuePtr val = NULL;
    char *str;
    if (virAsprintf(&str, "%u", data) < 0)
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


virJSONValuePtr virJSONValueNewNumberLong(long long data)
{
    virJSONValuePtr val = NULL;
    char *str;
    if (virAsprintf(&str, "%lld", data) < 0)
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


virJSONValuePtr virJSONValueNewNumberUlong(unsigned long long data)
{
    virJSONValuePtr val = NULL;
    char *str;
    if (virAsprintf(&str, "%llu", data) < 0)
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


virJSONValuePtr virJSONValueNewNumberDouble(double data)
{
    virJSONValuePtr val = NULL;
    char *str;
205
    if (virDoubleToStr(&str, data) < 0)
206 207 208 209 210 211 212
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


213
virJSONValuePtr virJSONValueNewBoolean(int boolean_)
214 215 216 217 218 219 220
{
    virJSONValuePtr val;

    if (VIR_ALLOC(val) < 0)
        return NULL;

    val->type = VIR_JSON_TYPE_BOOLEAN;
221
    val->data.boolean = boolean_;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    return val;
}

virJSONValuePtr virJSONValueNewNull(void)
{
    virJSONValuePtr val;

    if (VIR_ALLOC(val) < 0)
        return NULL;

    val->type = VIR_JSON_TYPE_NULL;

    return val;
}

virJSONValuePtr virJSONValueNewArray(void)
{
    virJSONValuePtr val;

    if (VIR_ALLOC(val) < 0)
        return NULL;

    val->type = VIR_JSON_TYPE_ARRAY;

    return val;
}

virJSONValuePtr virJSONValueNewObject(void)
{
    virJSONValuePtr val;

    if (VIR_ALLOC(val) < 0)
        return NULL;

    val->type = VIR_JSON_TYPE_OBJECT;

    return val;
}

int virJSONValueObjectAppend(virJSONValuePtr object, const char *key, virJSONValuePtr value)
{
    char *newkey;

    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    if (virJSONValueObjectHasKey(object, key))
        return -1;

272
    if (VIR_STRDUP(newkey, key) < 0)
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
        return -1;

    if (VIR_REALLOC_N(object->data.object.pairs,
                      object->data.object.npairs + 1) < 0) {
        VIR_FREE(newkey);
        return -1;
    }

    object->data.object.pairs[object->data.object.npairs].key = newkey;
    object->data.object.pairs[object->data.object.npairs].value = value;
    object->data.object.npairs++;

    return 0;
}


int virJSONValueObjectAppendString(virJSONValuePtr object, const char *key, const char *value)
{
    virJSONValuePtr jvalue = virJSONValueNewString(value);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

int virJSONValueObjectAppendNumberInt(virJSONValuePtr object, const char *key, int number)
{
    virJSONValuePtr jvalue = virJSONValueNewNumberInt(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}


int virJSONValueObjectAppendNumberUint(virJSONValuePtr object, const char *key, unsigned int number)
{
    virJSONValuePtr jvalue = virJSONValueNewNumberUint(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

int virJSONValueObjectAppendNumberLong(virJSONValuePtr object, const char *key, long long number)
{
    virJSONValuePtr jvalue = virJSONValueNewNumberLong(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

int virJSONValueObjectAppendNumberUlong(virJSONValuePtr object, const char *key, unsigned long long number)
{
    virJSONValuePtr jvalue = virJSONValueNewNumberUlong(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

int virJSONValueObjectAppendNumberDouble(virJSONValuePtr object, const char *key, double number)
{
    virJSONValuePtr jvalue = virJSONValueNewNumberDouble(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

362
int virJSONValueObjectAppendBoolean(virJSONValuePtr object, const char *key, int boolean_)
363
{
364
    virJSONValuePtr jvalue = virJSONValueNewBoolean(boolean_);
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

int virJSONValueObjectAppendNull(virJSONValuePtr object, const char *key)
{
    virJSONValuePtr jvalue = virJSONValueNewNull();
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}


int virJSONValueArrayAppend(virJSONValuePtr array, virJSONValuePtr value)
{
    if (array->type != VIR_JSON_TYPE_ARRAY)
        return -1;

    if (VIR_REALLOC_N(array->data.array.values,
                      array->data.array.nvalues + 1) < 0)
        return -1;

    array->data.array.values[array->data.array.nvalues] = value;
    array->data.array.nvalues++;

    return 0;
}

int virJSONValueObjectHasKey(virJSONValuePtr object, const char *key)
{
404
    size_t i;
405 406 407 408

    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

409
    for (i = 0; i < object->data.object.npairs; i++) {
410 411 412 413 414 415 416 417 418
        if (STREQ(object->data.object.pairs[i].key, key))
            return 1;
    }

    return 0;
}

virJSONValuePtr virJSONValueObjectGet(virJSONValuePtr object, const char *key)
{
419
    size_t i;
420 421 422 423

    if (object->type != VIR_JSON_TYPE_OBJECT)
        return NULL;

424
    for (i = 0; i < object->data.object.npairs; i++) {
425 426 427 428 429 430 431
        if (STREQ(object->data.object.pairs[i].key, key))
            return object->data.object.pairs[i].value;
    }

    return NULL;
}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
int virJSONValueObjectKeysNumber(virJSONValuePtr object)
{
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    return object->data.object.npairs;
}

const char *virJSONValueObjectGetKey(virJSONValuePtr object, unsigned int n)
{
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return NULL;

    if (n >= object->data.object.npairs)
        return NULL;

    return object->data.object.pairs[n].key;
}

451 452 453 454 455 456 457
/* Remove the key-value pair tied to @key out of @object.  If @value is
 * not NULL, the dropped value object is returned instead of freed.
 * Returns 1 on success, 0 if no key was found, and -1 on error.  */
int
virJSONValueObjectRemoveKey(virJSONValuePtr object, const char *key,
                            virJSONValuePtr *value)
{
458
    size_t i;
459 460 461 462 463 464 465

    if (value)
        *value = NULL;

    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

466
    for (i = 0; i < object->data.object.npairs; i++) {
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
        if (STREQ(object->data.object.pairs[i].key, key)) {
            if (value) {
                *value = object->data.object.pairs[i].value;
                object->data.object.pairs[i].value = NULL;
            }
            VIR_FREE(object->data.object.pairs[i].key);
            virJSONValueFree(object->data.object.pairs[i].value);
            VIR_DELETE_ELEMENT(object->data.object.pairs, i,
                               object->data.object.npairs);
            return 1;
        }
    }

    return 0;
}

483 484 485 486 487 488 489 490 491 492 493
virJSONValuePtr virJSONValueObjectGetValue(virJSONValuePtr object, unsigned int n)
{
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return NULL;

    if (n >= object->data.object.npairs)
        return NULL;

    return object->data.object.pairs[n].value;
}

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
int virJSONValueArraySize(virJSONValuePtr array)
{
    if (array->type != VIR_JSON_TYPE_ARRAY)
        return -1;

    return array->data.array.nvalues;
}


virJSONValuePtr virJSONValueArrayGet(virJSONValuePtr array, unsigned int element)
{
    if (array->type != VIR_JSON_TYPE_ARRAY)
        return NULL;

    if (element >= array->data.array.nvalues)
        return NULL;

    return array->data.array.values[element];
}

514
const char *virJSONValueGetString(virJSONValuePtr string)
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
{
    if (string->type != VIR_JSON_TYPE_STRING)
        return NULL;

    return string->data.string;
}


int virJSONValueGetNumberInt(virJSONValuePtr number, int *value)
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

    return virStrToLong_i(number->data.number, NULL, 10, value);
}

int virJSONValueGetNumberUint(virJSONValuePtr number, unsigned int *value)
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

    return virStrToLong_ui(number->data.number, NULL, 10, value);
}

int virJSONValueGetNumberLong(virJSONValuePtr number, long long *value)
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

    return virStrToLong_ll(number->data.number, NULL, 10, value);
}

int virJSONValueGetNumberUlong(virJSONValuePtr number, unsigned long long *value)
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

    return virStrToLong_ull(number->data.number, NULL, 10, value);
}

int virJSONValueGetNumberDouble(virJSONValuePtr number, double *value)
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

    return virStrToDouble(number->data.number, NULL, value);
}


J
Jiri Denemark 已提交
564
int virJSONValueGetBoolean(virJSONValuePtr val, bool *value)
565
{
J
Jiri Denemark 已提交
566
    if (val->type != VIR_JSON_TYPE_BOOLEAN)
567 568
        return -1;

J
Jiri Denemark 已提交
569 570
    *value = val->data.boolean;
    return 0;
571 572 573 574 575 576 577 578 579 580 581 582
}


int virJSONValueIsNull(virJSONValuePtr val)
{
    if (val->type != VIR_JSON_TYPE_NULL)
        return 0;

    return 1;
}


583
const char *virJSONValueObjectGetString(virJSONValuePtr object, const char *key)
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return NULL;

    val = virJSONValueObjectGet(object, key);
    if (!val)
        return NULL;

    return virJSONValueGetString(val);
}


int virJSONValueObjectGetNumberInt(virJSONValuePtr object, const char *key, int *value)
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    val = virJSONValueObjectGet(object, key);
    if (!val)
        return -1;

    return virJSONValueGetNumberInt(val, value);
}


int virJSONValueObjectGetNumberUint(virJSONValuePtr object, const char *key, unsigned int *value)
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    val = virJSONValueObjectGet(object, key);
    if (!val)
        return -1;

    return virJSONValueGetNumberUint(val, value);
}


int virJSONValueObjectGetNumberLong(virJSONValuePtr object, const char *key, long long *value)
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    val = virJSONValueObjectGet(object, key);
    if (!val)
        return -1;

    return virJSONValueGetNumberLong(val, value);
}


int virJSONValueObjectGetNumberUlong(virJSONValuePtr object, const char *key, unsigned long long *value)
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    val = virJSONValueObjectGet(object, key);
    if (!val)
        return -1;

    return virJSONValueGetNumberUlong(val, value);
}


int virJSONValueObjectGetNumberDouble(virJSONValuePtr object, const char *key, double *value)
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    val = virJSONValueObjectGet(object, key);
    if (!val)
        return -1;

    return virJSONValueGetNumberDouble(val, value);
}


J
Jiri Denemark 已提交
667
int virJSONValueObjectGetBoolean(virJSONValuePtr object, const char *key, bool *value)
668 669 670 671 672 673 674 675 676
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    val = virJSONValueObjectGet(object, key);
    if (!val)
        return -1;

J
Jiri Denemark 已提交
677
    return virJSONValueGetBoolean(val, value);
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
}


int virJSONValueObjectIsNull(virJSONValuePtr object, const char *key)
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    val = virJSONValueObjectGet(object, key);
    if (!val)
        return -1;

    return virJSONValueIsNull(val);
}


695
#if WITH_YAJL
696 697 698 699 700 701 702 703
static int virJSONParserInsertValue(virJSONParserPtr parser,
                                    virJSONValuePtr value)
{
    if (!parser->head) {
        parser->head = value;
    } else {
        virJSONParserStatePtr state;
        if (!parser->nstate) {
704
            VIR_DEBUG("got a value to insert without a container");
705 706 707 708 709 710 711 712
            return -1;
        }

        state = &parser->state[parser->nstate-1];

        switch (state->value->type) {
        case VIR_JSON_TYPE_OBJECT: {
            if (!state->key) {
713
                VIR_DEBUG("missing key when inserting object value");
714 715 716 717 718 719 720 721 722 723 724 725 726
                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) {
727
                VIR_DEBUG("unexpected key when inserting array value");
728 729 730 731 732 733 734 735 736
                return -1;
            }

            if (virJSONValueArrayAppend(state->value,
                                        value) < 0)
                return -1;
        }   break;

        default:
737
            VIR_DEBUG("unexpected value type, not a container");
738 739 740 741 742 743 744
            return -1;
        }
    }

    return 0;
}

745
static int virJSONParserHandleNull(void *ctx)
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
{
    virJSONParserPtr parser = ctx;
    virJSONValuePtr value = virJSONValueNewNull();

    VIR_DEBUG("parser=%p", parser);

    if (!value)
        return 0;

    if (virJSONParserInsertValue(parser, value) < 0) {
        virJSONValueFree(value);
        return 0;
    }

    return 1;
}

763
static int virJSONParserHandleBoolean(void *ctx, int boolean_)
764 765
{
    virJSONParserPtr parser = ctx;
766
    virJSONValuePtr value = virJSONValueNewBoolean(boolean_);
767

768
    VIR_DEBUG("parser=%p boolean=%d", parser, boolean_);
769 770 771 772 773 774 775 776 777 778 779 780

    if (!value)
        return 0;

    if (virJSONParserInsertValue(parser, value) < 0) {
        virJSONValueFree(value);
        return 0;
    }

    return 1;
}

781 782 783
static int virJSONParserHandleNumber(void *ctx,
                                     const char *s,
                                     yajl_size_t l)
784 785
{
    virJSONParserPtr parser = ctx;
786
    char *str;
787 788
    virJSONValuePtr value;

789
    if (VIR_STRNDUP(str, s, l) < 0)
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
        return -1;
    value = virJSONValueNewNumber(str);
    VIR_FREE(str);

    VIR_DEBUG("parser=%p str=%s", parser, str);

    if (!value)
        return 0;

    if (virJSONParserInsertValue(parser, value) < 0) {
        virJSONValueFree(value);
        return 0;
    }

    return 1;
}

807 808 809
static int virJSONParserHandleString(void *ctx,
                                     const unsigned char *stringVal,
                                     yajl_size_t stringLen)
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
{
    virJSONParserPtr parser = ctx;
    virJSONValuePtr value = virJSONValueNewStringLen((const char *)stringVal,
                                                     stringLen);

    VIR_DEBUG("parser=%p str=%p", parser, (const char *)stringVal);

    if (!value)
        return 0;

    if (virJSONParserInsertValue(parser, value) < 0) {
        virJSONValueFree(value);
        return 0;
    }

    return 1;
}

828 829 830
static int virJSONParserHandleMapKey(void *ctx,
                                     const unsigned char *stringVal,
                                     yajl_size_t stringLen)
831 832 833 834 835 836 837 838 839 840 841 842
{
    virJSONParserPtr parser = ctx;
    virJSONParserStatePtr 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;
843
    if (VIR_STRNDUP(state->key, (const char *)stringVal, stringLen) < 0)
844 845 846 847
        return 0;
    return 1;
}

848
static int virJSONParserHandleStartMap(void *ctx)
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
{
    virJSONParserPtr parser = ctx;
    virJSONValuePtr value = virJSONValueNewObject();

    VIR_DEBUG("parser=%p", parser);

    if (!value)
        return 0;

    if (virJSONParserInsertValue(parser, value) < 0) {
        virJSONValueFree(value);
        return 0;
    }

    if (VIR_REALLOC_N(parser->state,
864
                      parser->nstate + 1) < 0) {
865
        return 0;
866
    }
867 868 869 870 871 872 873 874 875

    parser->state[parser->nstate].value = value;
    parser->state[parser->nstate].key = NULL;
    parser->nstate++;

    return 1;
}


876
static int virJSONParserHandleEndMap(void *ctx)
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
{
    virJSONParserPtr parser = ctx;
    virJSONParserStatePtr 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;
    }

    if (VIR_REALLOC_N(parser->state,
                      parser->nstate - 1) < 0)
        return 0;
    parser->nstate--;

    return 1;
}

900
static int virJSONParserHandleStartArray(void *ctx)
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
{
    virJSONParserPtr parser = ctx;
    virJSONValuePtr value = virJSONValueNewArray();

    VIR_DEBUG("parser=%p", parser);

    if (!value)
        return 0;

    if (virJSONParserInsertValue(parser, value) < 0) {
        virJSONValueFree(value);
        return 0;
    }

    if (VIR_REALLOC_N(parser->state,
                      parser->nstate + 1) < 0)
        return 0;

    parser->state[parser->nstate].value = value;
    parser->state[parser->nstate].key = NULL;
    parser->nstate++;

    return 1;
}

926
static int virJSONParserHandleEndArray(void *ctx)
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
{
    virJSONParserPtr parser = ctx;
    virJSONParserStatePtr 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;
    }

    if (VIR_REALLOC_N(parser->state,
                      parser->nstate - 1) < 0)
        return 0;
    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 */
virJSONValuePtr virJSONValueFromString(const char *jsonstring)
{
    yajl_handle hand;
    virJSONParser parser = { NULL, NULL, 0 };
970
    virJSONValuePtr ret = NULL;
971
# ifndef WITH_YAJL2
972 973
    yajl_parser_config cfg = { 1, 1 };
# endif
974 975 976

    VIR_DEBUG("string=%s", jsonstring);

977
# ifdef WITH_YAJL2
978 979 980 981 982 983
    hand = yajl_alloc(&parserCallbacks, NULL, &parser);
    if (hand) {
        yajl_config(hand, yajl_allow_comments, 1);
        yajl_config(hand, yajl_dont_validate_strings, 0);
    }
# else
984
    hand = yajl_alloc(&parserCallbacks, &cfg, NULL, &parser);
985 986
# endif
    if (!hand) {
987 988
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to create JSON parser"));
989 990
        goto cleanup;
    }
991 992 993 994 995 996 997 998

    if (yajl_parse(hand,
                   (const unsigned char *)jsonstring,
                   strlen(jsonstring)) != yajl_status_ok) {
        unsigned char *errstr = yajl_get_error(hand, 1,
                                               (const unsigned char*)jsonstring,
                                               strlen(jsonstring));

999 1000 1001
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot parse json %s: %s"),
                       jsonstring, (const char*) errstr);
1002 1003 1004 1005 1006
        VIR_FREE(errstr);
        virJSONValueFree(parser.head);
        goto cleanup;
    }

1007 1008
    ret = parser.head;

1009 1010 1011 1012
cleanup:
    yajl_free(hand);

    if (parser.nstate) {
1013
        size_t i;
1014
        for (i = 0; i < parser.nstate; i++) {
1015 1016 1017 1018 1019 1020
            VIR_FREE(parser.state[i].key);
        }
    }

    VIR_DEBUG("result=%p", parser.head);

1021
    return ret;
1022 1023 1024 1025 1026 1027
}


static int virJSONValueToStringOne(virJSONValuePtr object,
                                   yajl_gen g)
{
1028
    size_t i;
1029 1030 1031 1032 1033 1034 1035

    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;
1036
        for (i = 0; i < object->data.object.npairs; i++) {
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
            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;
1051
        for (i = 0; i < object->data.array.nvalues; i++) {
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
            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;
}

1088 1089
char *virJSONValueToString(virJSONValuePtr object,
                           bool pretty)
1090 1091 1092 1093
{
    yajl_gen g;
    const unsigned char *str;
    char *ret = NULL;
1094
    yajl_size_t len;
1095
# ifndef WITH_YAJL2
1096
    yajl_gen_config conf = { pretty ? 1 : 0, pretty ? "    " : " "};
1097
# endif
1098 1099 1100

    VIR_DEBUG("object=%p", object);

1101
# ifdef WITH_YAJL2
1102 1103
    g = yajl_gen_alloc(NULL);
    if (g) {
1104 1105
        yajl_gen_config(g, yajl_gen_beautify, pretty ? 1 : 0);
        yajl_gen_config(g, yajl_gen_indent_string, pretty ? "    " : " ");
1106 1107 1108
        yajl_gen_config(g, yajl_gen_validate_utf8, 1);
    }
# else
1109
    g = yajl_gen_alloc(&conf, NULL);
1110 1111
# endif
    if (!g) {
1112 1113
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to create JSON formatter"));
1114 1115
        goto cleanup;
    }
1116 1117

    if (virJSONValueToStringOne(object, g) < 0) {
1118
        virReportOOMError();
1119 1120 1121 1122
        goto cleanup;
    }

    if (yajl_gen_get_buf(g, &str, &len) != yajl_gen_status_ok) {
1123
        virReportOOMError();
1124 1125 1126
        goto cleanup;
    }

1127
    ignore_value(VIR_STRDUP(ret, (const char *)str));
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140

cleanup:
    yajl_gen_free(g);

    VIR_DEBUG("result=%s", NULLSTR(ret));

    return ret;
}


#else
virJSONValuePtr virJSONValueFromString(const char *jsonstring ATTRIBUTE_UNUSED)
{
1141 1142
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("No JSON parser implementation is available"));
1143 1144
    return NULL;
}
1145 1146
char *virJSONValueToString(virJSONValuePtr object ATTRIBUTE_UNUSED,
                           bool pretty ATTRIBUTE_UNUSED)
1147
{
1148 1149
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("No JSON parser implementation is available"));
1150 1151 1152
    return NULL;
}
#endif