/** * @param string|array $serialized * @param int $format * * @return GameInterface */ public function deserialize($serialized, $format) { if ($format === self::FORMAT_ARRAY) { $serialized = json_encode($serialized, true); } return $this->serializer->deserialize($serialized, Game::class, self::FORMAT_JSON, $this->deserializationContext); }
/** * @param $id * @return Result */ protected function call($method, $resource, $body = null, $acceptedCodes = array(200)) { try { $response = $this->client->request($method, $resource, array('body' => $body)); $responseBody = (string) $response->getBody(); if ($responseBody) { /** @var Result $result */ $result = $this->serializer->deserialize($responseBody, $this->getResultClass(), 'json'); $result->deserializeData($this->serializer, $this->getModel()); } else { $result = new Result(); } $result->setSuccess(in_array($response->getStatusCode(), $acceptedCodes))->setMessage($response->getReasonPhrase()); return $result; } catch (GuzzleException $ge) { if ($ge->getCode() == \Symfony\Component\HttpFoundation\Response::HTTP_TOO_MANY_REQUESTS && php_sapi_name() == "cli") { sleep(5); return $this->call($method, $resource, $body, $acceptedCodes); } else { $result = new Result(); $result->setSuccess(false)->setMessage(sprintf("Client error: %s", $ge->getMessage())); return $result; } } catch (\Exception $e) { $result = new Result(); $result->setSuccess(false)->setMessage(sprintf("General error: %s", $e->getMessage())); return $result; } }
public function testSerializationToJson() { $subject = new Message('to', 'from', 'subject', 'message'); $data = $this->serializer->serialize($subject, 'json'); $object = $this->serializer->deserialize($data, get_class($subject), 'json'); $this->assertEquals($subject, $object); }
/** * @inheritdoc */ public function deserialize($data, $type, $format, $groups = null) { $context = null; if ($groups) { $context = DeserializationContext::create()->setGroups($groups); } return $this->serializer->deserialize($data, $type, $format, $context); }
public function deserialize(SerializedObjectInterface $data) { try { return $this->serializer->deserialize($data->getData(), $data->getContentType(), 'json'); } catch (\Exception $ex) { throw new UnknownSerializedTypeException($data->getType(), $ex); } }
/** * @param string $json * @param string $type * @param DeserializationContext $context * * @return object|null */ protected function deserialize($json, $type, DeserializationContext $context = null) { $object = $this->serializer->deserialize($json, $type, 'json', $context); if (!$object instanceof $type) { return null; } return $object; }
/** * {@inheritdoc} */ public function createCampaignSuppressionImport($campaignId, $source, $location, $data) { try { return $this->client->getCommand('createCampaignSuppressionImport', array('campaignId' => $campaignId, 'suppression_import' => array('source' => $source, 'location' => $location, 'data' => $data)))->execute(); } catch (ClientErrorResponseException $e) { return $this->serializer->deserialize($e->getResponse()->getBody(), 'EBC\\AdvertiserClient\\Campaign\\InvalidImportResponse', 'json'); } }
public function testSerializationToJson() { $exception = new \Exception('foobar', 100); $subject = new ExceptionResponse($exception); $data = $this->serializer->serialize($subject, 'json'); $object = $this->serializer->deserialize($data, ExceptionResponse::class, 'json'); $this->assertEquals($subject, $object); }
/** * Load the Satis JSON. * * @return \KevinDierkx\Muse\Repositories\Satis\SatisInterface */ public function load() { if (!Storage::has('satis.json')) { throw new \RuntimeException("The 'satis.json' does not exist."); } $data = Storage::get('satis.json'); return $this->serializer->deserialize($data, $this->getModel(), 'json'); }
/** * Deserialize XML into a BaseResponse * * @param $xml * @return BaseResponse * @throws SerializationException */ public function deserialize($xml) { try { return $this->serializer->deserialize($xml, 'SMH\\Enom\\Response\\BaseResponse', 'xml'); } catch (\RuntimeException $ex) { throw new SerializationException($ex->getMessage(), null, $ex); } }
/** * {@inheritdoc} */ public function apply(Request $request, ParamConverter $configuration) { $json = $request->getContent(); /** @var Event $event */ $event = $this->serializer->deserialize($json, 'Ndewez\\EventsBundle\\Model\\Event', 'json'); $request->attributes->set('event', $event); return true; }
/** * @param Request $request * * @return array */ public function deserializeRequest(Request $request) { $format = $this->checkAcceptHeader($request); try { return $this->serializer->deserialize($request->getContent(), 'array', $format); } catch (\Exception $e) { throw new \RuntimeException('Could not deserialize content from the request.'); } }
/** * @param Event $event */ public function listen(Event $event) { if (!in_array($event->getTitle(), $this->events, true)) { return; } $payload = $this->serializer->deserialize($event->getPayload(), $event->getNamespace(), 'json'); $ndewezEvent = new NdewezEvent($payload); $this->dispatcher->dispatch(sprintf('ndewez_events.%s', $event->getTitle()), $ndewezEvent); }
/** * Send a request to fastbill. * * @param mixed $request The request to send. * @param string $responseClass The class that should be used to unserialize the response. * @return ApiResponseInterface */ protected function sendRequest(RequestInterface $request, $responseClass) { if (!in_array(ApiResponseInterface::class, class_implements($responseClass))) { throw new \InvalidArgumentException('The response class must implement "' . ApiResponseInterface::class . '".'); } $body = $this->serializer->serialize($request, 'json'); $response = $this->transport->sendRequest($body); return $this->serializer->deserialize($response, $responseClass, 'json'); }
public function enrichRequestWithParsedBody(ServerRequestInterface $request) { if ($request->hasHeader(HeaderName::CONTENT_TYPE) && $request->getHeaderLine(HeaderName::CONTENT_TYPE) === 'application/json') { $parsedBody = $this->serializer->deserialize($request->getBody()->__toString(), 'array', 'json'); return $request->withParsedBody($parsedBody); } else { return $request->withParsedBody([]); } }
/** * {@inheritdoc} */ public function execute(AMQPMessage $amqpMessage) { // Ack message and get event $amqpMessage->delivery_info['channel']->basic_ack($amqpMessage->delivery_info['delivery_tag']); /** @var Event $event */ $event = $this->serializer->deserialize($amqpMessage->getBody(), 'Ndewez\\EventsBundle\\Model\\Event', 'json'); // Process $this->listen->listen($event); }
/** * @param string $format * @dataProvider provideSupportedFormats */ public function testSerializeToJsonWithMemberType($format) { $subject = new MemberTest(); $subject->aString = 'foobar'; $subject->enum = TaggedTestType::VALUE1(); $data = $this->serializer->serialize($subject, $format); $object = $this->serializer->deserialize($data, MemberTest::class, $format); $this->assertEquals($subject, $object); }
/** * {@inheritdoc} */ public function deserialize($json) { $context = DeserializationContext::create(); if (!is_null($this->groupName)) { $context->setGroups(array($this->groupName)); $context->setSerializeNull($this->serializeNull); } return $this->jmsSerializer->deserialize($json, $this->documentClass, 'json', $context); }
/** * @param Job $expectedJob * @param string $data * @param array $groups * @dataProvider provideSerializedJob */ public function testDeserialization($expectedJob, $data, array $groups = []) { $context = null; if (count($groups) > 0) { $context = new DeserializationContext(); $context->setGroups($groups); } $job = $this->serializer->deserialize($data, Job::class, 'json', $context); $this->assertEquals($expectedJob, $job); }
/** * @param GuzzleCreateResponseClassEvent $event * * @throws GuzzleResponseClassException */ public function commandParseResponse(GuzzleCreateResponseClassEvent $event) { /** @var GuzzleCommandInterface $command */ $command = $event['command']; $className = $this->getClassName($command); // if the guzzle way fromCommand is present don't do anything if (!method_exists($className, 'fromCommand')) { $event->setResult($this->serializer->deserialize($command->getResponse()->getBody(), $className, 'json')); } }
/** * Execute * * @param AMQPMessage $msg The message * @throws \InvalidArgumentException * * @return mixed false to reject and requeue, any other value to acknowledge */ public function execute(AMQPMessage $msg) { if (is_null($this->messageClassName)) { throw new \InvalidArgumentException('You have to specify Domain class name'); } /** @var MessageInterface $message */ $message = $this->serializer->deserialize($msg->body, $this->messageClassName, 'json'); $this->messageDispatcher->dispatchMessage($message); return true; }
/** * @param Request $request * * @return array */ public function deserializeRequest(Request $request) { $type = 'array'; $format = $this->checkAcceptHeader($request); try { return $this->serializer->deserialize($request->getContent(), $type, $format); } catch (\Exception $e) { throw new \RuntimeException('Could not deserialize content to object of \'' . $type . '\' type and \'' . $format . '\' format.'); } }
/** * @param string $data * @param string $type * * @return array|object * @throws SerializerException */ public function fromJson($data, $type) { try { $result = $this->serializer->deserialize($data, $type, 'json'); } catch (\Exception $exception) { $adaptedException = new SerializerException($exception->getMessage(), $exception->getCode(), $exception); $this->logException($adaptedException); throw $adaptedException; } return $result; }
public function testSerializationToJson() { $definition = new Definition(); $definition->setType('LOCAL'); $definition->setPath('/path/to/filesystem'); $definition->setProperties(array('create' => true, 'mode' => 0755)); $data = $this->serializer->serialize($definition, 'json'); $object = $this->serializer->deserialize($data, 'Abc\\Filesystem\\Definition', 'json'); $this->assertEquals($definition, $object); $this->assertSame($definition->getProperties(), $object->getProperties()); }
/** * @depends testSerialize */ public function testDeserialize($data) { $result = $this->serializer->deserialize($data, PageBridge::class, 'json'); $this->assertInstanceOf(StructureBridge::class, $result); $this->assertEquals('internallinks', $result->getKey()); $property = $result->getProperty('internalLinks'); $this->assertInstanceOf(Property::class, $property); $value = $property->getValue(); $this->assertInternalType('array', $value); $this->assertCount(1, $value); }
/** * @param $jsonString * * @return EmailTemplate[] */ public function createFromJsonCollection($jsonString) { /* * @var EmailTemplate[] */ $templates = $this->serializer->deserialize($jsonString, 'array<TPN\\EmailTemplatesComponent\\Model\\EmailTemplate>', 'json'); foreach ($templates as $template) { $template->setChildren($this->arrayConverter->createFromArray($template->getBody())); $this->validateTemplate($template); $template->setBody($template->render()); } return $templates; }
public function populateRequestParams(FilterControllerEvent $event) { if ($event->getController()[0] instanceof ApiControllerInterface) { $request = $event->getRequest(); $resource = $this->resourceResolver->resolve($request->attributes->get('resource')); $action = $this->actionResolver->resolve($resource, $request->attributes->get('actionName'), $request->getMethod(), null !== $request->attributes->get('identifier')); $content = $request->getContent(); $payload = empty($content) ? null : $this->serializer->deserialize($content, 'array', 'json'); $request->attributes->set(self::API_REQUEST_RESOURCE, $resource); $request->attributes->set(self::API_REQUEST_ACTION, $action); $request->attributes->set(self::API_REQUEST_PAYLOAD, $payload); } }
/** * Send a request to fastbill. * * @param mixed $request The request to send. * @param string $responseClass The class that should be used to unserialize the response. * @return ApiResponseInterface * @throws ApiResponseException If the API response has errors. */ protected function sendRequest(RequestInterface $request, $responseClass) { if (!in_array(ApiResponseInterface::class, class_implements($responseClass))) { throw new \InvalidArgumentException('The response class must implement "' . ApiResponseInterface::class . '".'); } $body = $this->serializer->serialize($request, 'json'); $response = $this->transport->sendRequest($body); /** @var ApiResponseInterface $apiResponse */ $apiResponse = $this->serializer->deserialize($response, $responseClass, 'json'); if ($apiResponse->getResponse() !== null && $apiResponse->getResponse()->hasErrors()) { throw new ApiResponseException(sprintf('Error calling "%s"', $request->getService()), $apiResponse->getResponse()->getErrors()); } return $apiResponse; }
/** * @param CommandRunner $runner Runs a console command. * @param XmlManipulator $xmlManipulator Helper to change the content of a xml file. * @param LoaderInterface $definitionLoader JSON definition loader * @param SerializerInterface $serializer Serializer * @param string|null $bundleAdditions Additional bundles list in JSON format * @param string|null $serviceWhitelist Service whitelist in JSON format * @param string|null $name The name of the command; passing null means it must be set in * configure() */ public function __construct(CommandRunner $runner, XmlManipulator $xmlManipulator, LoaderInterface $definitionLoader, SerializerInterface $serializer, $bundleAdditions = null, $serviceWhitelist = null, $name = null) { parent::__construct($name); $this->runner = $runner; $this->xmlManipulator = $xmlManipulator; $this->definitionLoader = $definitionLoader; $this->serializer = $serializer; if ($bundleAdditions !== null && $bundleAdditions !== '') { $this->bundleAdditions = $serializer->deserialize($bundleAdditions, 'array<string>', 'json'); } if ($serviceWhitelist !== null && $serviceWhitelist !== '') { $this->serviceWhitelist = $serializer->deserialize($serviceWhitelist, 'array<string>', 'json'); } }
public function getDeserializedRequestContent($rawContent, HeaderCollectionInterface $headerCollection) { if (!empty($rawContent)) { $contentType = $headerCollection->getHeaderValue(HeaderName::CONTENT_TYPE); if ($this->stringUtility->contains($contentType, ContentType::APPLICATION_JSON)) { $deserializedContent = $this->serializer->deserialize($rawContent, 'array', 'json'); } else { throw new UnsupportedMediaTypeException('Submitted media type not supported'); } } else { $deserializedContent = []; } return $deserializedContent; }