Skip to content

How to output explicit object by yaml_emit()  #70

@HJ-fukura

Description

@HJ-fukura

json_encode() can ouptut empty object, but yaml_emit() cannot.
Please tell me how to output empty object like {}.

tested with versions:

php 8.1.8 (Windows x86_64)
php_yaml-2.2.2-8.1-nts-vs16-x64.zip

<?php
	$a = [
		'int'        => 123,
		'string'     => 'aaa', 
		'array'      => [],
		'object_ok'  => ['a'=>123],
		'object_ng1' => (object)[],
		'object_ng2' => (object)['a'=>123],
	];
	
	echo json_encode($a, JSON_PRETTY_PRINT), "\n", yaml_emit($a);

output:

{
    "int": 123,
    "string": "aaa",
    "array": [],
    "object_ok": {
        "a": 123
    },
    "object_ng1": {},
    "object_ng2": {
        "a": 123
    }
}
---
int: 123
string: aaa
array: []
object_ok:
  a: 123
object_ng1: !php/object "O:8:\"stdClass\":0:{}"
object_ng2: !php/object "O:8:\"stdClass\":1:{s:1:\"a\";i:123;}"
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions