<?php
/**
* Serializes objects from JSON responses for compatibility with older PHP versions.
*
* @param mixed $object The object to serialize.
* @return string The JSON encoded string.
*/
function serializeObjectToJson( $object ) {
if ( is_object( $object ) ) {
return json_encode( (array) $object, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ); // Convert object to array and encode
} else {
return json_encode( $object, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ); // Encode directly if not an object
}
}
//Example Usage (for testing):
/*
class MyClass {
public $name = "John Doe";
public $age = 30;
}
$obj = new MyClass();
$json = serializeObjectToJson($obj);
echo $json . "\n";
$data = ['key' => 'value', 'number' => 123];
$json2 = serializeObjectToJson($data);
echo $json2 . "\n";
*/
?>
Add your comment