Skip to content

Commit

Permalink
Add support paths with numbers
Browse files Browse the repository at this point in the history
We have discovered that some specific paths are not properly build.

If your JSON content is something like:

```json
{
  "foo": {
       "bar2": "baz"
   }
}
```

The exported json path is `foo.["bar2"]`. This commit solves this issue.
  • Loading branch information
fcsonline authored and MaloPolese committed Dec 21, 2024
1 parent 00b0cb2 commit 3b2d0ca
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/json.path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ export function getPropertyPathWithQuotes(
return `[${propertyNameJson}]`;
}

export const nonQuotedCharacterRanges = ['A-Z', 'À-Ö', 'Ø-ö', 'ø-ÿ'];
export const nonQuotedCharacterRanges = ['A-Z', 'À-Ö', 'Ø-ö', 'ø-ÿ', '0-9'];

export function propertyRequiresQuotes(propertyName: jsonc.Segment): boolean {
// If the property start with a numbrer we are forced to require quotes
if (propertyName.toString().match(/^\d/)) {
return true;
}

// https://stackoverflow.com/questions/20690499/concrete-javascript-regular-expression-for-accented-characters-diacritics/26900132#26900132
const allowedCharRanges = nonQuotedCharacterRanges.join('');
const allowedCharactersWithoutEscaping = new RegExp(
Expand Down

0 comments on commit 3b2d0ca

Please sign in to comment.