virjson.c 29.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

/* XXX fixme */
#define VIR_FROM_THIS VIR_FROM_NONE

48
VIR_LOG_INIT("util.json");
49 50 51 52 53 54 55 56 57 58 59 60 61

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;
62
    size_t nstate;
63 64 65
};


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

E
Eric Blake 已提交
73
    switch ((virJSONType) value->type) {
74
    case VIR_JSON_TYPE_OBJECT:
75
        for (i = 0; i < value->data.object.npairs; i++) {
76 77 78 79 80 81
            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:
82
        for (i = 0; i < value->data.array.nvalues; i++)
83 84 85 86 87 88 89 90 91
            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 已提交
92 93 94
    case VIR_JSON_TYPE_BOOLEAN:
    case VIR_JSON_TYPE_NULL:
        break;
95
    }
96 97

    VIR_FREE(value);
98 99 100
}


101 102
virJSONValuePtr
virJSONValueNewString(const char *data)
103 104 105 106 107 108 109 110 111 112
{
    virJSONValuePtr val;

    if (!data)
        return virJSONValueNewNull();

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

    val->type = VIR_JSON_TYPE_STRING;
113
    if (VIR_STRDUP(val->data.string, data) < 0) {
114 115 116 117 118 119 120
        VIR_FREE(val);
        return NULL;
    }

    return val;
}

121 122 123 124

virJSONValuePtr
virJSONValueNewStringLen(const char *data,
                         size_t length)
125 126 127 128 129 130 131 132 133 134
{
    virJSONValuePtr val;

    if (!data)
        return virJSONValueNewNull();

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

    val->type = VIR_JSON_TYPE_STRING;
135
    if (VIR_STRNDUP(val->data.string, data, length) < 0) {
136 137 138 139 140 141 142
        VIR_FREE(val);
        return NULL;
    }

    return val;
}

143 144 145

static virJSONValuePtr
virJSONValueNewNumber(const char *data)
146 147 148 149 150 151 152
{
    virJSONValuePtr val;

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

    val->type = VIR_JSON_TYPE_NUMBER;
153
    if (VIR_STRDUP(val->data.number, data) < 0) {
154 155 156 157 158 159 160
        VIR_FREE(val);
        return NULL;
    }

    return val;
}

161 162 163

virJSONValuePtr
virJSONValueNewNumberInt(int data)
164 165 166 167 168 169 170 171 172 173 174
{
    virJSONValuePtr val = NULL;
    char *str;
    if (virAsprintf(&str, "%i", data) < 0)
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


175 176
virJSONValuePtr
virJSONValueNewNumberUint(unsigned int data)
177 178 179 180 181 182 183 184 185 186 187
{
    virJSONValuePtr val = NULL;
    char *str;
    if (virAsprintf(&str, "%u", data) < 0)
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


188 189
virJSONValuePtr
virJSONValueNewNumberLong(long long data)
190 191 192 193 194 195 196 197 198 199 200
{
    virJSONValuePtr val = NULL;
    char *str;
    if (virAsprintf(&str, "%lld", data) < 0)
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


201 202
virJSONValuePtr
virJSONValueNewNumberUlong(unsigned long long data)
203 204 205 206 207 208 209 210 211 212 213
{
    virJSONValuePtr val = NULL;
    char *str;
    if (virAsprintf(&str, "%llu", data) < 0)
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


214 215
virJSONValuePtr
virJSONValueNewNumberDouble(double data)
216 217 218
{
    virJSONValuePtr val = NULL;
    char *str;
219
    if (virDoubleToStr(&str, data) < 0)
220 221 222 223 224 225 226
        return NULL;
    val = virJSONValueNewNumber(str);
    VIR_FREE(str);
    return val;
}


227 228
virJSONValuePtr
virJSONValueNewBoolean(int boolean_)
229 230 231 232 233 234 235
{
    virJSONValuePtr val;

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

    val->type = VIR_JSON_TYPE_BOOLEAN;
236
    val->data.boolean = boolean_;
237 238 239 240

    return val;
}

241 242 243

virJSONValuePtr
virJSONValueNewNull(void)
244 245 246 247 248 249 250 251 252 253 254
{
    virJSONValuePtr val;

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

    val->type = VIR_JSON_TYPE_NULL;

    return val;
}

255 256 257

virJSONValuePtr
virJSONValueNewArray(void)
258 259 260 261 262 263 264 265 266 267 268
{
    virJSONValuePtr val;

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

    val->type = VIR_JSON_TYPE_ARRAY;

    return val;
}

269 270 271

virJSONValuePtr
virJSONValueNewObject(void)
272 273 274 275 276 277 278 279 280 281 282
{
    virJSONValuePtr val;

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

    val->type = VIR_JSON_TYPE_OBJECT;

    return val;
}

283 284 285 286 287

int
virJSONValueObjectAppend(virJSONValuePtr object,
                         const char *key,
                         virJSONValuePtr value)
288 289 290 291 292 293 294 295 296
{
    char *newkey;

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

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

297
    if (VIR_STRDUP(newkey, key) < 0)
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        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;
}


314 315 316 317
int
virJSONValueObjectAppendString(virJSONValuePtr object,
                               const char *key,
                               const char *value)
318 319 320 321 322 323 324 325 326 327 328
{
    virJSONValuePtr jvalue = virJSONValueNewString(value);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

329 330 331 332 333

int
virJSONValueObjectAppendNumberInt(virJSONValuePtr object,
                                  const char *key,
                                  int number)
334 335 336 337 338 339 340 341 342 343 344 345
{
    virJSONValuePtr jvalue = virJSONValueNewNumberInt(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}


346 347 348 349
int
virJSONValueObjectAppendNumberUint(virJSONValuePtr object,
                                   const char *key,
                                   unsigned int number)
350 351 352 353 354 355 356 357 358 359 360
{
    virJSONValuePtr jvalue = virJSONValueNewNumberUint(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

361 362 363 364 365

int
virJSONValueObjectAppendNumberLong(virJSONValuePtr object,
                                   const char *key,
                                   long long number)
366 367 368 369 370 371 372 373 374 375 376
{
    virJSONValuePtr jvalue = virJSONValueNewNumberLong(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

377 378 379 380 381

int
virJSONValueObjectAppendNumberUlong(virJSONValuePtr object,
                                    const char *key,
                                    unsigned long long number)
382 383 384 385 386 387 388 389 390 391 392
{
    virJSONValuePtr jvalue = virJSONValueNewNumberUlong(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

393 394 395 396 397

int
virJSONValueObjectAppendNumberDouble(virJSONValuePtr object,
                                     const char *key,
                                     double number)
398 399 400 401 402 403 404 405 406 407 408
{
    virJSONValuePtr jvalue = virJSONValueNewNumberDouble(number);
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

409 410 411 412 413

int
virJSONValueObjectAppendBoolean(virJSONValuePtr object,
                                const char *key,
                                int boolean_)
414
{
415
    virJSONValuePtr jvalue = virJSONValueNewBoolean(boolean_);
416 417 418 419 420 421 422 423 424
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}

425 426 427 428

int
virJSONValueObjectAppendNull(virJSONValuePtr object,
                             const char *key)
429 430 431 432 433 434 435 436 437 438 439 440
{
    virJSONValuePtr jvalue = virJSONValueNewNull();
    if (!jvalue)
        return -1;
    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
        virJSONValueFree(jvalue);
        return -1;
    }
    return 0;
}


441 442 443
int
virJSONValueArrayAppend(virJSONValuePtr array,
                        virJSONValuePtr value)
444 445 446 447 448 449 450 451 452 453 454 455 456 457
{
    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;
}

458 459 460 461

int
virJSONValueObjectHasKey(virJSONValuePtr object,
                         const char *key)
462
{
463
    size_t i;
464 465 466 467

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

468
    for (i = 0; i < object->data.object.npairs; i++) {
469 470 471 472 473 474 475
        if (STREQ(object->data.object.pairs[i].key, key))
            return 1;
    }

    return 0;
}

476 477 478 479

virJSONValuePtr
virJSONValueObjectGet(virJSONValuePtr object,
                      const char *key)
480
{
481
    size_t i;
482 483 484 485

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

486
    for (i = 0; i < object->data.object.npairs; i++) {
487 488 489 490 491 492 493
        if (STREQ(object->data.object.pairs[i].key, key))
            return object->data.object.pairs[i].value;
    }

    return NULL;
}

494 495 496

int
virJSONValueObjectKeysNumber(virJSONValuePtr object)
497 498 499 500 501 502 503
{
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

    return object->data.object.npairs;
}

504 505 506 507

const char *
virJSONValueObjectGetKey(virJSONValuePtr object,
                         unsigned int n)
508 509 510 511 512 513 514 515 516 517
{
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return NULL;

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

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

518

519 520 521 522
/* 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
523 524
virJSONValueObjectRemoveKey(virJSONValuePtr object,
                            const char *key,
525 526
                            virJSONValuePtr *value)
{
527
    size_t i;
528 529 530 531 532 533 534

    if (value)
        *value = NULL;

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

535
    for (i = 0; i < object->data.object.npairs; i++) {
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        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;
}

552 553 554 555

virJSONValuePtr
virJSONValueObjectGetValue(virJSONValuePtr object,
                           unsigned int n)
556 557 558 559 560 561 562 563 564 565
{
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return NULL;

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

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

566

567 568 569 570 571 572 573
bool
virJSONValueIsArray(virJSONValuePtr array)
{
    return array->type == VIR_JSON_TYPE_ARRAY;
}


574 575
int
virJSONValueArraySize(virJSONValuePtr array)
576 577 578 579 580 581 582 583
{
    if (array->type != VIR_JSON_TYPE_ARRAY)
        return -1;

    return array->data.array.nvalues;
}


584 585 586
virJSONValuePtr
virJSONValueArrayGet(virJSONValuePtr array,
                     unsigned int element)
587 588 589 590 591 592 593 594 595 596
{
    if (array->type != VIR_JSON_TYPE_ARRAY)
        return NULL;

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

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

597

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
virJSONValuePtr
virJSONValueArraySteal(virJSONValuePtr array,
                       unsigned int element)
{
    virJSONValuePtr ret = NULL;

    if (array->type != VIR_JSON_TYPE_ARRAY)
        return NULL;

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

    ret = array->data.array.values[element];

    VIR_DELETE_ELEMENT(array->data.array.values,
                       element,
                       array->data.array.nvalues);

    return ret;
}


620 621
const char *
virJSONValueGetString(virJSONValuePtr string)
622 623 624 625 626 627 628 629
{
    if (string->type != VIR_JSON_TYPE_STRING)
        return NULL;

    return string->data.string;
}


630 631 632
int
virJSONValueGetNumberInt(virJSONValuePtr number,
                         int *value)
633 634 635 636 637 638 639
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

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

640 641 642 643

int
virJSONValueGetNumberUint(virJSONValuePtr number,
                          unsigned int *value)
644 645 646 647 648 649 650
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

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

651 652 653 654

int
virJSONValueGetNumberLong(virJSONValuePtr number,
                          long long *value)
655 656 657 658 659 660 661
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

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

662 663 664 665

int
virJSONValueGetNumberUlong(virJSONValuePtr number,
                           unsigned long long *value)
666 667 668 669 670 671 672
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

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

673 674 675 676

int
virJSONValueGetNumberDouble(virJSONValuePtr number,
                            double *value)
677 678 679 680 681 682 683 684
{
    if (number->type != VIR_JSON_TYPE_NUMBER)
        return -1;

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


685 686 687
int
virJSONValueGetBoolean(virJSONValuePtr val,
                       bool *value)
688
{
J
Jiri Denemark 已提交
689
    if (val->type != VIR_JSON_TYPE_BOOLEAN)
690 691
        return -1;

J
Jiri Denemark 已提交
692 693
    *value = val->data.boolean;
    return 0;
694 695 696
}


697 698
int
virJSONValueIsNull(virJSONValuePtr val)
699 700 701 702 703 704 705 706
{
    if (val->type != VIR_JSON_TYPE_NULL)
        return 0;

    return 1;
}


707 708 709
const char *
virJSONValueObjectGetString(virJSONValuePtr object,
                            const char *key)
710 711 712 713 714 715 716 717 718 719 720 721 722
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return NULL;

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

    return virJSONValueGetString(val);
}


723 724 725 726
int
virJSONValueObjectGetNumberInt(virJSONValuePtr object,
                               const char *key,
                               int *value)
727 728 729 730 731 732 733 734 735 736 737 738 739
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

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

    return virJSONValueGetNumberInt(val, value);
}


740 741 742 743
int
virJSONValueObjectGetNumberUint(virJSONValuePtr object,
                                const char *key,
                                unsigned int *value)
744 745 746 747 748 749 750 751 752 753 754 755 756
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

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

    return virJSONValueGetNumberUint(val, value);
}


757 758 759 760
int
virJSONValueObjectGetNumberLong(virJSONValuePtr object,
                                const char *key,
                                long long *value)
761 762 763 764 765 766 767 768 769 770 771 772 773
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

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

    return virJSONValueGetNumberLong(val, value);
}


774 775 776 777
int
virJSONValueObjectGetNumberUlong(virJSONValuePtr object,
                                 const char *key,
                                 unsigned long long *value)
778 779 780 781 782 783 784 785 786 787 788 789 790
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

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

    return virJSONValueGetNumberUlong(val, value);
}


791 792 793 794
int
virJSONValueObjectGetNumberDouble(virJSONValuePtr object,
                                  const char *key,
                                  double *value)
795 796 797 798 799 800 801 802 803 804 805 806 807
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

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

    return virJSONValueGetNumberDouble(val, value);
}


808 809 810 811
int
virJSONValueObjectGetBoolean(virJSONValuePtr object,
                             const char *key,
                             bool *value)
812 813 814 815 816 817 818 819 820
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

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

J
Jiri Denemark 已提交
821
    return virJSONValueGetBoolean(val, value);
822 823 824
}


825 826 827
int
virJSONValueObjectIsNull(virJSONValuePtr object,
                         const char *key)
828 829 830 831 832 833 834 835 836 837 838 839 840
{
    virJSONValuePtr val;
    if (object->type != VIR_JSON_TYPE_OBJECT)
        return -1;

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

    return virJSONValueIsNull(val);
}


841
#if WITH_YAJL
842 843 844
static int
virJSONParserInsertValue(virJSONParserPtr parser,
                         virJSONValuePtr value)
845 846 847 848 849 850
{
    if (!parser->head) {
        parser->head = value;
    } else {
        virJSONParserStatePtr state;
        if (!parser->nstate) {
851
            VIR_DEBUG("got a value to insert without a container");
852 853 854 855 856 857 858 859
            return -1;
        }

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

        switch (state->value->type) {
        case VIR_JSON_TYPE_OBJECT: {
            if (!state->key) {
860
                VIR_DEBUG("missing key when inserting object value");
861 862 863 864 865 866 867 868 869 870 871 872 873
                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) {
874
                VIR_DEBUG("unexpected key when inserting array value");
875 876 877 878 879 880 881 882 883
                return -1;
            }

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

        default:
884
            VIR_DEBUG("unexpected value type, not a container");
885 886 887 888 889 890 891
            return -1;
        }
    }

    return 0;
}

892 893 894

static int
virJSONParserHandleNull(void *ctx)
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
{
    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;
}

912 913 914 915

static int
virJSONParserHandleBoolean(void *ctx,
                           int boolean_)
916 917
{
    virJSONParserPtr parser = ctx;
918
    virJSONValuePtr value = virJSONValueNewBoolean(boolean_);
919

920
    VIR_DEBUG("parser=%p boolean=%d", parser, boolean_);
921 922 923 924 925 926 927 928 929 930 931 932

    if (!value)
        return 0;

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

    return 1;
}

933 934 935 936 937

static int
virJSONParserHandleNumber(void *ctx,
                          const char *s,
                          yajl_size_t l)
938 939
{
    virJSONParserPtr parser = ctx;
940
    char *str;
941 942
    virJSONValuePtr value;

943
    if (VIR_STRNDUP(str, s, l) < 0)
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
        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;
}

961 962 963 964 965

static int
virJSONParserHandleString(void *ctx,
                          const unsigned char *stringVal,
                          yajl_size_t stringLen)
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
{
    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;
}

984 985 986 987 988

static int
virJSONParserHandleMapKey(void *ctx,
                          const unsigned char *stringVal,
                          yajl_size_t stringLen)
989 990 991 992 993 994 995 996 997 998 999 1000
{
    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;
1001
    if (VIR_STRNDUP(state->key, (const char *)stringVal, stringLen) < 0)
1002 1003 1004 1005
        return 0;
    return 1;
}

1006 1007 1008

static int
virJSONParserHandleStartMap(void *ctx)
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
{
    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,
1024
                      parser->nstate + 1) < 0) {
1025
        return 0;
1026
    }
1027 1028 1029 1030 1031 1032 1033 1034 1035

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

    return 1;
}


1036 1037
static int
virJSONParserHandleEndMap(void *ctx)
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
{
    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;
    }

1053
    VIR_DELETE_ELEMENT(parser->state, parser->nstate - 1, parser->nstate);
1054 1055 1056 1057

    return 1;
}

1058 1059 1060

static int
virJSONParserHandleStartArray(void *ctx)
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
{
    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;
}

1086 1087 1088

static int
virJSONParserHandleEndArray(void *ctx)
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
{
    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;
    }

1104
    VIR_DELETE_ELEMENT(parser->state, parser->nstate - 1, parser->nstate);
1105 1106 1107 1108

    return 1;
}

1109

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
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 */
1126 1127
virJSONValuePtr
virJSONValueFromString(const char *jsonstring)
1128 1129 1130
{
    yajl_handle hand;
    virJSONParser parser = { NULL, NULL, 0 };
1131
    virJSONValuePtr ret = NULL;
1132
# ifndef WITH_YAJL2
1133 1134
    yajl_parser_config cfg = { 1, 1 };
# endif
1135 1136 1137

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

1138
# ifdef WITH_YAJL2
1139 1140 1141 1142 1143 1144
    hand = yajl_alloc(&parserCallbacks, NULL, &parser);
    if (hand) {
        yajl_config(hand, yajl_allow_comments, 1);
        yajl_config(hand, yajl_dont_validate_strings, 0);
    }
# else
1145
    hand = yajl_alloc(&parserCallbacks, &cfg, NULL, &parser);
1146 1147
# endif
    if (!hand) {
1148 1149
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to create JSON parser"));
1150 1151
        goto cleanup;
    }
1152 1153 1154 1155 1156 1157 1158 1159

    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));

1160 1161 1162
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot parse json %s: %s"),
                       jsonstring, (const char*) errstr);
1163 1164 1165 1166 1167
        VIR_FREE(errstr);
        virJSONValueFree(parser.head);
        goto cleanup;
    }

1168 1169 1170 1171 1172 1173 1174 1175
    if (parser.nstate != 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot parse json %s: unterminated string/map/array"),
                       jsonstring);
        virJSONValueFree(parser.head);
    } else {
        ret = parser.head;
    }
1176

1177
 cleanup:
1178 1179 1180
    yajl_free(hand);

    if (parser.nstate) {
1181
        size_t i;
J
Ján Tomko 已提交
1182
        for (i = 0; i < parser.nstate; i++)
1183
            VIR_FREE(parser.state[i].key);
1184
        VIR_FREE(parser.state);
1185 1186 1187 1188
    }

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

1189
    return ret;
1190 1191 1192
}


1193 1194 1195
static int
virJSONValueToStringOne(virJSONValuePtr object,
                        yajl_gen g)
1196
{
1197
    size_t i;
1198 1199 1200 1201 1202 1203 1204

    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;
1205
        for (i = 0; i < object->data.object.npairs; i++) {
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
            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;
1220
        for (i = 0; i < object->data.array.nvalues; i++) {
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
            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;
}

1257 1258 1259 1260

char *
virJSONValueToString(virJSONValuePtr object,
                     bool pretty)
1261 1262 1263 1264
{
    yajl_gen g;
    const unsigned char *str;
    char *ret = NULL;
1265
    yajl_size_t len;
1266
# ifndef WITH_YAJL2
1267
    yajl_gen_config conf = { pretty ? 1 : 0, pretty ? "    " : " "};
1268
# endif
1269 1270 1271

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

1272
# ifdef WITH_YAJL2
1273 1274
    g = yajl_gen_alloc(NULL);
    if (g) {
1275 1276
        yajl_gen_config(g, yajl_gen_beautify, pretty ? 1 : 0);
        yajl_gen_config(g, yajl_gen_indent_string, pretty ? "    " : " ");
1277 1278 1279
        yajl_gen_config(g, yajl_gen_validate_utf8, 1);
    }
# else
1280
    g = yajl_gen_alloc(&conf, NULL);
1281 1282
# endif
    if (!g) {
1283 1284
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to create JSON formatter"));
1285 1286
        goto cleanup;
    }
1287 1288

    if (virJSONValueToStringOne(object, g) < 0) {
1289
        virReportOOMError();
1290 1291 1292 1293
        goto cleanup;
    }

    if (yajl_gen_get_buf(g, &str, &len) != yajl_gen_status_ok) {
1294
        virReportOOMError();
1295 1296 1297
        goto cleanup;
    }

1298
    ignore_value(VIR_STRDUP(ret, (const char *)str));
1299

1300
 cleanup:
1301 1302 1303 1304 1305 1306 1307 1308 1309
    yajl_gen_free(g);

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

    return ret;
}


#else
1310 1311
virJSONValuePtr
virJSONValueFromString(const char *jsonstring ATTRIBUTE_UNUSED)
1312
{
1313 1314
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("No JSON parser implementation is available"));
1315 1316
    return NULL;
}
1317 1318 1319 1320 1321


char *
virJSONValueToString(virJSONValuePtr object ATTRIBUTE_UNUSED,
                     bool pretty ATTRIBUTE_UNUSED)
1322
{
1323 1324
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("No JSON parser implementation is available"));
1325 1326 1327
    return NULL;
}
#endif