Galbar / JsonPath-PHP

A JsonPath implementation in PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error with alpha-numeric property name

Lukazar opened this issue · comments

This is sort of related to #18

The issue is if I have a path that looks something like
$.504Plan the RE_* regexes within the JsonObject.php fail to match and throw an InvalidJsonPathException.

I've verified that allowing the RE_CHILD_NAME, RE_RECURSIVE_SELECTOR to start with [0-9a-zA-Z] works for my particular case (realizing of course this regex is probably too lacks). Would making such a change invalidate the intent of this JSON path library? Should that be an allowed use case?

Thanks in advance!

Hi, @Lukazar !

This is by design. The idea is to mimic (to some extent) the behavior of JS:

>> var a = {}
<< undefined
>> a.foo = 3
<< 3
>> a.123bar = 4
<< SyntaxError: identifier starts immediately after numeric literal
>> a['123bar'] = 4
<< 4
>> a.123bar
<< SyntaxError: identifier starts immediately after numeric literal
>> a['123bar']
<< 4

The intended way to access these kind of variable names is using the [] operator.
For this case it would be: $['504Plan'].

Let me know if you have further questions.

Cheers,
Galbar

@Galbar Oh! That makes perfect sense. Thank you for the quick response!