Skip to content

[Yaml] Add an option to dump null as tilde #26717

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

Closed
wants to merge 2 commits into from
Closed
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.1.0
-----

* Added support to dump `null` values as `~` through the `Yaml::DUMP_NULL_AS_TILDE` flag.

4.0.0
-----

Expand Down
15 changes: 12 additions & 3 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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::getNullRepresentationValue($flags);
case $value instanceof \DateTimeInterface:
return $value->format('c');
case is_object($value):
Expand All @@ -153,11 +153,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::getNullRepresentationValue($flags);
case is_array($value):
return self::dumpArray($value, $flags);
case null === $value:
return 'null';
return self::getNullRepresentationValue($flags);
case true === $value:
return 'true';
case false === $value:
Expand Down Expand Up @@ -738,4 +738,13 @@ private static function getHexRegex(): string
{
return '~^0x[0-9a-f_]++$~i';
}

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

return 'null';
}
}
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 @@ -421,6 +421,11 @@ public function testNegativeIndentationThrowsException()
{
new Dumper(-4);
}

public function testDumpNullAsTilde()
{
$this->assertSame('{ foo: ~ }', $this->dumper->dump(array('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