コード例 #1
0
ファイル: JSONArrayUnitTest.php プロジェクト: sgoendoer/json
 public function test()
 {
     $jsonString = '[]';
     $jsonStringString = '["a"]';
     $jsonStringInteger = '[2]';
     $jsonStringBool = '[true]';
     $jsonStringNull = '[null]';
     $jsonStringObject = '[{}]';
     $jsonStringArray = '[[]]';
     // serialization // creation from string
     $this->assertEquals($jsonString, (new JSONArray())->__toString());
     $this->assertEquals(json_decode($jsonString), (new JSONArray())->toArray());
     $this->assertEquals(json_decode($jsonStringString), (new JSONArray($jsonStringString))->toArray());
     $this->assertEquals(json_decode($jsonStringInteger), (new JSONArray($jsonStringInteger))->toArray());
     $this->assertEquals(json_decode($jsonStringBool), (new JSONArray($jsonStringBool))->toArray());
     $this->assertEquals(json_decode($jsonStringNull), (new JSONArray($jsonStringNull))->toArray());
     $this->assertEquals(json_decode($jsonStringObject), (new JSONArray($jsonStringObject))->toArray());
     $this->assertEquals(json_decode($jsonStringArray), (new JSONArray($jsonStringArray))->toArray());
     // building arrays
     $a = new JSONArray();
     $a->put('x');
     $a->put(new JSONArray());
     $a1 = '["x",[]]';
     $this->assertEquals($a1, $a->write());
     // accessing parameters
     $this->assertEquals("x", $a->get(0));
     $this->assertEquals("[]", $a->get(1));
 }
コード例 #2
0
ファイル: JSONObject.php プロジェクト: sgoendoer/json
 /**
  * Produce a JSONArray containing the values of the members of this JSONObject.
  *
  * @param $names An array containing a list of key strings. This determines the sequence of the values in the result.
  * @return A JSONArray of values.
  */
 public function toJSONArray($names = NULL)
 {
     if ($names == NULL || count($names) == 0) {
         return NULL;
     }
     $ja = new JSONArray();
     foreach ($this->map as $key => $value) {
         $ja->put($this->opt($key));
     }
     return $ja;
 }