Пример #1
0
static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
                                  const VMStateField *field, int i, int max)
{
    char *name, *old_name;
    bool is_array = max > 1;
    bool can_compress = vmsd_can_compress(field);

    if (!vmdesc) {
        return;
    }

    name = g_strdup(field->name);

    /* Field name is not unique, need to make it unique */
    if (!vmfield_name_is_unique(vmsd->fields, field)) {
        int num = vmfield_name_num(vmsd->fields, field);
        old_name = name;
        name = g_strdup_printf("%s[%d]", name, num);
        g_free(old_name);
    }

    json_start_object(vmdesc, NULL);
    json_prop_str(vmdesc, "name", name);
    if (is_array) {
        if (can_compress) {
            json_prop_int(vmdesc, "array_len", max);
        } else {
            json_prop_int(vmdesc, "index", i);
        }
    }
    json_prop_str(vmdesc, "type", vmfield_get_type_name(field));

    if (field->flags & VMS_STRUCT) {
        json_start_object(vmdesc, "struct");
    }

    g_free(name);
}
Пример #2
0
static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
                                   void *opaque, QJSON *vmdesc)
{
    const VMStateDescription **sub = vmsd->subsections;
    bool subsection_found = false;
    int ret = 0;

    trace_vmstate_subsection_save_top(vmsd->name);
    while (sub && *sub) {
        if (vmstate_save_needed(*sub, opaque)) {
            const VMStateDescription *vmsdsub = *sub;
            uint8_t len;

            trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
            if (vmdesc) {
                /* Only create subsection array when we have any */
                if (!subsection_found) {
                    json_start_array(vmdesc, "subsections");
                    subsection_found = true;
                }

                json_start_object(vmdesc, NULL);
            }

            qemu_put_byte(f, QEMU_VM_SUBSECTION);
            len = strlen(vmsdsub->name);
            qemu_put_byte(f, len);
            qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
            qemu_put_be32(f, vmsdsub->version_id);
            ret = vmstate_save_state(f, vmsdsub, opaque, vmdesc);
            if (ret) {
                return ret;
            }

            if (vmdesc) {
                json_end_object(vmdesc);
            }
        }
        sub++;
    }

    if (vmdesc && subsection_found) {
        json_end_array(vmdesc);
    }

    return ret;
}
Пример #3
0
void json_test(int argc, char **argv)
{
	int num;
	int fmt;
	char text[] =
	    "{\n     \"precision\": \"zip\",\n       \"Latitude\":  37668,\n       \"Longitude\": -12259,\n     \"Address\":   \"\",\n  \"City\":      \"SAN FRANCISCO\",\n     \"State\":     \"CA\",\n        \"Zip\":       \"94107\",\n     \"Country\":   \"US\"\n                                                                                                    }";
	char complex_obj[] =
	    "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", 		\"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\",\"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", 	\"Abbrev\": \"ISO 8879:1986\", 	\"GlossDef\": { \"para\": \"language\",	\"GlossSeeAlso\": \"XML\" },	\"GlossSee\": \"markup\"  } } } } }";

	debug("#### json_parser ####\n");

	json_object_init(&tmp, text);

	if (!json_get_val_str(&tmp, "Country", str, MAX_JSON_STR_LEN)) {
		if (!strcmp(str, "US")) {
			debug("***Json_parser ---str_get match\n");
		} else {
			debug("***Err ---str_get mismatch\n");
			goto ERROR;
		}
	} else {
		debug("***Err ---str_get failed\n");
		goto ERROR;
	}

	if (!json_get_val_int(&tmp, "Longitude", &num)) {
		if (num == -12259) {
			debug("***Json_parser ---int_get match\n");
		} else {
			debug("***Err ---int_get mismatch\n");
			goto ERROR;
		}
	} else {
		debug("***Err ---int_get failed\n");
		goto ERROR;
	}

	fmt = 1;
	debug("***Json_parser ---Simple Set Example\n");
	json_str_init(&jstr, tmpstr, sizeof(tmpstr), fmt);
	json_start_object(&jstr);
	json_set_val_str(&jstr, "name", "John Galt");
	json_set_val_int(&jstr, "age", 29);
	json_set_val_str(&jstr, "numberstr", "123");
	json_close_object(&jstr);
	debug("The JSON String is :%s:\n", jstr.buff);

	debug("***Json_parser --- Complex object Set Example\n");
	json_str_init(&jstr_c, tmpstr, sizeof(tmpstr), fmt);
	json_start_object(&jstr_c);
	json_push_object(&jstr_c, "menu");
	json_set_val_str(&jstr_c, "pictures", "tmp");

	json_push_object(&jstr_c, "Image");
	json_set_val_str(&jstr_c, "name", "sun");
	json_set_val_int(&jstr_c, "length", 1000);
	json_set_val_str(&jstr_c, "size", "1600pi");

	json_pop_object(&jstr_c);	/* Close Image */

	json_push_object(&jstr_c, "text");
	json_set_val_str(&jstr_c, "style", "bold");
	json_set_val_str(&jstr_c, "alignment", "center");
	json_set_val_str(&jstr_c, "name", "text1");

	json_pop_object(&jstr_c);	/* Close text */
	json_set_val_str(&jstr_c, "object", "close");
	json_pop_object(&jstr_c);	/* Close menu */
	json_close_object(&jstr_c);	/* Close main */

	debug("The JSON String is *%s*\n", jstr_c.buff);

	struct json_object obj;
	json_object_init(&obj, jstr_c.buff);
	if (!json_get_composite_object(&obj, "text")) {
		if (!json_get_val_int(&obj, "length", &num))
			debug("val is %d \n", num);
		else
			debug("***Json_parser ---passed, no such member\n");

		if (!json_get_val_str(&obj, "style", str, MAX_JSON_STR_LEN)) {
			if (!strcmp(str, "bold")) {
				debug
				    ("***Json_parser ---composite_str_get match\n");
				debug("member->style:val->%s\n", str);
			} else {
				debug("***Err ---composite_str_get mismatch\n");
				goto ERROR;
			}
		} else {
			debug("***Err ---composite_str_get failed\n");
			goto ERROR;
		}

	} else {
		debug("***Err ---get_object_offsets failed\n");
		goto ERROR;
	}

	json_release_composite_object(&obj);

	json_object_init(&obj, complex_obj);
	if (!json_get_composite_object(&obj, "GlossEntry")) {
		if (!json_get_val_str(&obj, "GlossSee", str, 64)) {
			if (!strcmp(str, "markup")) {
				debug
				    ("***Json_parser ---composite_str_get match\n");
				debug("member->GlossEntry:val->%s\n", str);
			} else {
				debug("***Err ---composite_str_get mismatch\n");
				goto ERROR;
			}
		} else {
			debug("***Err ---composite_str_get failed\n");
			goto ERROR;
		}
	} else {
		debug("***Err ---get_object_offsets failed\n");
		goto ERROR;
	}

	json_release_composite_object(&obj);
	goto SUCCESS;
ERROR:
	wmprintf("Error");
SUCCESS:
	wmprintf("Success");
}