mustangostang / spyc

A simple YAML loader/dumper class for PHP
MIT License
707 stars 207 forks source link

Added feature to convert {} into empty object using stdClass() #77

Closed ezimuel closed 5 years ago

ezimuel commented 5 years ago

Added the conversion of an empty YAML hash {} into an empty object using new stdClass(). This is needed if you want to differentiate an empty array [] from an empty hash {}. At the moment the two items are converted into array. I need this feature in order to convert a YAML file into a JSON, and I have empty objects ({}). A possible way to accomplish this is to use an empty stdClass() value, as discussed here.

I added a public option Spyc ::setting_empty_hash_as_object to enable or disable this feature (false by default).

I changed the static functions YAMLLoad and YAMLLoadString to allow an optional parameter, as follows:

public static function YAMLLoadString($input, $options = []);
public static function YAMLLoad($input, $options = []);

This can facilitate the settings of the Sync properties. With this PR now you can set this option (also setting_dump_force_quotes and setting_use_syck_is_possible) as follows:

$string = 'test: {}';
$yaml = Spyc::YAMLLoadString($string, [
     'setting_empty_hash_as_object' => true
]);
var_dump($yaml);
/*
array(1) {
  ["test"]=>
  object(stdClass)#2 (0) {
  }
}
*/

I also provided a ParseTest::testElementWithEmptyHash unit test.

ezimuel commented 5 years ago

@mustangostang is this library still maintained? I need a light library to read YAML files. Let me know, thanks!

jackmcdade commented 5 years ago

It's kinda on auto-pilot at the moment. I do have write access and this looks like a reasonable, non-breaking, tested update to me. Gonna merge and tag. 👍

ezimuel commented 5 years ago

Thanks @jackmcdade.

mustangostang commented 5 years ago

agreed there! thanks @jackmcdade and @ezimuel!

ezimuel commented 5 years ago

@jackmcdade You released this with tag 06.2 (you forgot a dash). Moreover, I guess this new release should be 0.6.3 since 0.6.2 has been released Feb 24, 2017.

jackmcdade commented 5 years ago

Wow, really botched that one. My apologies, sorting it out now.

ezimuel commented 5 years ago

@jackmcdade thanks for your fast response! I really appreciate that.

ezimuel commented 5 years ago

@jackmcdade and @mustangostang the tag 0.6.3 has been released but packagist.org is not yet updated. Can you check please? Thanks!