Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to output explicit object by yaml_emit() #70

Open
HJ-fukura opened this issue Sep 1, 2022 · 4 comments
Open

How to output explicit object by yaml_emit() #70

HJ-fukura opened this issue Sep 1, 2022 · 4 comments
Labels

Comments

@HJ-fukura
Copy link

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;}"
...
@vaheen
Copy link

vaheen commented Sep 6, 2022

Consider the following workaround to convert objects to arrays

echo yaml_emit(json_decode(json_encode($a),true));

output:

---
int: 123
string: aaa
array: []
object_ok:
  a: 123
object_ng1: []
object_ng2:
  a: 123
...

@bd808 bd808 added the question label Mar 12, 2023
@bd808
Copy link
Collaborator

bd808 commented Mar 12, 2023

I do not believe that the current implementation can be coerced into emitting {}.

The extension emits (most) objects as serialized data with an associated !php/object tag. The {} you want is an empty flow mapping. Both YAML mappings and sequences are generated for PHP arrays. If the array contains non-numeric keys or non-sequential numeric keys then it is emitted as a mapping, otherwise it is emitted as an array. An empty array will emit as an empty flow sequence ([]) and not an empty flow mapping ({}).

@JohnRDOrazio
Copy link

Perhaps an internal serialization of objects to associative arrays could be considered? It would make it that much easier to use...

@JohnRDOrazio
Copy link

JohnRDOrazio commented May 2, 2024

I'm seeing that there is an ini setting for decoding serialized objects yaml.decode_php=1. Perhaps there could be a similar setting for encoding serialized objects yaml.encode_php=1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants