Skip to content

Commit

Permalink
API Convert::raw2json can be passed an optional bitmask of JSON const…
Browse files Browse the repository at this point in the history
…ants as options
  • Loading branch information
robbieaverill committed Jan 24, 2017
1 parent de8b5d6 commit 3583f1f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
23 changes: 13 additions & 10 deletions core/Convert.php
Expand Up @@ -126,24 +126,27 @@ public static function raw2js($val) {
}

/**
* Encode a value as a JSON encoded string.
* Encode a value as a JSON encoded string. You can optionally pass a bitmask of
* JSON constants as options through to the encode function.
*
* @param mixed $val Value to be encoded
* @return string JSON encoded string
* @param mixed $val Value to be encoded
* @param int $options Optional bitmask of JSON constants
* @return string JSON encoded string
*/
public static function raw2json($val) {
return json_encode($val);
public static function raw2json($val, $options = 0) {
return json_encode($val, $options);
}

/**
* Encode an array as a JSON encoded string.
* THis is an alias to {@link raw2json()}
* This is an alias to {@link raw2json()}
*
* @param array $val Array to convert
* @return string JSON encoded string
* @param array $val Array to convert
* @param int $options Optional bitmask of JSON constants
* @return string JSON encoded string
*/
public static function array2json($val) {
return self::raw2json($val);
public static function array2json($val, $options = 0) {
return self::raw2json($val, $options);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/core/ConvertTest.php
Expand Up @@ -303,6 +303,20 @@ public function testRaw2JSON() {
);
}

/**
* Test that a context bitmask can be passed through to the json_encode method in {@link Convert::raw2json()}
* and in {@link Convert::array2json()}
*/
public function testRaw2JsonWithContext()
{
$data = array('foo' => 'b"ar');
$expected = '{"foo":"b\u0022ar"}';
$result = Convert::raw2json($data, JSON_HEX_QUOT);
$this->assertSame($expected, $result);
$wrapperResult = Convert::array2json($data, JSON_HEX_QUOT);
$this->assertSame($expected, $wrapperResult);
}

/**
* Tests {@link Convert::xml2array()}
*/
Expand Down

0 comments on commit 3583f1f

Please sign in to comment.