json_value *json_object_push_nocopy(json_value *object, unsigned int name_length, json_char *name, json_value *value) { json_object_entry *entry; assert(object->type == json_object); if (!builderize(object) || !builderize(value)) return NULL; if (((json_builder_value *)object)->additional_length_allocated > 0) { --((json_builder_value *)object)->additional_length_allocated; } else { json_object_entry *values_new = (json_object_entry *)realloc( object->u.object.values, sizeof(*object->u.object.values) * (object->u.object.length + 1)); if (!values_new) return NULL; object->u.object.values = values_new; } entry = object->u.object.values + object->u.object.length; entry->name_length = name_length; entry->name = name; entry->value = value; ++object->u.object.length; value->parent = object; return value; }
json_value * json_array_push (json_value * array, json_value * value) { assert (array->type == json_array); if (!builderize (array) || !builderize (value)) return NULL; if (((json_builder_value *) array)->additional_length_allocated > 0) { -- ((json_builder_value *) array)->additional_length_allocated; } else { json_value ** values_new = (json_value **) realloc (array->u.array.values, sizeof (json_value *) * (array->u.array.length + 1)); if (!values_new) return NULL; array->u.array.values = values_new; } array->u.array.values [array->u.array.length] = value; ++ array->u.array.length; value->parent = array; return value; }
json_value * json_object_merge (json_value * objectA, json_value * objectB) { unsigned int i; assert (objectA->type == json_object); assert (objectB->type == json_object); assert (objectA != objectB); if (!builderize (objectA) || !builderize (objectB)) return NULL; if (objectB->u.object.length <= ((json_builder_value *) objectA)->additional_length_allocated) { ((json_builder_value *) objectA)->additional_length_allocated -= objectB->u.object.length; } else { json_object_entry * values_new; unsigned int alloc = objectA->u.object.length + ((json_builder_value *) objectA)->additional_length_allocated + objectB->u.object.length; if (! (values_new = (json_object_entry *) realloc (objectA->u.object.values, sizeof (json_object_entry) * alloc))) { return NULL; } objectA->u.object.values = values_new; } for (i = 0; i < objectB->u.object.length; ++ i) { json_object_entry * entry = &objectA->u.object.values[objectA->u.object.length + i]; *entry = objectB->u.object.values[i]; entry->value->parent = objectA; } objectA->u.object.length += objectB->u.object.length; free (objectB->u.object.values); free (objectB); return objectA; }
void json_object_sort(json_value *object, json_value *proto) { unsigned int i, out_index = 0; if (!builderize(object)) return; /* TODO error */ assert(object->type == json_object); assert(proto->type == json_object); for (i = 0; i < proto->u.object.length; ++i) { unsigned int j; json_object_entry proto_entry = proto->u.object.values[i]; for (j = 0; j < object->u.object.length; ++j) { json_object_entry entry = object->u.object.values[j]; if (entry.name_length != proto_entry.name_length) continue; if (memcmp(entry.name, proto_entry.name, entry.name_length) != 0) continue; object->u.object.values[j] = object->u.object.values[out_index]; object->u.object.values[out_index] = entry; ++out_index; } } }