public function testJsonencode() { $a = new stdClass(); $a->prout = 'pue'; $a->couleur = ['marron', 'jaune']; $a->compteur = ['incrémental' => true, 'fiente' => 'vrai']; $this->assertEquals('{"prout":"pue","couleur":["marron","jaune"],"compteur":{"incr\\u00e9mental":true,"fiente":"vrai"}}', p4string::jsonencode($a)); $b = ['un', 'petit' => 'tout petit', 'cul', 1 => 'qui', 10 => 'roule']; $this->assertEquals('{"0":"un","petit":"tout petit","1":"qui","10":"roule"}', p4string::jsonencode($b)); $c = ['gros', 'chien']; $this->assertEquals('["gros","chien"]', p4string::jsonencode($c)); }
/** * * @return string */ public function serialize() { $ret = []; foreach ($this as $key => $value) { if ($value instanceof \DateTime) { $value = $value->format(DATE_ATOM); } if (in_array($key, ['date_fields', 'fields'])) { $value = array_map(function (\databox_field $field) { return $field->get_databox()->get_sbas_id() . '_' . $field->get_id(); }, $value); } if (in_array($key, ['collections', 'business_fields'])) { $value = array_map(function ($collection) { return $collection->get_base_id(); }, $value); } $ret[$key] = $value; } return \p4string::jsonencode($ret); }
private function serializeJSON(\caption_record $caption, $includeBusinessFields) { return \p4string::jsonencode($this->toArray($caption, $includeBusinessFields)); }
/** * Format the data and return serialized string * * @return string */ public function format() { $request_uri = sprintf('%s %s', $this->request->getMethod(), $this->request->getBasePath() . $this->request->getPathInfo()); $ret = ['meta' => ['api_version' => $this->api_version, 'request' => $request_uri, 'response_time' => $this->response_time, 'http_code' => $this->http_code, 'error_type' => $this->error_type, 'error_message' => $this->error_message, 'error_details' => $this->error_details, 'charset' => 'UTF-8'], 'response' => $this->response]; $this->app['dispatcher']->dispatch(PhraseaEvents::API_RESULT, new ApiResultEvent()); if ($this->app['conf']->get(['main', 'api-timers'], false)) { $ret['timers'] = $this->app['api.timers']->toArray(); } switch ($this->response_type) { case self::FORMAT_JSON: default: $return_value = p4string::jsonencode($ret); break; case self::FORMAT_YAML: if ($ret['response'] instanceof stdClass) { $ret['response'] = []; } $dumper = new Symfony\Component\Yaml\Dumper(); $return_value = $dumper->dump($ret, 8); break; case self::FORMAT_JSONP: $callback = trim($this->request->get('callback')); $return_value = $callback . '(' . p4string::jsonencode($ret) . ')'; break; } return $return_value; }