/* 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. */ }
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; }
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; }
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; }
/* 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; }
/* 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; }
/* 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. */ }
srjson_t *srjson_CreateObject(srjson_doc_t *doc) { srjson_t *item = srjson_New_Item(doc); if (item) item->type = srjson_Object; return item; }
srjson_t *srjson_CreateArray(srjson_doc_t *doc) { srjson_t *item = srjson_New_Item(doc); if (item) item->type = srjson_Array; return item; }
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; }
srjson_t *srjson_CreateFalse(srjson_doc_t *doc) { srjson_t *item = srjson_New_Item(doc); if (item) item->type = srjson_False; return item; }
/* 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; }