Commit 262f8f651b361c71cc5eaf4becff1f7adbe4be6f
1 parent
ac539ffa
hash integrated in dyntype...array still missing, but for simple sas tokens this should be suitable.
Showing
4 changed files
with
59 additions
and
1 deletions
... | ... | @@ -56,6 +56,12 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json) |
56 | 56 | } |
57 | 57 | break; |
58 | 58 | |
59 | + case json_type_object: | |
60 | + _this->type = BIGPOINT_DYNTYPE_HASH; | |
61 | + _this->size = sizeof(struct BIGPOINT_HASH *); | |
62 | + (_this->data)._hash = newFromJson(BIGPOINT_HASH, json); | |
63 | + break; | |
64 | + | |
59 | 65 | default: |
60 | 66 | _this->type = BIGPOINT_DYNTYPE_NULL; |
61 | 67 | _this->size = 0; |
... | ... | @@ -73,6 +79,10 @@ __destruct(struct BIGPOINT_DYNTYPE * _this) |
73 | 79 | free((_this->data)._string); |
74 | 80 | break; |
75 | 81 | |
82 | + case BIGPOINT_DYNTYPE_HASH: | |
83 | + delete((_this->data)._hash); | |
84 | + break; | |
85 | + | |
76 | 86 | default: |
77 | 87 | break; |
78 | 88 | } |
... | ... | @@ -94,6 +104,10 @@ __toJson(struct BIGPOINT_DYNTYPE * _this) |
94 | 104 | json = json_object_new_string((_this->data)._string); |
95 | 105 | break; |
96 | 106 | |
107 | + case BIGPOINT_DYNTYPE_HASH: | |
108 | + json = toJson((_this->data)._hash); | |
109 | + break; | |
110 | + | |
97 | 111 | default: |
98 | 112 | json = NULL; |
99 | 113 | } | ... | ... |
... | ... | @@ -52,6 +52,17 @@ static |
52 | 52 | struct json_object * |
53 | 53 | __toJson(struct BIGPOINT_HASH * _this) |
54 | 54 | { |
55 | + size_t index; | |
56 | + struct json_object * json = json_object_new_object(); | |
57 | + | |
58 | + for (index = 0; index < _this->used; index ++) { | |
59 | + json_object_object_add( | |
60 | + json, | |
61 | + _this->keys[index], | |
62 | + toJson(_this->values[index])); | |
63 | + } | |
64 | + | |
65 | + return json; | |
55 | 66 | } |
56 | 67 | |
57 | 68 | static const | ... | ... |
... | ... | @@ -6,6 +6,9 @@ |
6 | 6 | #include "../bigpoint_dyntype.h" |
7 | 7 | |
8 | 8 | #define TEST_STR "this is a foo string" |
9 | +#define TEST_KEY1 "key1" | |
10 | +#define TEST_KEY2 "key2" | |
11 | +#define TEST_KEY3 "key3" | |
9 | 12 | |
10 | 13 | |
11 | 14 | int |
... | ... | @@ -43,6 +46,33 @@ main(int argc, char * argv[]) |
43 | 46 | delete(dyn); |
44 | 47 | json_object_put(json); |
45 | 48 | |
49 | + json = json_tokener_parse("{\"key1\":123,\"key2\":321,\"key3\":\"" TEST_STR "\"}"); | |
50 | + dyn = newFromJson(BIGPOINT_DYNTYPE, json); | |
51 | + json_object_put(json); | |
52 | + | |
53 | + json = toJson(dyn); | |
54 | + printf("%s\n", json_object_to_json_string(json)); | |
55 | + json_object_put(json); | |
56 | + | |
57 | + if (BIGPOINT_DYNTYPE_HASH == dyn->type) { | |
58 | + struct BIGPOINT_HASH * hash = (dyn->data)._hash; | |
59 | + struct BIGPOINT_DYNTYPE * value; | |
60 | + | |
61 | + value = bigpoint_hash_get(hash, TEST_KEY1); | |
62 | + printf("%d\n", (value->data)._int); | |
63 | + delete(value); | |
64 | + | |
65 | + value = bigpoint_hash_get(hash, TEST_KEY2); | |
66 | + printf("%d\n", (value->data)._int); | |
67 | + delete(value); | |
68 | + | |
69 | + value = bigpoint_hash_get(hash, TEST_KEY3); | |
70 | + printf("%s\n", (value->data)._string); | |
71 | + delete(value); | |
72 | + } | |
73 | + | |
74 | + delete(dyn); | |
75 | + | |
46 | 76 | return 0; |
47 | 77 | } |
48 | 78 | ... | ... |
... | ... | @@ -3,7 +3,6 @@ |
3 | 3 | #include <json/json.h> |
4 | 4 | |
5 | 5 | #include "../bigpoint_cclass.h" |
6 | -#include "../bigpoint_dyntype.h" | |
7 | 6 | #include "../bigpoint_hash.h" |
8 | 7 | |
9 | 8 | |
... | ... | @@ -24,6 +23,10 @@ main(int argc, char * argv[]) |
24 | 23 | dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_INT, sizeof(int), 321); |
25 | 24 | bigpoint_hash_set(hash, TEST_KEY2, dyn); |
26 | 25 | |
26 | + json = toJson(hash); | |
27 | + printf("%s\n", json_object_to_json_string(json)); | |
28 | + json_object_put(json); | |
29 | + | |
27 | 30 | dyn = bigpoint_hash_get(hash, TEST_KEY1); |
28 | 31 | printf("%d\n", (dyn->data)._int); |
29 | 32 | delete(dyn); | ... | ... |
Please
register
or
login
to post a comment