Skip to content

Commit

Permalink
Merge pull request #7 from fayway/master
Browse files Browse the repository at this point in the history
Allow setting json_encode options when calling getJson()
  • Loading branch information
Alessio Linares authored Sep 13, 2017
2 parents 25aa0ee + d46fc76 commit d08d1ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Skyscanner/JsonPath/JsonObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,15 @@ public function &getValue()

/**
* Returns the json object encoded as string.
* See http://php.net/manual/en/json.constants.php for more information on the $options bitmask.
*
* @param int $options json_encode options bitmask
*
* @return string
*/
public function getJson()
public function getJson($options=0)
{
return json_encode($this->jsonObject);
return json_encode($this->jsonObject, $options);
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/Skyscanner/JsonPath/JsonObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,40 @@ public function testGetJson()
$this->assertEquals('{"size":41,"color":"black","meta":{"code":65076}}', $jsonObject->getJson());
}

public function testGetJsonWithoutOptionsBitmask()
{
$jsonObject = new JsonObject();
$jsonObject
->add('$', 'Ö Kent C. Dodds', 'author')
->add('$', 'À First Timers Only', 'title')
->add('$', array(), 'volunteers')
->add('$.volunteers[0]', 'Fayçal', 'name');
$expectedJson = '{"author":"\u00d6 Kent C. Dodds","title":"\u00c0 First Timers Only","volunteers":[{"name":"Fay\u00e7al"}]}';
$this->assertEquals($expectedJson, $jsonObject->getJson());
}

public function testGetJsonWithOptionsBitmask()
{
$jsonObject = new JsonObject();
$jsonObject
->add('$', 'Ö Kent C. Dodds', 'author')
->add('$', 'À First Timers Only', 'title')
->add('$', array(), 'volunteers')
->add('$.volunteers[0]', 'Fayçal', 'name');
$expectedJson = <<<EOF
{
"author": "Ö Kent C. Dodds",
"title": "À First Timers Only",
"volunteers": [
{
"name": "Fayçal"
}
]
}
EOF;
$this->assertEquals($expectedJson, $jsonObject->getJson(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}

public function testMagickMethods()
{
$jsonObject = new JsonObject($this->json);
Expand Down

0 comments on commit d08d1ef

Please sign in to comment.