From 1c5d451619710cba3edec4767e47689aa6291729 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 20 Jul 2021 18:15:23 +0200 Subject: [PATCH] Add json_encode_object for encoding objects (mainly SimpleXML obj.) --- src/MD_STD.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/MD_STD.php b/src/MD_STD.php index 1f514f2..0e78530 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -215,6 +215,27 @@ final class MD_STD { } + /** + * Type-safe wrapper around json_encode for objects. + * + * @see https://www.php.net/manual/en/function.json-encode.php + * + * @param object $value The value being encoded. Can be any type except a resource. + * @param integer $options Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT ... + * @param integer $depth Depth of coding. + * + * @return string + */ + public static function json_encode_object(object $value, int $options = 0, int $depth = 512):string { + + $output = \json_encode($value, $options, $depth); + if ($output === false) { + throw new Exception("JSON output could not be generated"); + } + return $output; + + } + /** * Type-safe wrapper around strtotime(). *