taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

json.rs (1541B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero 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 Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 #[macro_export]
     18 macro_rules! json {
     19     ($object:ident + $($json:tt)+) => {
     20         {
     21             pub fn merge(a: &mut ::serde_json::Value, b: ::serde_json::Value) {
     22                 match (a, b) {
     23                     (a @ &mut ::serde_json::Value::Object(_), ::serde_json::Value::Object(b)) => {
     24                         let a = a.as_object_mut().unwrap();
     25                         for (k, v) in b {
     26                             merge(a.entry(k).or_insert(::serde_json::Value::Null), v);
     27                         }
     28                     }
     29                     (a, b) => *a = b,
     30                 }
     31             }
     32             let change = json!($($json)+);
     33             let mut original = $object.clone();
     34             merge(&mut original, change);
     35             original
     36         }
     37     };
     38     ($($json:tt)+) => { serde_json::json!($($json)+) };
     39 }