Adds json_object_get_value_at function to access values in objects in O(1) time.

Also adds some missing null checking.
pull/41/head
Krzysztof Gabis 8 years ago
parent 77ef99571e
commit 473c7f3d8d
  1. 10
      parson.c
  2. 1
      parson.h

@ -983,14 +983,20 @@ size_t json_object_get_count(const JSON_Object *object) {
}
const char * json_object_get_name(const JSON_Object *object, size_t index) {
if (index >= json_object_get_count(object))
if (object == NULL || index >= json_object_get_count(object))
return NULL;
return object->names[index];
}
JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) {
if (object == NULL || index >= json_object_get_count(object))
return NULL;
return object->values[index];
}
/* JSON Array API */
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) {
if (index >= json_array_get_count(array))
if (array == NULL || index >= json_array_get_count(array))
return NULL;
return array->items[index];
}

@ -127,6 +127,7 @@ int json_object_dotget_boolean(const JSON_Object *object, const char *
/* Functions to get available names */
size_t json_object_get_count(const JSON_Object *object);
const char * json_object_get_name (const JSON_Object *object, size_t index);
JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index);
/* Creates new name-value pair or frees and replaces old value with a new one.
* json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */

Loading…
Cancel
Save