*/ function encode( $value, bool $formatted = false ) : string { $string = json_encode($value, $formatted ? JSON_PRETTY_PRINT : 0); if (json_last_error() !== JSON_ERROR_NONE) { throw (new \Exception('json not encodable: ' . json_last_error_msg())); } else { return $string; } } /** * @param string $string * @return mixed * @throws \Exception if not decodable * @author Christian Fraß */ function decode( string $string ) { $value = json_decode($string, true); if (json_last_error() !== JSON_ERROR_NONE) { throw (new \Exception('json not decodable: ' . json_last_error_msg())); } else { return $value; } }