merchant_parse.c (4658B)
1 /* 2 This file is part of TALER 3 (C) 2024, 2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Lesser General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER 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 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file src/util/merchant_parse.c 18 * @brief shared logic for merchant metadata parsing 19 * @author Iván Ávalos 20 * @author Christian Grothoff 21 */ 22 #include "platform.h" 23 #include <gnunet/gnunet_common.h> 24 #include <gnunet/gnunet_json_lib.h> 25 #include <jansson.h> 26 #include <stdbool.h> 27 #include <stdint.h> 28 #include <taler/taler_json_lib.h> 29 #include <taler/taler_util.h> 30 #include "taler/taler_merchant_util.h" 31 32 33 /** 34 * Parse merchant details of given JSON order terms. 35 * 36 * @param cls closure, unused parameter 37 * @param root the JSON object representing data 38 * @param[out] ospec where to write the data 39 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 40 */ 41 static enum GNUNET_GenericReturnValue 42 parse_merchant_details (void *cls, 43 json_t *root, 44 struct GNUNET_JSON_Specification *ospec) 45 { 46 struct TALER_MERCHANT_MetaData *merchant = ospec->ptr; 47 struct GNUNET_JSON_Specification spec[] = { 48 GNUNET_JSON_spec_string_copy ("name", 49 &merchant->name), 50 GNUNET_JSON_spec_mark_optional ( 51 GNUNET_JSON_spec_string_copy ("email", 52 &merchant->email), 53 NULL), 54 GNUNET_JSON_spec_mark_optional ( 55 GNUNET_JSON_spec_string_copy ("website", 56 &merchant->website), 57 NULL), 58 GNUNET_JSON_spec_mark_optional ( 59 GNUNET_JSON_spec_string_copy ("logo", 60 &merchant->logo), 61 NULL), 62 GNUNET_JSON_spec_mark_optional ( 63 GNUNET_JSON_spec_object_copy ("address", 64 &merchant->address), 65 NULL), 66 GNUNET_JSON_spec_mark_optional ( 67 GNUNET_JSON_spec_object_copy ("jurisdiction", 68 &merchant->jurisdiction), 69 NULL), 70 GNUNET_JSON_spec_end () 71 }; 72 const char *error_name; 73 unsigned int error_line; 74 75 (void) cls; 76 if (GNUNET_OK != 77 GNUNET_JSON_parse (root, 78 spec, 79 &error_name, 80 &error_line)) 81 { 82 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 83 "Failed to parse %s at %u: %s\n", 84 spec[error_line].field, 85 error_line, 86 error_name); 87 GNUNET_break_op (0); 88 return GNUNET_SYSERR; 89 } 90 return GNUNET_OK; 91 } 92 93 94 json_t * 95 TALER_MERCHANT_metadata_to_json ( 96 const struct TALER_MERCHANT_MetaData *merchant) 97 { 98 return GNUNET_JSON_PACK ( 99 GNUNET_JSON_pack_string ("name", 100 merchant->name), 101 GNUNET_JSON_pack_allow_null ( 102 GNUNET_JSON_pack_string ("email", 103 merchant->email)), 104 GNUNET_JSON_pack_allow_null ( 105 GNUNET_JSON_pack_string ("website", 106 merchant->website)), 107 GNUNET_JSON_pack_allow_null ( 108 GNUNET_JSON_pack_string ("logo", 109 merchant->logo)), 110 GNUNET_JSON_pack_allow_null ( 111 GNUNET_JSON_pack_object_incref ("address", 112 merchant->address)), 113 GNUNET_JSON_pack_allow_null ( 114 GNUNET_JSON_pack_object_incref ("jurisdiction", 115 merchant->jurisdiction))); 116 } 117 118 119 struct GNUNET_JSON_Specification 120 TALER_MERCHANT_spec_merchant_details ( 121 const char *name, 122 struct TALER_MERCHANT_MetaData *merchant) 123 { 124 struct GNUNET_JSON_Specification ret = { 125 .parser = &parse_merchant_details, 126 .field = name, 127 .ptr = merchant, 128 }; 129 130 return ret; 131 } 132 133 134 void 135 TALER_MERCHANT_metadata_free ( 136 struct TALER_MERCHANT_MetaData *merchant) 137 { 138 GNUNET_free (merchant->name); 139 GNUNET_free (merchant->website); 140 GNUNET_free (merchant->email); 141 GNUNET_free (merchant->logo); 142 if (NULL != merchant->address) 143 { 144 json_decref (merchant->address); 145 merchant->address = NULL; 146 } 147 if (NULL != merchant->jurisdiction) 148 { 149 json_decref (merchant->jurisdiction); 150 merchant->jurisdiction = NULL; 151 } 152 }