@@ -26,6 +26,9 @@ public static function stringify(mixed $value): string
2626 }
2727 return $ leading . self ::stringifyObject ($ value , 0 , $ annotated );
2828 }
29+ if ($ value instanceof \stdClass) {
30+ return $ leading . self ::stringifyObject ((array ) $ value , 0 , $ annotated );
31+ }
2932 if ($ annotated !== null ) {
3033 foreach ($ annotated ->danglingComments as $ c ) {
3134 $ dangling .= "\n" . '# ' . $ c ;
@@ -66,6 +69,9 @@ private static function doStringify(mixed $value, int $level): string
6669 }
6770 return self ::stringifyObject ($ value , $ level , $ annotated );
6871 }
72+ if ($ value instanceof \stdClass) {
73+ return self ::stringifyObject ((array ) $ value , $ level , $ annotated );
74+ }
6975 throw new \InvalidArgumentException ('Unsupported value type: ' . get_debug_type ($ value ));
7076 }
7177
@@ -100,7 +106,7 @@ private static function stringifyArray(array $arr, int $level, ?Annotated $annot
100106 }
101107 }
102108 $ out .= $ childIndent . self ::doStringify ($ arr [$ i ], $ level + 1 );
103- if ($ elAnnotated !== null && $ elAnnotated ->trailingComment !== null && !is_array ($ innerValue )) {
109+ if ($ elAnnotated !== null && $ elAnnotated ->trailingComment !== null && !is_array ($ innerValue ) && ! $ innerValue instanceof \stdClass ) {
104110 $ out .= ' # ' . $ elAnnotated ->trailingComment ;
105111 }
106112 }
@@ -123,6 +129,9 @@ private static function stringifyObject(array $obj, int $level, ?Annotated $anno
123129 {
124130 $ keys = array_keys ($ obj );
125131 $ hasDangling = $ annotated !== null && count ($ annotated ->danglingComments ) > 0 ;
132+ if (count ($ keys ) === 0 && !$ hasDangling ) {
133+ return '{} ' ;
134+ }
126135 $ childIndent = self ::getIndent ($ level + 1 );
127136 $ parentIndent = self ::getIndent ($ level );
128137 $ out = "{ \n" ;
@@ -146,7 +155,7 @@ private static function stringifyObject(array $obj, int $level, ?Annotated $anno
146155 }
147156 }
148157 $ out .= $ childIndent . self ::stringifyKey ($ key ) . ': ' . self ::doStringify ($ obj [$ key ], $ level + 1 );
149- if ($ elAnnotated !== null && $ elAnnotated ->trailingComment !== null && !is_array ($ innerValue )) {
158+ if ($ elAnnotated !== null && $ elAnnotated ->trailingComment !== null && !is_array ($ innerValue ) && ! $ innerValue instanceof \stdClass ) {
150159 $ out .= ' # ' . $ elAnnotated ->trailingComment ;
151160 }
152161 }
0 commit comments