Skip to content

Commit 8cb5b59

Browse files
authored
Merge pull request #10 from nichtich/master
Support parsing into associative array (#5)
2 parents 1b28536 + 1bf0207 commit 8cb5b59

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ This method parses *JSON* or *Hjson* text to produce an object or array.
5757
- *text*: the string to parse as JSON or Hjson
5858
- *options*: array
5959
- *keepWsc*: boolean, keep white space and comments. This is useful if you want to edit an hjson file and save it while preserving comments (default false)
60+
- *assoc*: boolean, return associative array instead of object (default false)
6061

6162
### HJSONStringifier: stringify($value, $options)
6263

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"require-dev": {
2727
"phpunit/phpunit": "~4.0"
2828
},
29-
"autoload": {
29+
"autoload": {
3030
"psr-4": {
3131
"HJSON\\": "src/HJSON"
3232
}

src/HJSON/HJSONParser.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ function __construct() {
2525

2626
public function parse($source, $options=[])
2727
{
28-
$this->keepWsc = $options && $options['keepWsc'];
28+
$this->keepWsc = $options && isset($options['keepWsc']) && $options['keepWsc'];
2929
$this->text = $source;
30-
return $this->rootValue();
30+
$data = $this->rootValue();
31+
32+
if ($options && isset($options['assoc']) && $options['assoc']) {
33+
$data = json_decode(json_encode($data), true);
34+
}
35+
36+
return $data;
3137
}
3238

3339
private function resetAt()

tests/HJSONParserTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77

88
class HJSONParserTest extends PHPUnit_Framework_TestCase {
99

10-
1110
public function setUp()
1211
{
1312
parent::setUp();
1413
$this->rootDir = dirname(__FILE__).DIRECTORY_SEPARATOR."assets";
15-
$this->parser = new HJSONParser();
16-
$this->stringifier = new HJSONStringifier();
1714
}
1815

1916
private function load($file, $cr)
@@ -30,11 +27,16 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
3027
$shouldFail = substr($name, 0, 4) === "fail";
3128

3229
try {
33-
$data = $this->parser->parse($text);
30+
$parser = new HJSONParser();
31+
$data = $parser->parse($text);
32+
33+
$arrayData = $parser->parse($text, ['assoc' => true]);
34+
$this->assertEquals($arrayData, json_decode(json_encode($data), true));
3435

3536
if (!$shouldFail) {
3637
$text1 = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
37-
$hjson1 = $this->stringifier->stringify($data, [
38+
$stringifier = new HJSONStringifier();
39+
$hjson1 = $stringifier->stringify($data, [
3840
'eol' => $outputCr ? "\r\n" : "\n",
3941
'emitRootBraces' => true,
4042
'space' => 2

0 commit comments

Comments
 (0)