From f9a18dff7a626c687bafce74b1664cde894fcc54 Mon Sep 17 00:00:00 2001 From: Mark Amery Date: Wed, 19 Nov 2014 16:26:28 +0000 Subject: [PATCH] Don't rely on count() working on lists Rationale is basically the same as for d6930f966499bd4bf76ccfeee7f5dfdc60a3df93; the 'list' may actually be a Traversible object in which case it is not obligated to implement the Countable interface --- src/JsonStream/Encoder.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/JsonStream/Encoder.php b/src/JsonStream/Encoder.php index d8f6862..e53ccc2 100644 --- a/src/JsonStream/Encoder.php +++ b/src/JsonStream/Encoder.php @@ -133,13 +133,16 @@ private function _isList($value) private function _encodeList($list) { $this->_writeValue('['); - + + $firstIteration = true; + foreach ($list as $x => $value) { - $this->encode($value); - - if ($x < count($list) - 1) { + if (!$firstIteration) { $this->_writeValue(','); } + $firstIteration = false; + + $this->encode($value); } $this->_writeValue(']');