InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in file - Laraval

Viewed 820

When returning Std class type object, the error is showing. I'm using PHP 7 and the Laravel 7.0

The error is returning from the setData function of JsonResponse.php (Illuminate\Http) the setData function using DEFAULT_ENCODING_OPTIONS = 15. this is the reason for that issue

enter image description here

I have no way to fix this issue, but i tried to change JsonResponse setData method $this->encodingOptions into JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE.

Now the error resolved. but I need another way to fix this.

public function setData($data = [])
{
    $this->original = $data;

    if ($data instanceof Jsonable) {
        $this->data = $data->toJson($this->encodingOptions);
        //$this->data = json_encode($data->jsonSerialize(), JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE);  // LINE NO: 01

    } elseif ($data instanceof JsonSerializable) {

        $this->data = json_encode($data->jsonSerialize(), $this->encodingOptions);
        //$this->data = json_encode($data->jsonSerialize(), $this->encodingOptions);  // LINE NO: 02

    } elseif ($data instanceof Arrayable) {

        $this->data = json_encode($data->toArray(), $this->encodingOptions);
    } else {
        $this->data = json_encode($data, $this->encodingOptions);
        //$this->data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE);  // LINE NO: 03

    }

    if (!$this->hasValidJson(json_last_error())) {
        throw new InvalidArgumentException(json_last_error_msg());
    }

    return $this->update();
}

Uncomment line no 1,2,3 error resolved ! I need another way to fix this. because, after run, composer install that change dismissing... this is part of my retrieving json

 {  "cart": [
        {
            "id": "a9756db0d79a11ea96c7cf86f7c6e0ee",
            "book_id": "81180660d2ed11eaa5299b806f9af290",
            "user_customer_user_id": "437a2b60d23611ea942efb15df219288",
            "status": "ACTIVE",
            "book": {
                "id": "81180660d2ed11eaa5299b806f9af290",
                "user_admin_user_id": "9c80e8006d5b11e9bb9835ecbe2a229e",
                "publisher_id": "7b0d3ac0d6ec11ea8cb15deb159804b0",
                "printed_price": 319,
                "purchase_link": "no",
                "description": "no",
                "pages": -1,
                "rating_value": 0,
                "file_type": "e-pub",
                "revenue_split_type": "OP",
                "book_authors": [
                    {
                        "user_id": "a6d99c206d5b11e9af1a4bbf4f149b08",
                        "reference_no": "XN87397509",
                        "description": null,
                        "search_tags": null,
                        "bank_name": null,
                        "account_number": null,
                        "user": {
                            "id": "a6d99c206d5b11e9af1a4bbf4f149b08",
                            "name": "Anton Dias",
                            "email": "Anton Dias@email.com",
                            "country_code": null,
                            "contact_number": null,
                            "status": "ACTIVE"
                        }
                    },
                    {
                        "user_id": "aa45c0206d5b11e9941ae50cf510f0f3",
                        "reference_no": "EB26705547",
                        "description": null,
                        "search_tags": null,
                        "bank_name": null,
                        "account_number": null,
                        "user": {
                            "id": "aa45c0206d5b11e9941ae50cf510f0f3",
                            "name": "K.G.Karunathilake",
                            "email": "K.G.Karunathilake@email.com",
                            "country_code": null,
                            "contact_number": null,
                            "status": "ACTIVE"
                        }
                    }
                ],
                "book_images": [
                    {
                        "book_id": "81180660d2ed11eaa5299b806f9af290",
                        "type": "LIST",
                        "size": 0.67
                    },
                    {
                        "book_id": "81180660d2ed11eaa5299b806f9af290",
                        "type": "PROFILE",
                        "size": 0.67
                    }
                ],
                "promotions": []
            }
        }
]
}

0 Answers
Related