adding library for basic JSON conversions
[oweals/gnunet.git] / src / json / json_generator.c
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  */
34 json_t *
35 GNUNET_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  */
54 json_t *
55 GNUNET_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  */
81 json_t *
82 GNUNET_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  */
108 json_t *
109 GNUNET_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  */
130 json_t *
131 GNUNET_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 */