<commit_before><?php

namespace PhpCoveralls\Bundle\CoverallsBundle\Entity;

/**
 * Data for Coveralls API.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class Coveralls implements ArrayConvertable
{
    /**
     * String expression (convert to json).
     *
     * @return string
     */
    public function __toString()
    {
        return json_encode($this->toArray());
    }
}<commit_msg>JsonFile::__toString() - detect json_encode failure (#263)<commit_after><?php

namespace PhpCoveralls\Bundle\CoverallsBundle\Entity;

/**
 * Data for Coveralls API.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class Coveralls implements ArrayConvertable
{
    /**
     * String expression (convert to json).
     *
     * @return string
     */
    public function __toString()
    {
        $result = json_encode($this->toArray());

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \UnexpectedValueException(sprintf(
                'Can not encode to JSON, error: "%s". If you have non-UTF8 chars, consider migration to UTF8.',
                json_last_error_msg()
            ));
        }

        return $result;
    }
}
{'fraction_non_alphanumeric': 0.12521891418563924, 'fraction_numerical': 0.0043782837127845885, 'mean_word_length': 2.538699690402477, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1782157', 'n_tokens_mistral': 338, 'n_tokens_neox': 325, 'n_words': 83}