product_sold_serialize.c (4452B)
1 /* 2 This file is part of GNU Taler 3 Copyright (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 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/product_sold_serialize.c 18 * @brief shared logic for product sold serialization 19 * @author Iván Ávalos 20 * @author Christian Grothoff 21 */ 22 #include "platform.h" 23 #include <gnunet/gnunet_json_lib.h> 24 #include <gnunet/gnunet_common.h> 25 #include <taler/taler_json_lib.h> 26 #include <jansson.h> 27 #include "taler/taler_util.h" 28 #include "taler/taler_merchant_util.h" 29 30 31 /** 32 * Convert quantity @a q into a string for JSON serialization 33 * 34 * @param q quantity to convert 35 * @return formatted string 36 */ 37 static const char * 38 quantity_to_string (const struct TALER_MERCHANT_ProductQuantity *q) 39 { 40 static char res[64]; 41 42 TALER_MERCHANT_vk_format_fractional_string (TALER_MERCHANT_VK_QUANTITY, 43 q->integer, 44 q->fractional, 45 sizeof (res), 46 res); 47 return res; 48 } 49 50 51 json_t * 52 TALER_MERCHANT_product_sold_serialize ( 53 const struct TALER_MERCHANT_ProductSold *p) 54 { 55 json_t *prices; 56 57 prices = json_array (); 58 GNUNET_assert (NULL != prices); 59 for (unsigned int i = 0; i<p->prices_length; i++) 60 GNUNET_assert (0 == 61 json_array_append_new (prices, 62 TALER_JSON_from_amount ( 63 &p->prices[i]))); 64 return GNUNET_JSON_PACK ( 65 GNUNET_JSON_pack_allow_null ( 66 GNUNET_JSON_pack_string ("product_id", 67 p->product_id)), 68 GNUNET_JSON_pack_allow_null ( 69 GNUNET_JSON_pack_string ("product_name", 70 p->product_name)), 71 GNUNET_JSON_pack_allow_null ( 72 GNUNET_JSON_pack_string ("description", 73 p->description)), 74 GNUNET_JSON_pack_allow_null ( 75 GNUNET_JSON_pack_object_incref ("description_i18n", 76 (json_t *) p->description_i18n)), 77 GNUNET_JSON_pack_allow_null ( 78 ( (0 != p->unit_quantity.integer) || 79 (0 != p->unit_quantity.fractional) ) 80 ? GNUNET_JSON_pack_string ("unit_quantity", 81 quantity_to_string (&p->unit_quantity)) 82 : GNUNET_JSON_pack_string ("dummy", 83 NULL) ), 84 /* Legacy */ 85 GNUNET_JSON_pack_allow_null ( 86 (0 == p->unit_quantity.fractional) 87 ? GNUNET_JSON_pack_uint64 ("quantity", 88 p->unit_quantity.integer) 89 : GNUNET_JSON_pack_string ("dummy", 90 NULL) ), 91 GNUNET_JSON_pack_bool ("prices_are_net", 92 p->prices_are_net), 93 GNUNET_JSON_pack_allow_null ( 94 GNUNET_JSON_pack_string ("unit", 95 p->unit)), 96 /* Deprecated, use prices! */ 97 GNUNET_JSON_pack_allow_null ( 98 TALER_JSON_pack_amount ("price", 99 0 < p->prices_length 100 ? &p->prices[0] 101 : NULL)), 102 GNUNET_JSON_pack_array_steal ("prices", 103 prices), 104 GNUNET_JSON_pack_allow_null ( 105 GNUNET_JSON_pack_string ("image", 106 p->image)), 107 GNUNET_JSON_pack_allow_null ( 108 GNUNET_JSON_pack_array_incref ("taxes", 109 (json_t *) p->taxes)), 110 GNUNET_JSON_pack_allow_null ( 111 GNUNET_TIME_absolute_is_never (p->delivery_date.abs_time) 112 ? GNUNET_JSON_pack_string ("dummy", 113 NULL) 114 : GNUNET_JSON_pack_timestamp ("delivery_date", 115 p->delivery_date)), 116 GNUNET_JSON_pack_uint64 ("product_money_pot", 117 p->product_money_pot)); 118 }