aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_generator.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-03-17 19:24:35 +0000
committerChristian Grothoff <christian@grothoff.org>2016-03-17 19:24:35 +0000
commita5361231e36224607d8c2c1376757c0b99f34f59 (patch)
treeee2ee9fa2c114c6b8e5850f9b31d2bbb54b8c5d2 /src/json/json_generator.c
parent23b982b51e8f1c152053154c0fce4cfc4cfcbf67 (diff)
downloadgnunet-a5361231e36224607d8c2c1376757c0b99f34f59.tar.gz
gnunet-a5361231e36224607d8c2c1376757c0b99f34f59.zip
adding library for basic JSON conversions
Diffstat (limited to 'src/json/json_generator.c')
-rw-r--r--src/json/json_generator.c146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
new file mode 100644
index 000000000..4b1ac31b1
--- /dev/null
+++ b/src/json/json_generator.c
@@ -0,0 +1,146 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
15*/
16/**
17 * @file json/json_generator.c
18 * @brief helper functions for generating JSON from GNUnet data structures
19 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20 */
21#include "platform.h"
22#include "gnunet_util_lib.h"
23#include "gnunet_json_lib.h"
24
25
26/**
27 * Convert binary data to a JSON string
28 * with the base32crockford encoding.
29 *
30 * @param data binary data
31 * @param size size of @a data in bytes
32 * @return json string that encodes @a data
33 */
34json_t *
35GNUNET_JSON_from_data (const void *data,
36 size_t size)
37{
38 char *buf;
39 json_t *json;
40
41 buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
42 json = json_string (buf);
43 GNUNET_free (buf);
44 return json;
45}
46
47
48/**
49 * Convert absolute timestamp to a json string.
50 *
51 * @param stamp the time stamp
52 * @return a json string with the timestamp in @a stamp
53 */
54json_t *
55GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
56{
57 json_t *j;
58 char *mystr;
59 int ret;
60
61 GNUNET_assert (GNUNET_OK ==
62 GNUNET_TIME_round_abs (&stamp));
63 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
64 return json_string ("/never/");
65 ret = GNUNET_asprintf (&mystr,
66 "/Date(%llu)/",
67 (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL)));
68 GNUNET_assert (ret > 0);
69 j = json_string (mystr);
70 GNUNET_free (mystr);
71 return j;
72}
73
74
75/**
76 * Convert relative timestamp to a json string.
77 *
78 * @param stamp the time stamp
79 * @return a json string with the timestamp in @a stamp
80 */
81json_t *
82GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
83{
84 json_t *j;
85 char *mystr;
86 int ret;
87
88 GNUNET_assert (GNUNET_OK ==
89 GNUNET_TIME_round_rel (&stamp));
90 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
91 return json_string ("/forever/");
92 ret = GNUNET_asprintf (&mystr,
93 "/Delay(%llu)/",
94 (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL)));
95 GNUNET_assert (ret > 0);
96 j = json_string (mystr);
97 GNUNET_free (mystr);
98 return j;
99}
100
101
102/**
103 * Convert RSA public key to JSON.
104 *
105 * @param pk public key to convert
106 * @return corresponding JSON encoding
107 */
108json_t *
109GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_rsa_PublicKey *pk)
110{
111 char *buf;
112 size_t buf_len;
113 json_t *ret;
114
115 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
116 &buf);
117 ret = GNUNET_JSON_from_data (buf,
118 buf_len);
119 GNUNET_free (buf);
120 return ret;
121}
122
123
124/**
125 * Convert RSA signature to JSON.
126 *
127 * @param sig signature to convert
128 * @return corresponding JSON encoding
129 */
130json_t *
131GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_rsa_Signature *sig)
132{
133 char *buf;
134 size_t buf_len;
135 json_t *ret;
136
137 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
138 &buf);
139 ret = GNUNET_JSON_from_data (buf,
140 buf_len);
141 GNUNET_free (buf);
142 return ret;
143}
144
145
146/* End of json/json_generator.c */