Esempio n. 1
0
 /**
  *
  */
 public function testReading()
 {
     $filename = new Stream($this->getJSON());
     $catalog = array();
     $fh = fopen($filename, "r");
     $reader = new Reader($fh);
     $reader->enter(Reader::TYPE_OBJECT);
     // enter root object
     $catalog['id'] = $reader->read("catalog");
     // read catalog node
     $reader->enter("items", Reader::TYPE_ARRAY);
     // enter item array
     while ($product = $reader->read()) {
         // read product structure
         $catalog['products'][] = $product;
     }
     $reader->leave();
     // leave item node
     $reader->leave();
     // leave root object
     fclose($fh);
     $this->assertEquals($this->getData(), $catalog);
 }
Esempio n. 2
0
    /**
     *
     */
    public function testReaderLeaving()
    {
        $stream = new Stream(<<<JSON
        {
            "first": "1",
            "items": [
                "String",
                {"sku":"ABC","qty":1},
                {"sku":"A\\"BC","qty":.095},
                999,
                {"sku":"CDE","qty":[{  }]}
            ],
            "last": "2"
        }
JSON
);
        $reader = new Reader(fopen($stream, "r"));
        $this->assertTrue($reader->enter(null, Reader::TYPE_OBJECT));
        // enter root object
        $first = $reader->read("first");
        // read property first
        $this->assertTrue($reader->enter("items"));
        // enter property items
        $this->assertTrue($reader->leave());
        // leave items node
        $last = $reader->read("last");
        // leave root node
        $this->assertEquals("1", $first);
        $this->assertEquals("2", $last);
    }