Converts a Cloze question into a JSON-encodable structure.

@param ClozeQuestion $clozeQuestion
@param array         $options

@return array
497089
public function serialize(ClozeQuestion $clozeQuestion, array $options = [])
    {
        $serialized = [
            'text' => $clozeQuestion->getText(),
            'holes' => $this->serializeHoles($clozeQuestion),
        ];

        if (in_array(Transfer::INCLUDE_SOLUTIONS, $options)) {
            $serialized['solutions'] = $this->serializeSolutions($clozeQuestion, $options);
        }

        return $serialized;
    }
221571
private function deserializeSolutions(OpenQuestion $wordsQuestion, array $solutions, array $options = [])
    {
        $updatedKeywords = $this->keywordSerializer->deserializeCollection($solutions, $wordsQuestion->getKeywords()->toArray(), $options);

        // Replace keywords collection by the updated one
        $wordsQuestion->setKeywords($updatedKeywords);
    }
348321
private function serializeMetadata(Item $question, array $options = [])
    {
        $metadata = ['protectQuestion' => $question->getProtectUpdate()];

        $creator = $question->getCreator();

        if (!empty($creator)) {
            $metadata['creator'] = $this->userSerializer->serialize($creator, $options);
            // TODO : remove me. for retro compatibility with old schema
            $metadata['authors'] = [$this->userSerializer->serialize($creator, $options)];
        }

        if ($question->getDateCreate()) {
            $metadata['created'] = DateNormalizer::normalize($question->getDateCreate());
        }

        if ($question->getDateModify()) {
            $metadata['updated'] = DateNormalizer::normalize($question->getDateModify());
        }

        if (in_array(Transfer::INCLUDE_ADMIN_META, $options)) {
            /** @var ExerciseRepository $exerciseRepo */
            $exerciseRepo = $this->om->getRepository(Exercise::class);

            // Gets exercises that use this item
            $exercises = $exerciseRepo->findByQuestion($question);
            $metadata['usedBy'] = array_map(function (Exercise $exercise) {
                return $exercise->getUuid();
            }, $exercises);

            // Gets users who have access to this item
            $users = $this->om->getRepository(Shared::class)->findBy(['question' => $question]);
            $metadata['sharedWith'] = array_map(function (Shared $sharedQuestion) use ($options) {
                $shared = [
                    'adminRights' => $sharedQuestion->hasAdminRights(),
                    'user' => $this->userSerializer->serialize($sharedQuestion->getUser(), $options),
                ];

                return $shared;
            }, $users);
        }

        return $metadata;
    }
77868
public function serialize(BooleanQuestion $question, array $options = [])
    {
        // Serializes choices
        $choices = $this->serializeChoices($question, $options);

        if (in_array(Transfer::SHUFFLE_ANSWERS, $options)) {
            shuffle($choices);
        }

        $serialized = ['choices' => $choices];

        if (in_array(Transfer::INCLUDE_SOLUTIONS, $options)) {
            $serialized['solutions'] = $this->serializeSolutions($question);
        }

        return $serialized;
    }
372508
protected function getFormattedListQuestion($question, $default, array $options)
    {
        $list = '';
        foreach ($options as $key => $title) {
            $list .= "  $key - $title".($default == $key ? ' (Default)' : '').PHP_EOL;
        }

        return $question.":\n".$list;
    }
1222255
public function toJson($options = 0)
    {
        if ($this->object instanceof Jsonable) {
            return $this->object->toJson();
        }

        return json_encode($this->object);
    }
1005651
protected function makeRequest($url, $options = array(), $parseJson = true) {
		$options['query']['alt'] = 'json';
		return parent::makeRequest($url, $options, $parseJson);
	}
1077879
public function serialize(ChoiceQuestion $choiceQuestion, array $options = [])
    {
        $serialized = [
            'random' => $choiceQuestion->getShuffle(),
            'multiple' => $choiceQuestion->isMultiple(),
            'numbering' => $choiceQuestion->getNumbering(),
            'direction' => $choiceQuestion->getDirection(),
        ];

        // Serializes choices
        $choices = $this->serializeChoices($choiceQuestion, $options);

        if ($choiceQuestion->getShuffle() && in_array(Transfer::SHUFFLE_ANSWERS, $options)) {
            shuffle($choices);
        }

        $serialized['choices'] = $choices;

        if (in_array(Transfer::INCLUDE_SOLUTIONS, $options)) {
            $serialized['solutions'] = $this->serializeSolutions($choiceQuestion);
        }

        return $serialized;
    }
685368
public function serialize(OpenQuestion $openQuestion, array $options = [])
    {
        $serialized = [
            'contentType' => 'text',
            'maxLength' => $openQuestion->getAnswerMaxLength(),
        ];

        if (in_array(Transfer::INCLUDE_SOLUTIONS, $options)) {
            $serialized['solutions'] = [];
        }

        return $serialized;
    }
1097361
public static function fromJson($json, int $options = 0)
    {
        $result = json_decode($json, true, 512, $options);
        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new JsonDecodeException(json_last_error_msg());
        }

        return new static(\is_array($result) ? $result : []);
    }
489054
public static final function jsonDecode($data, array $options = null): array
    {
        $encoder = new JsonEncoder($options);

        return [$encoder->decode($data), $encoder->hasError()
            ? new EncoderException('JSON Error: '. $encoder->getError()) : null];
    }
327023
public function __toJson($options = JSON_PRETTY_PRINT, $depth = 512)
    {
        $depth = $depth == 0 ? 512 : $depth;

        return call_user_func_array('json_encode', [$this->getArrayCopy(), $options, $depth]);
    }
503250
public function json($value, array $options, $mode) {
        if ($mode === self::TO && $options['encode']) {
            return json_encode($value);
        }

        if ($mode === self::FROM && $options['decode']) {
            return json_decode($value, !$options['object']);
        }

        return $value;
    }
1362114
public function encode(array $options = [])
    {
        $options = array_replace(
            [
                'encodingOptions'       => $this->getOption('encodingOptions')
            ],
            $options
        );

        $json = json_encode($this->getContents(), $options['encodingOptions']);

        if ($json === false) {
            switch (json_last_error()) {
                case JSON_ERROR_DEPTH:
                    $msg = 'The maximum stack depth has been exceeded.';

                    break;
                case JSON_ERROR_STATE_MISMATCH:
                    $msg = 'Occurs with underflow or with the modes mismatch.';

                    break;
                case JSON_ERROR_CTRL_CHAR:
                    $msg = 'Control character error, possibly incorrectly encoded.';

                    break;
                case JSON_ERROR_UTF8:
                    $msg = 'alformed UTF-8 characters, possibly incorrectly encoded.';

                    break;
                default:
                    $msg = 'Unknown error';
            }

            throw EncodeException::create($this->getPath(), $msg);
        }

        return $json;
    }
1346938
public static function encodeJson($value, int $options = JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION): string
    {
        return \json_encode($value, $options);
    }
856897
private function deserializeTags(Item $question, array $tags = [], array $options = [])
    {
        if (in_array(Transfer::PERSIST_TAG, $options)) {
            $user = null;

            if ($this->tokenStorage->getToken() && $this->tokenStorage->getToken()->getUser() instanceof User) {
                $user = $this->tokenStorage->getToken()->getUser();
            }

            $event = new GenericDataEvent([
                'user' => $user,
                'tags' => $tags,
                'data' => [
                    [
                        'class' => Item::class,
                        'id' => $question->getUuid(),
                        'name' => $question->getTitle(),
                    ],
                ],
                'replace' => true,
            ]);

            $this->eventDispatcher->dispatch('claroline_tag_multiple_data', $event);
        }
    }
517822
public function getQuestionConfirm(
        $question,
        $default = 'y',
        array $options = array('y', 'n'),
        $required = true
    ) {
        return $this->getQuestion($question, $default, $options, $required);
    }