2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
16 * @file json/json_generator.c
17 * @brief helper functions for generating JSON from GNUnet data structures
18 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
21 #include "gnunet_util_lib.h"
22 #include "gnunet_json_lib.h"
26 * Convert binary data to a JSON string
27 * with the base32crockford encoding.
29 * @param data binary data
30 * @param size size of @a data in bytes
31 * @return json string that encodes @a data
34 GNUNET_JSON_from_data (const void *data,
40 buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
41 json = json_string (buf);
48 * Convert absolute timestamp to a json string.
50 * @param stamp the time stamp
51 * @return a json string with the timestamp in @a stamp
54 GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
60 GNUNET_assert (GNUNET_OK ==
61 GNUNET_TIME_round_abs (&stamp));
62 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
63 return json_string ("/never/");
64 ret = GNUNET_asprintf (&mystr,
66 (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL)));
67 GNUNET_assert (ret > 0);
68 j = json_string (mystr);
75 * Convert absolute timestamp to a json string.
77 * @param stamp the time stamp
78 * @return a json string with the timestamp in @a stamp
81 GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
83 return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
88 * Convert relative timestamp to a json string.
90 * @param stamp the time stamp
91 * @return a json string with the timestamp in @a stamp
94 GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
100 GNUNET_assert (GNUNET_OK ==
101 GNUNET_TIME_round_rel (&stamp));
102 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
103 return json_string ("/forever/");
104 ret = GNUNET_asprintf (&mystr,
106 (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL)));
107 GNUNET_assert (ret > 0);
108 j = json_string (mystr);
115 * Convert RSA public key to JSON.
117 * @param pk public key to convert
118 * @return corresponding JSON encoding
121 GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
127 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
129 ret = GNUNET_JSON_from_data (buf,
137 * Convert RSA signature to JSON.
139 * @param sig signature to convert
140 * @return corresponding JSON encoding
143 GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
149 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
151 ret = GNUNET_JSON_from_data (buf,
158 /* End of json/json_generator.c */