Skip to content

Commit 5841b11

Browse files
authored
Merge pull request #23 from sprak3000/issue-22-stringify-assoc-arrays
Issue #22 - can't stringify assoc arrays
2 parents 1b49a12 + c6093b1 commit 5841b11

5 files changed

Lines changed: 39 additions & 7 deletions

File tree

src/HJSON/HJSONParser.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ private function error($m)
325325
$colBytes += strlen($ch);
326326
}
327327

328-
for (; $i > 0;
329-
$i--) {
328+
for (; $i > 0; $i--) {
330329
if ($this->text[$i] === "\n") {
331330
$line++;
332331
}
@@ -526,8 +525,7 @@ private function getComment($wat)
526525
$i;
527526
$wat--;
528527
// remove trailing whitespace
529-
for ($i = $this->at - 2; $i > $wat && $this->text[$i] <= ' ' && $this->text[$i] !== "\n";
530-
$i--) {
528+
for ($i = $this->at - 2; $i > $wat && $this->text[$i] <= ' ' && $this->text[$i] !== "\n"; $i--) {
531529
}
532530

533531
// but only up to EOL

src/HJSON/HJSONStringifier.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public function stringify($value, $opt = [])
7777
// many spaces. If it is a string, it will be used as the indent string.
7878
if (is_int($space)) {
7979
$this->indent = '';
80-
for ($i = 0; $i < $space;
81-
$i++) {
80+
for ($i = 0; $i < $space; $i++) {
8281
$this->indent .= ' ';
8382
}
8483
} elseif (is_string($space)) {
@@ -239,6 +238,17 @@ private function str($value, $hasComment = null, $noIndent = null, $isRootObject
239238
case 'array':
240239
$isArray = is_array($value);
241240

241+
$isAssocArray = function (array $arr) {
242+
if (array() === $arr) {
243+
return false;
244+
}
245+
return array_keys($arr) !== range(0, count($arr) - 1);
246+
};
247+
if ($isArray && $isAssocArray($value)) {
248+
$value = (object) $value;
249+
$isArray = false;
250+
}
251+
242252
$kw = null;
243253
$kwl = null; // whitespace & comments
244254
if ($this->keepWsc) {
@@ -324,7 +334,7 @@ private function str($value, $hasComment = null, $noIndent = null, $isRootObject
324334
}
325335
} else {
326336
foreach ($value as $k => $vvv) {
327-
$v = $this->str($value->$k);
337+
$v = $this->str($vvv);
328338
if ($v !== null) {
329339
$partial[] = $this->quoteName($k) . ($startsWithNL($v) ? ':' : ': ') . $v;
330340
}

tests/assets/pass1_result.hjson

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
}
99
{}
1010
[]
11+
{
12+
1: foo
13+
3: bar
14+
}
15+
{
16+
foo: foo
17+
bar: bar
18+
}
1119
-42
1220
true
1321
false

tests/assets/pass1_result.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
},
88
{},
99
[],
10+
{
11+
"1": "foo",
12+
"3": "bar"
13+
},
14+
{
15+
"foo": "foo",
16+
"bar": "bar"
17+
},
1018
-42,
1119
true,
1220
false,

tests/assets/pass1_test.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
{"object with 1 member":["array with 1 element"]},
44
{},
55
[],
6+
{
7+
"1": "foo",
8+
"3": "bar"
9+
},
10+
{
11+
"foo": "foo",
12+
"bar": "bar"
13+
},
614
-42,
715
true,
816
false,

0 commit comments

Comments
 (0)