Skip to content

Conversation

@shaedrich
Copy link
Contributor

Counterpart of #56697, so to speak

Examples

Arr::fromJson('}', false, 512, 0); // ❌ Throws 'Please provide a valid JSON: Syntax error'
Arr::fromJson("[\"\u{D800}\"]", false, 512, 0); // ❌ Throws 'Please provide a valid JSON: Malformed UTF-8 characters, possibly incorrectly encoded'
Arr::fromJson('[[[[]]]]', false, 2, 0); // ❌ Throws 'Please provide a valid JSON: Maximum stack depth exceeded'
Arr::fromJson('null', false, 512, 0); // ❌ Throws 'The given JSON cannot be parsed into an array: null given instead'
Arr::fromJson('42', false, 512, 0); // ❌ Throws 'The given JSON cannot be parsed into an array: int given instead'
Arr::fromJson('42.1', false, 512, 0); // ❌ Throws 'The given JSON cannot be parsed into an array: float given instead'
Arr::fromJson('"foo"', false, 512, 0); // ❌ Throws 'The given JSON cannot be parsed into an array: string given instead'
Arr::fromJson('true', false, 512, 0); // ❌ Throws 'The given JSON cannot be parsed into an array: bool given instead'
Arr::fromJson('{"foo": "bar"}', false, 512, 0); // ❌ Throws 'The given JSON cannot be parsed into an array: stdClass given instead'

Arr::fromJson('[1, 2, 3]', false, 512, 0); // ✔️ [1, 2, 3]
Arr::fromJson('[1, 2, 3]', null, 512, JSON_OBJECT_AS_ARRAY); // ✔️ [1, 2, 3]
Arr::fromJson('[1, 2, 3]', true, 512, 0); // ✔️ [1, 2, 3]
Arr::fromJson('{"foo": "bar"}', null, 512, JSON_OBJECT_AS_ARRAY); // ✔️ ["foo" => "bar"]
Arr::fromJson('{"foo": "bar"}', true, 512, 0); // ✔️ ["foo" => "bar"]

@shaedrich shaedrich changed the title Add fromJson() to Arr helper [12.x] Add fromJson() to Arr helper Aug 21, 2025
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants