-
Notifications
You must be signed in to change notification settings - Fork 10
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
Problem converting data that has functions inside #7
Comments
Hi @rvalitov and thank you for opening this issue. The fact that you get {
"data": {
"type": "JS_FUNCTION_CALL",
"function_name": "s",
"function_args": ["London"]
}
} Besides this, I think the $jsObjectString = '{data: s("London")}';
$options = [
'js_function_callback' => function ($name, $args = []) {
if ($name === "s" && isset($args[0])) {
return some_php_translation_func($args[0]);
}
return NULL;
}
];
$json = \OviDigital\JsObjectToJson\JsConverter::convertToJson($jsObjectString, $options); It would take me about 1-2 weeks or so to implement this, as I am currently busy with other stuff. A quick workaround for you might be to something like below to fix the output from $incorrect_json = '{"data":"s(""London")}';
$fixed_json = preg_replace_callback('/"s\(""([^"]+)\)/', function ($matches) {
return '{"type": "JS_FUNCTION_CALL", "function_name": "s", "function_args": [' . $matches[1] .']}';
}); I didn't test any of the code I wrote here, so there might be errors |
Thank you for a prompt response! I'm trying to figure this out with regex. But it becomes more complicated, because the strings can have backslashes inside for escaping and should be processed properly. Besides, there are also cases with formulas, such as
or
I don't think such things should be solved by your library, but I think such cases should be mentioned. Or at least detected in your processing code with throwing exceptions that some value in the JS is incorrect or not supported. Otherwise it becomes troublesome to debug such cases and errors. |
Hi! Thank you for you project!
I hope it can help me to parse data from a JS file.
I have a following example of JS data:
where
s
is a translation function used in JS code. I need to get an array or PHP object from that.Unfortunately, such code can't be processed with the
json_decode
function that you use under the hood. It saysSyntax Error
.If I call
convertToJson
I get:Double quote appears, which should not be there. Do you think if there's any workaround for that?
The text was updated successfully, but these errors were encountered: