Skip to content

Commit d08d1ef

Browse files
author
Alessio Linares
authored
Merge pull request #7 from fayway/master
Allow setting json_encode options when calling getJson()
2 parents 25aa0ee + d46fc76 commit d08d1ef

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Skyscanner/JsonPath/JsonObject.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,15 @@ public function &getValue()
240240

241241
/**
242242
* Returns the json object encoded as string.
243+
* See http://php.net/manual/en/json.constants.php for more information on the $options bitmask.
243244
*
245+
* @param int $options json_encode options bitmask
244246
*
245247
* @return string
246248
*/
247-
public function getJson()
249+
public function getJson($options=0)
248250
{
249-
return json_encode($this->jsonObject);
251+
return json_encode($this->jsonObject, $options);
250252
}
251253

252254
/**

tests/Skyscanner/JsonPath/JsonObjectTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,40 @@ public function testGetJson()
834834
$this->assertEquals('{"size":41,"color":"black","meta":{"code":65076}}', $jsonObject->getJson());
835835
}
836836

837+
public function testGetJsonWithoutOptionsBitmask()
838+
{
839+
$jsonObject = new JsonObject();
840+
$jsonObject
841+
->add('$', 'Ö Kent C. Dodds', 'author')
842+
->add('$', 'À First Timers Only', 'title')
843+
->add('$', array(), 'volunteers')
844+
->add('$.volunteers[0]', 'Fayçal', 'name');
845+
$expectedJson = '{"author":"\u00d6 Kent C. Dodds","title":"\u00c0 First Timers Only","volunteers":[{"name":"Fay\u00e7al"}]}';
846+
$this->assertEquals($expectedJson, $jsonObject->getJson());
847+
}
848+
849+
public function testGetJsonWithOptionsBitmask()
850+
{
851+
$jsonObject = new JsonObject();
852+
$jsonObject
853+
->add('$', 'Ö Kent C. Dodds', 'author')
854+
->add('$', 'À First Timers Only', 'title')
855+
->add('$', array(), 'volunteers')
856+
->add('$.volunteers[0]', 'Fayçal', 'name');
857+
$expectedJson = <<<EOF
858+
{
859+
"author": "Ö Kent C. Dodds",
860+
"title": "À First Timers Only",
861+
"volunteers": [
862+
{
863+
"name": "Fayçal"
864+
}
865+
]
866+
}
867+
EOF;
868+
$this->assertEquals($expectedJson, $jsonObject->getJson(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
869+
}
870+
837871
public function testMagickMethods()
838872
{
839873
$jsonObject = new JsonObject($this->json);

0 commit comments

Comments
 (0)