Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect handling of arrays with gaps #12

Closed
KapitanOczywisty opened this issue Feb 17, 2022 · 4 comments
Closed

Incorrect handling of arrays with gaps #12

KapitanOczywisty opened this issue Feb 17, 2022 · 4 comments

Comments

@KapitanOczywisty
Copy link

Function cannot handle gaps in arrays

import json2php from "json2php";

console.log(json2php([1, , , , , 3]));

Results in:

array(1, , , , , 3)
// Fatal error: Cannot use empty array elements in arrays in /in/KKbIJ on line 3

https://3v4l.org/KKbIJ

Best would be treat arrays like objects.

@daniel-zahariev
Copy link
Owner

Hi @KapitanOczywisty, the library isn't intended to handle gaps because they're not part of JSON specification.

@KapitanOczywisty
Copy link
Author

@daniel-zahariev Beside producing invalid code without any exception, such arrays are correctly handled by JSON.stringify

JSON.stringify([1,,,2]); // '[1,null,null,2]'

@daniel-zahariev
Copy link
Owner

@KapitanOczywisty, [1,,,2] simply isn't valid JSON. You can test it for yourself.
If you're unhappy due to the lack of errors or exceptions feel free to fork, modify and publish your upgraded library.

@KapitanOczywisty
Copy link
Author

I know it's invalid, but You're providing JS object not JSON string as function argument.

And yes, I've already produced my own implementation, since I cannot afford script which results in invalid PHP without any warning and doesn't have any fail-safe for circular references.

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

No branches or pull requests

2 participants