diff --git a/bigpoint_dyntype.c b/bigpoint_dyntype.c index 9fa2a73..5fff3c0 100644 --- a/bigpoint_dyntype.c +++ b/bigpoint_dyntype.c @@ -23,6 +23,9 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params) memcpy((_this->data)._string, va_arg(* params, const char *), _this->size); break; + case BIGPOINT_DYNTYPE_HASH: + break; + default: (_this->data)._object = NULL; } diff --git a/bigpoint_dyntype.h b/bigpoint_dyntype.h index 86fe3a4..c91ea5f 100644 --- a/bigpoint_dyntype.h +++ b/bigpoint_dyntype.h @@ -13,7 +13,7 @@ enum BIGPOINT_DYNTYPE_TYPES { BIGPOINT_DYNTYPE_FLOAT, BIGPOINT_DYNTYPE_STRING, BIGPOINT_DYNTYPE_ARRAY, - BIGPOINT_DYNTYPE_OBJECT + BIGPOINT_DYNTYPE_HASH }; diff --git a/bigpoint_hash.c b/bigpoint_hash.c index 3be5609..49ddaff 100644 --- a/bigpoint_hash.c +++ b/bigpoint_hash.c @@ -8,39 +8,7 @@ #define HASH_ENTRY_CHUNK_SIZE 128 -static -void -_updateHashSize(struct BIGPOINT_HASH * _this) -{ - if (_this->used == _this->size) { - _this->size += HASH_ENTRY_CHUNK_SIZE; - - _this->keys = realloc(_this->keys, sizeof(char*) * _this->size); - memset(_this->keys + (_this->used * sizeof(char*)), - HASH_ENTRY_CHUNK_SIZE * sizeof(char*), - 0); - - _this->values = realloc( - _this->values, - sizeof(struct BIGPOINT_DYNTYPE *) * _this->size); - memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)), - HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *), - 0); - } -} - -static -size_t -_getHashIdx(struct BIGPOINT_HASH * _this, const char * key) -{ - size_t index; - - for (index = 0; - index < _this->used && strcmp(_this->keys[index], key); - index++); - - return index; -} +static void _updateHashSize(struct BIGPOINT_HASH * _this); static void @@ -55,6 +23,15 @@ static void __jsonConst(struct BIGPOINT_HASH * _this, struct json_object * json) { + __construct(_this, NULL); + + if (json_type_object != json_object_get_type(json)) { + return; + } + + json_object_object_foreach(json, key, value) { + bigpoint_hash_set(_this, key, newFromJson(BIGPOINT_DYNTYPE, value)); + } } static @@ -88,6 +65,40 @@ struct BIGPOINT_CCLASS _bigpoint_hash = { const struct BIGPOINT_CCLASS * const BIGPOINT_HASH = &_bigpoint_hash; +static +void +_updateHashSize(struct BIGPOINT_HASH * _this) +{ + if (_this->used == _this->size) { + _this->size += HASH_ENTRY_CHUNK_SIZE; + + _this->keys = realloc(_this->keys, sizeof(char*) * _this->size); + memset(_this->keys + (_this->used * sizeof(char*)), + HASH_ENTRY_CHUNK_SIZE * sizeof(char*), + 0); + + _this->values = realloc( + _this->values, + sizeof(struct BIGPOINT_DYNTYPE *) * _this->size); + memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)), + HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *), + 0); + } +} + +static +size_t +_getHashIdx(struct BIGPOINT_HASH * _this, const char * key) +{ + size_t index; + + for (index = 0; + index < _this->used && strcmp(_this->keys[index], key); + index++); + + return index; +} + void bigpoint_hash_set(struct BIGPOINT_HASH * _this, const char * key, diff --git a/tests/hash_test.c b/tests/hash_test.c index 1f54613..644ebb8 100644 --- a/tests/hash_test.c +++ b/tests/hash_test.c @@ -33,6 +33,20 @@ main(int argc, char * argv[]) delete(hash); + hash = newFromJson( + BIGPOINT_HASH, + json_tokener_parse("{\"key1\":123,\"key2\":321}")); + + dyn = bigpoint_hash_get(hash, TEST_KEY1); + printf("%d\n", (dyn->data)._int); + delete(dyn); + + dyn = bigpoint_hash_get(hash, TEST_KEY2); + printf("%d\n", (dyn->data)._int); + delete(dyn); + + delete(hash); + return 0; }