示例#1
0
/* Build an object from the text. */
static const char *parse_object(srjson_doc_t *doc, srjson_t *item, const char *value)
{
    srjson_t *child;
    if (*value != '{') {
        ep = value;
        return 0;
    }			/* not an object! */
    item->type = srjson_Object;
    value = skip(value + 1);
    if (*value == '}')
        return value + 1;	/* empty array. */

    item->child = child = srjson_New_Item(doc);
    if (!item->child)
        return 0;
    value = skip(parse_string(doc, child, skip(value)));
    if (!value)
        return 0;
    child->string = child->valuestring;
    child->valuestring = 0;
    if (*value != ':') {
        ep = value;
        return 0;
    }			/* fail! */
    value = skip(parse_value(doc, child, skip(value + 1)));	/* skip any spacing, get
								 * the value. */
    if (!value)
        return 0;

    while (*value == ',') {
        srjson_t *new_item;
        if (!(new_item = srjson_New_Item(doc)))
            return 0;	/* memory fail */
        child->next = new_item;
        new_item->prev = child;
        child = new_item;
        value = skip(parse_string(doc, child, skip(value + 1)));
        if (!value)
            return 0;
        child->string = child->valuestring;
        child->valuestring = 0;
        if (*value != ':') {
            ep = value;
            return 0;
        }		/* fail! */
        value = skip(parse_value(doc, child, skip(value + 1)));	/* skip any spacing, get
									 * the value. */
        if (!value)
            return 0;
    }

    if (*value == '}')
        return value + 1;	/* end of array */
    ep = value;
    return 0;		/* malformed. */
}
示例#2
0
srjson_t *srjson_CreateStr(srjson_doc_t *doc, const char *string, int len) {
	srjson_t *item = srjson_New_Item(doc);
	if (item) {
		item->type = srjson_String;
		item->valuestring = srjson_strndupz(doc, string, len);
	} return item;
}
示例#3
0
srjson_t *srjson_CreateString(srjson_doc_t *doc, const char *string) {
	srjson_t *item = srjson_New_Item(doc);
	if (item) {
		item->type = srjson_String;
		item->valuestring = srjson_strdup(doc, string);
	} return item;
}
示例#4
0
srjson_t *srjson_CreateNumber(srjson_doc_t *doc, double num) {
	srjson_t *item = srjson_New_Item(doc);
	if (item) {
		item->type = srjson_Number;
		item->valuedouble = num;
	} return item;
}
示例#5
0
/* Utility for handling references. */
static srjson_t *create_reference(srjson_doc_t *doc, srjson_t *item) {
    srjson_t *ref = srjson_New_Item(doc);
    if (!ref)
        return 0;
    memcpy(ref, item, sizeof(srjson_t));
    ref->string = 0;
    ref->type |= srjson_IsReference;
    ref->next = ref->prev = 0;
    return ref;
}
示例#6
0
/* Parse an object - create a new root, and populate. */
srjson_t *srjson_Parse(srjson_doc_t *doc, const char *value)
{
    srjson_t *c = srjson_New_Item(doc);
    ep = 0;
    if (!c)
        return 0;	/* memory fail */

    if (!parse_value(doc, c, skip(value))) {
        srjson_Delete(doc, c);
        return 0;
    }
    return c;
}
示例#7
0
/* Build an array from input text. */
static const char *parse_array(srjson_doc_t *doc, srjson_t *item, const char *value)
{
    srjson_t *child;
    if (*value != '[') {
        ep = value;
        return 0;
    }			/* not an array! */
    item->type = srjson_Array;
    value = skip(value + 1);
    if (*value == ']')
        return value + 1;	/* empty array. */

    item->child = child = srjson_New_Item(doc);
    if (!item->child)
        return 0;	/* memory fail */
    value = skip(parse_value(doc, child, skip(value)));	/* skip any spacing, get
							 * the value. */
    if (!value)
        return 0;

    while (*value == ',') {
        srjson_t *new_item;
        if (!(new_item = srjson_New_Item(doc)))
            return 0;	/* memory fail */
        child->next = new_item;
        new_item->prev = child;
        child = new_item;
        value = skip(parse_value(doc, child, skip(value + 1)));
        if (!value)
            return 0;	/* memory fail */
    }

    if (*value == ']')
        return value + 1;	/* end of array */
    ep = value;
    return 0;		/* malformed. */
}
示例#8
0
srjson_t *srjson_CreateObject(srjson_doc_t *doc) {
    srjson_t *item = srjson_New_Item(doc);
    if (item)
        item->type = srjson_Object;
    return item;
}
示例#9
0
srjson_t *srjson_CreateArray(srjson_doc_t *doc) {
    srjson_t *item = srjson_New_Item(doc);
    if (item)
        item->type = srjson_Array;
    return item;
}
示例#10
0
srjson_t *srjson_CreateBool(srjson_doc_t *doc, int b) {
    srjson_t *item = srjson_New_Item(doc);
    if (item)
        item->type = b ? srjson_True : srjson_False;
    return item;
}
示例#11
0
srjson_t *srjson_CreateFalse(srjson_doc_t *doc) {
    srjson_t *item = srjson_New_Item(doc);
    if (item)
        item->type = srjson_False;
    return item;
}
示例#12
0
/* Create basic types: */
srjson_t *srjson_CreateNull(srjson_doc_t *doc) {
    srjson_t *item = srjson_New_Item(doc);
    if (item)
        item->type = srjson_NULL;
    return item;
}