Skip to content

[Yaml] Add flag to dump NULL as ~ #32669

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

Merged
merged 1 commit into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* Added support to dump `null` as `~` by using the `Yaml::DUMP_NULL_AS_TILDE` flag.

4.3.0
-----

Expand Down
17 changes: 14 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Inline
private static $objectSupport = false;
private static $objectForMap = false;
private static $constantSupport = false;
private static $nullAsTilde = false;

/**
* @param int $flags
Expand All @@ -45,6 +46,7 @@ public static function initialize($flags, $parsedLineNumber = null, $parsedFilen
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
self::$nullAsTilde = (bool) (Yaml::DUMP_NULL_AS_TILDE & $flags);
self::$parsedFilename = $parsedFilename;

if (null !== $parsedLineNumber) {
Expand Down Expand Up @@ -129,7 +131,7 @@ public static function dump($value, int $flags = 0): string
throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
}

return 'null';
return self::dumpNull($flags);
case $value instanceof \DateTimeInterface:
return $value->format('c');
case \is_object($value):
Expand All @@ -155,11 +157,11 @@ public static function dump($value, int $flags = 0): string
throw new DumpException('Object support when dumping a YAML file has been disabled.');
}

return 'null';
return self::dumpNull($flags);
case \is_array($value):
return self::dumpArray($value, $flags);
case null === $value:
return 'null';
return self::dumpNull($flags);
case true === $value:
return 'true';
case false === $value:
Expand Down Expand Up @@ -256,6 +258,15 @@ private static function dumpArray(array $value, int $flags): string
return sprintf('{ %s }', implode(', ', $output));
}

private static function dumpNull(int $flags): string
{
if (Yaml::DUMP_NULL_AS_TILDE & $flags) {
return '~';
}

return 'null';
}

/**
* Parses a YAML scalar.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ public function testNegativeIndentationThrowsException()
{
new Dumper(-4);
}

public function testDumpNullAsTilde()
{
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
}
}

class A
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Yaml
const PARSE_CONSTANT = 256;
const PARSE_CUSTOM_TAGS = 512;
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
const DUMP_NULL_AS_TILDE = 2048;

/**
* Parses a YAML file into a PHP value.
Expand Down