Encodable+toJSON.swift (490B)
1 /* 2 * This file is part of GNU Taler, ©2022-25 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Iván Ávalos 7 * @author Marc Stibane 8 */ 9 import Foundation 10 11 extension Encodable { 12 func toJSON(_ encoder: JSONEncoder = JSONEncoder()) -> String? { 13 encoder.outputFormatting = .prettyPrinted 14 if let data = try? encoder.encode(self) { 15 let result = String(decoding: data, as: UTF8.self) 16 return String(result) 17 } 18 return nil 19 } 20 }