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')); }
/** * Exports a SocialRecord object to a serialized JSONObject * * @param SocialRecord The SocialRecord to export * @param KeyPair account key pair to export * @param KeyPair personal key pair to export * @return string The exported SocialRecord */ public static function exportSocialRecord(SocialRecord $socialRecord, KeyPair $accountKeyPair = NULL, KeyPair $personalKeyPair = NULL) { $json = new JSONObject(); $json->put('socialRecord', $socialRecord->getJSONObject()); if ($accountKeyPair != NULL) { $json->put('accountPrivateKey', PrivateKey::exportKey($accountKeyPair->getPrivateKey())); } if ($personalKeyPair != NULL) { $json->put('personalPrivateKey', PrivateKey::exportKey($personalKeyPair->getPrivateKey())); } return $json->write(); }
/** * 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; }