public static function parse($document)
    {
        if (function_exists('mb_detect_encoding') &&
            (false === mb_detect_encoding($document, 'UTF-8', true))) {
            throw new JsonLdException(
                JsonLdException::LOADING_DOCUMENT_FAILED,
                'The JSON-LD document does not appear to be valid UTF-8.'
            );
        }

        $data = json_decode($document, false, 512);

        switch (json_last_error()) {
            case JSON_ERROR_NONE:
                break;  // no error
            case JSON_ERROR_DEPTH:
                throw new JsonLdException(
                    JsonLdException::LOADING_DOCUMENT_FAILED,
                    'The maximum stack depth has been exceeded.'
                );
            case JSON_ERROR_STATE_MISMATCH:
                throw new JsonLdException(
                    JsonLdException::LOADING_DOCUMENT_FAILED,
                    'Invalid or malformed JSON.'
                );
            case JSON_ERROR_CTRL_CHAR:
                throw new JsonLdException(
                    JsonLdException::LOADING_DOCUMENT_FAILED,
                    'Control character error (possibly incorrectly encoded).'
                );
            case JSON_ERROR_SYNTAX:
                throw new JsonLdException(
                    JsonLdException::LOADING_DOCUMENT_FAILED,
                    'Syntax error, malformed JSON.'
                );
            case JSON_ERROR_UTF8:
                throw new JsonLdException(
                    JsonLdException::LOADING_DOCUMENT_FAILED,
                    'Malformed UTF-8 characters (possibly incorrectly encoded).'
                );
            default:
                throw new JsonLdException(
                    JsonLdException::LOADING_DOCUMENT_FAILED,
                    'Unknown error while parsing JSON.'
                );
        }

        return (empty($data)) ? null : $data;
    }