Example #1
0
 public function test()
 {
     $jsonString = '{}';
     $jsonStringString = '{"a": "a"}';
     $jsonStringInteger = '{"b": 1}';
     $jsonStringBool = '{"c": true}';
     $jsonStringNull = '{"d": null}';
     $jsonStringObject = '{"e": {}}';
     $jsonStringArray = '{"f": []}';
     // serialization // creation from string
     $this->assertEquals($jsonString, (new JSONObject())->__toString());
     $this->assertEquals(json_decode($jsonString), (new JSONObject())->toStdClass());
     $this->assertEquals(json_decode($jsonStringString), (new JSONObject($jsonStringString))->toStdClass());
     $this->assertEquals(json_decode($jsonStringInteger), (new JSONObject($jsonStringInteger))->toStdClass());
     $this->assertEquals(json_decode($jsonStringBool), (new JSONObject($jsonStringBool))->toStdClass());
     $this->assertEquals(json_decode($jsonStringNull), (new JSONObject($jsonStringNull))->toStdClass());
     $this->assertEquals(json_decode($jsonStringObject), (new JSONObject($jsonStringObject))->toStdClass());
     $this->assertEquals(json_decode($jsonStringArray), (new JSONObject($jsonStringArray))->toStdClass());
     // escaping / quoting
     $this->assertEquals('\\\\n', JSONObject::quote('\\n'));
     $this->assertEquals('\\/', JSONObject::quote('/'));
     $this->assertEquals('\\\\', JSONObject::quote('\\'));
     // building objects
     $a = new JSONObject();
     $a->put('arr', new JSONArray());
     $a->get('arr')->put('value');
     $a1 = '{"arr":["value"]}';
     $this->assertEquals($a1, $a->write());
     $b = (new JSONObject())->put('obj', new JSONObject('{"a": "1"}'));
     $b->put("b", 2);
     $b1 = '{"obj":{"a":"1"},"b":2}';
     $this->assertEquals($b1, $b->write());
     // accessing parameters
     $this->assertEquals("1", $b->get('obj')->get("a"));
     $this->assertEquals(2, $b->get('b'));
 }
Example #2
0
 /**
  * Imports a SocialRecord from a string resource
  * 
  * @param $source The string to parse
  * @return array Array containting the SocialRecord object and optinally the KeyPair(s)
  */
 public static function importSocialRecord($source)
 {
     $json = new JSONObject($source);
     $socialRecord = SocialRecordBuilder::buildFromJSON($json->get('socialRecord'));
     $result = array('socialRecord' => $socialRecord);
     if ($json->has('accountPrivateKey')) {
         $result['accountKeyPair'] = new KeyPair($json->get('accountPrivateKey'), $socialRecord->getAccountPublicKey());
     }
     if ($json->has('personalPrivateKey')) {
         $result['personalKeyPair'] = new KeyPair($json->get('personalPrivateKey'), $socialRecord->getPersonalPublicKey());
     }
     return $result;
 }
Example #3
0
 /**
  * Produces a JSONObject from the JSONArray
  *
  * @param names An array containing a list of key strings. These will be paired with the values. If $names contains
  * less values than the source JSONArray, remaining values will be ommitted.
  * @return A JSONObject, or null if there are no names or if this JSONArray has no values.
  */
 public function toJSONObject($names)
 {
     if (!is_array($names)) {
         throw new JSONException('Provided value must be an array');
     }
     if ($names == NULL || count($names) == 0 || $this->length() == 0) {
         return NULL;
     }
     $jsonObject = new JSONObject();
     foreach ($this->map as $index => $value) {
         $jsonObject->put($value, $names[$index]);
     }
     return $jsonObject;
 }