/** * Convert data into a JSON string * * @param mixed $data Data to convert * @return string JSON data */ protected function jsonify($data) { // JSON_NUMERIC_CHECK removes leading zeros // which is an issue in cases like postcode e.g. 00160 // see https://bugs.php.net/bug.php?id=64695 $json = json_encode($data); //catch JSON parsing error $error = RESTfulAPI_Error::get_json_error(); if ($error !== false) { return new RESTfulAPI_Error(400, $error); } return $json; }
/** * Convert client JSON data to an array of data * ready to be consumed by SilverStripe * * Expects payload to be formatted: * { * "FieldName": "Field value", * "Relations": [1] * } * * @param string $data JSON to be converted to data ready to be consumed by SilverStripe * @return array|false Formatted array representation of the JSON data or false if failed */ public function deserialize($json) { $data = json_decode($json, true); //catch JSON parsing error $error = RESTfulAPI_Error::get_json_error(); if ($error !== false) { return new RESTfulAPI_Error(400, $error); } if ($data) { $data = $this->unformatPayloadData($data); } else { return new RESTfulAPI_Error(400, "No data received."); } return $data; }
/** * Convert client JSON data to an array of data * ready to be consumed by SilverStripe * * Expects payload to be formatted: * { * "className": { * "fieldName": "Field value", * "relations": [1] * } * } * * @param string $data JSON to be converted to data ready to be consumed by SilverStripe * @return array|false Formatted array representation of the JSON data or false if failed */ public function deserialize($json) { $data = json_decode($json, true); //catch JSON parsing error $error = RESTfulAPI_Error::get_json_error(); if ($error !== false) { return new RESTfulAPI_Error(400, $error); } if ($data) { $data = array_shift($data); $data = $this->unformatPayloadData($data); } else { return new RESTfulAPI_Error(400, 'Malformed JSON payload.'); } return $data; }