hash.c
1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <string.h>
#include <json/json.h>
#include "token/cclass.h"
#include "token/dyntype/hash.h"
#define TEST_KEY1 "key1"
#define TEST_KEY2 "key2"
int
main(int argc, char * argv[])
{
struct DYNTYPE * dyn;
struct DYNTYPE_HASH * hash = new(DYNTYPE_HASH);
struct json_object * json;
dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), 123);
dyntype_hash_set(hash, TEST_KEY1, dyn);
dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), 321);
dyntype_hash_set(hash, TEST_KEY2, dyn);
json = toJson(hash);
printf("%s\n", json_object_to_json_string(json));
json_object_put(json);
dyn = dyntype_hash_get(hash, TEST_KEY1);
printf("%d\n", (dyn->data)._int);
delete(&dyn);
dyn = dyntype_hash_get(hash, TEST_KEY2);
printf("%d\n", (dyn->data)._int);
delete(&dyn);
delete(&hash);
json = json_tokener_parse("{\"key1\":123,\"key2\":321}");
hash = newFromJson(DYNTYPE_HASH, json);
json_object_put(json);
dyn = dyntype_hash_get(hash, TEST_KEY1);
printf("%d\n", (dyn->data)._int);
delete(&dyn);
dyn = dyntype_hash_get(hash, TEST_KEY2);
printf("%d\n", (dyn->data)._int);
delete(&dyn);
delete(&hash);
return 0;
}
// vim: set et ts=4 sw=4: