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 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.
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.
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/>
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>
22 #include "gnunet_util_lib.h"
23 #include "gnunet_json_lib.h"
27 * Convert binary data to a JSON string
28 * with the base32crockford encoding.
30 * @param data binary data
31 * @param size size of @a data in bytes
32 * @return json string that encodes @a data
35 GNUNET_JSON_from_data (const void *data,
41 buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
42 json = json_string (buf);
49 * Convert absolute timestamp to a json string.
51 * @param stamp the time stamp
52 * @return a json string with the timestamp in @a stamp
55 GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
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,
67 (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL)));
68 GNUNET_assert (ret > 0);
69 j = json_string (mystr);
76 * Convert relative timestamp to a json string.
78 * @param stamp the time stamp
79 * @return a json string with the timestamp in @a stamp
82 GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
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,
94 (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL)));
95 GNUNET_assert (ret > 0);
96 j = json_string (mystr);
103 * Convert RSA public key to JSON.
105 * @param pk public key to convert
106 * @return corresponding JSON encoding
109 GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
115 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
117 ret = GNUNET_JSON_from_data (buf,
125 * Convert RSA signature to JSON.
127 * @param sig signature to convert
128 * @return corresponding JSON encoding
131 GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
137 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
139 ret = GNUNET_JSON_from_data (buf,
146 /* End of json/json_generator.c */