Skip to content

Commit

Permalink
Replaces index test with generic exception handling that returns null…
Browse files Browse the repository at this point in the history
…. Adds more tests, for invalid and longer indexes.
renatoliveira committed May 16, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 0105006 commit 6222559
Showing 2 changed files with 35 additions and 29 deletions.
27 changes: 10 additions & 17 deletions force-app/main/default/classes/JSONPath.cls
Original file line number Diff line number Diff line change
@@ -8,7 +8,12 @@ public class JSONPath {
}

public Object get(String path) {
return this.get(path.removeStart('$'), this.data);
try {
Object o = this.get(path.removeStart('$'), this.data);
return o;
} catch (Exception e) {
return null;
}
}

private Object get(String path, Object data) {
@@ -39,7 +44,7 @@ public class JSONPath {
if (currentLevel.endsWith(']')) {
return this.get(
('[' +
currentLevel.reverse().substringBetween(']', '[') +
currentLevel.reverse().substringBetween(']', '[').reverse() +
'].' +
String.join(levels, '.')),
d.get(currentLevel.split('\\[').remove(0))
@@ -65,32 +70,20 @@ public class JSONPath {
this.get(String.join(levels, '.'), objectInstance)
);
}
System.debug(result);
return result;
}
return dataList;
}

Integer index = getIndex(currentLevel);
Integer index = Integer.valueOf(
currentLevel.substringBetween('[', ']')
);

return levels.size() == 0
? dataList.get(index)
: this.get(String.join(levels, '.'), dataList.get(index));
}

private Integer getIndex(String currentLevel) {
String indexStr = currentLevel.substringBetween('[', ']');

if (indexStr == null) {
String m = 'Invalid path.';
JSONPathException e = new JSONPathException(m);
e.setMessage(m);
throw e;
}

return Integer.valueOf(indexStr);
}

public class JSONPathException extends Exception {
}
}
37 changes: 25 additions & 12 deletions force-app/main/default/classes/JSONPathTest.cls
Original file line number Diff line number Diff line change
@@ -5,11 +5,7 @@ private class JSONPathTest {
JSONPath jpObject = new JSONPath(
'{"name":"John","company":{"name":"Company"}}'
);
System.assertEquals(
'John',
jpObject.get('$.name'),
'Incorrect data.'
);
System.assertEquals('John', jpObject.get('$.name'), 'Incorrect data.');
System.assertEquals(
'Company',
jpObject.get('$.company.name'),
@@ -18,11 +14,7 @@ private class JSONPathTest {
JSONPath jpList = new JSONPath(
'[{"name":"John","company":{"name":"Company"}}]'
);
System.assertEquals(
'John',
jpList.get('$[0].name'),
'Incorrect data.'
);
System.assertEquals('John', jpList.get('$[0].name'), 'Incorrect data.');
System.assert(
jpList.get('$[]') instanceof List<Object>,
'Should be a list.'
@@ -46,7 +38,9 @@ private class JSONPathTest {
JSONPath jpAttributeListFromObjectList = new JSONPath(
'[{"name":"John"},{"name":"Mary"}]'
);
List<Object> names = (List<Object>) jpAttributeListFromObjectList.get('$[].name');
List<Object> names = (List<Object>) jpAttributeListFromObjectList.get(
'$[].name'
);
System.assert(
names[0] == 'John' &&
names[1] == 'Mary',
@@ -55,11 +49,30 @@ private class JSONPathTest {
JSONPath jpAttributeListFromInnerObjectList = new JSONPath(
'{"people":[{"name":"John"},{"name":"Mary"}]}}'
);
names = (List<Object>) jpAttributeListFromInnerObjectList.get('$.people[].name');
names = (List<Object>) jpAttributeListFromInnerObjectList.get(
'$.people[].name'
);
System.assert(
names[0] == 'John' &&
names[1] == 'Mary',
'Should have returned a list of names.'
);
JSONPath invalidPath = new JSONPath(
'{"messages":["Hello, world!", "Goodbye, world!"]}'
);
System.assertEquals(
null,
invalidPath.get('$.messages[-1]'),
'Should have returned null for invalid path.'
);

JSONPath twoDigitsList = new JSONPath(
'{"integers":[0,1,2,3,4,5,6,7,8,9,10]}'
);
System.assertEquals(
10,
twoDigitsList.get('$.integers[10]'),
'Should have returned 10.'
);
}
}

0 comments on commit 6222559

Please sign in to comment.