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

Problem converting data that has functions inside #7

Open
rvalitov opened this issue May 18, 2022 · 2 comments
Open

Problem converting data that has functions inside #7

rvalitov opened this issue May 18, 2022 · 2 comments

Comments

@rvalitov
Copy link

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:

var some_data = {data: s("London")};

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 says Syntax Error.
If I call convertToJson I get:

{"data":"s(""London")}

Double quote appears, which should not be there. Do you think if there's any workaround for that?

@ovidyou
Copy link
Member

ovidyou commented May 18, 2022

Hi @rvalitov and thank you for opening this issue.

The fact that you get {"data":"s(""London")} when using convertToJson is because this case wasn't considered until now. I am not sure of the best way to represent this in JSON. Maybe something like below would make sense? What do you think?

{
  "data": {
    "type": "JS_FUNCTION_CALL",
    "function_name": "s",
    "function_args": ["London"]
  }
}

Besides this, I think the convertToJson method should be refactored so that it also accepts an $options argument, where someone could provide a PHP function callback for dealing with these JS function calls, so that you could for instance do following:

$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 convertToJson:

$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

@rvalitov
Copy link
Author

rvalitov commented May 18, 2022

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

!value

or

2+3

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.

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