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 1 commit
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
Next Next commit
[Yaml] Add an option to dump null as tilde
  • Loading branch information
fancyweb committed Mar 30, 2018
commit 8a2b28bfba9a65c41b3931c8d2edcb93fb77cc7f
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy