dyntype.c
1.81 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <string.h>
#include <json/json.h>
#include "token/cclass.h"
#include "token/dyntype.h"
#define TEST_STR "this is a foo string"
#define TEST_KEY1 "key1"
#define TEST_KEY2 "key2"
#define TEST_KEY3 "key3"
int
main(int argc, char * argv[])
{
struct json_object * json = json_object_new_int(123);
struct DYNTYPE * dyn = newFromJson(DYNTYPE, json);
printf("%d\n", (dyn->data)._int);
delete(dyn);
json_object_put(json);
dyn = new(DYNTYPE, DYNTYPE_TYPE_INT, sizeof(int), 321);
json = toJson(dyn);
printf("%s\n", json_object_to_json_string(json));
delete(dyn);
json_object_put(json);
json = json_object_new_string(TEST_STR);
dyn = newFromJson(DYNTYPE, json);
printf("%s\n", (dyn->data)._string);
delete(dyn);
json_object_put(json);
dyn = new(DYNTYPE, DYNTYPE_TYPE_STRING, strlen(TEST_STR), TEST_STR);
json = toJson(dyn);
printf("%s\n", json_object_to_json_string(json));
delete(dyn);
json_object_put(json);
json = json_tokener_parse("{\"key1\":123,\"key2\":321,\"key3\":\"" TEST_STR "\"}");
dyn = newFromJson(DYNTYPE, json);
json_object_put(json);
json = toJson(dyn);
printf("%s\n", json_object_to_json_string(json));
json_object_put(json);
if (DYNTYPE_TYPE_HASH == dyn->type) {
struct DYNTYPE_HASH * hash = (dyn->data)._hash;
struct DYNTYPE * value;
value = dyntype_hash_get(hash, TEST_KEY1);
printf("%d\n", (value->data)._int);
delete(value);
value = dyntype_hash_get(hash, TEST_KEY2);
printf("%d\n", (value->data)._int);
delete(value);
value = dyntype_hash_get(hash, TEST_KEY3);
printf("%s\n", (value->data)._string);
delete(value);
}
delete(dyn);
return 0;
}
// vim: set et ts=4 sw=4: