From 6222559f20004338b11ea2ea951a089290fc1673 Mon Sep 17 00:00:00 2001 From: Renato Oliveira Date: Mon, 16 May 2022 09:50:18 -0300 Subject: [PATCH] Replaces index test with generic exception handling that returns null. Adds more tests, for invalid and longer indexes. --- force-app/main/default/classes/JSONPath.cls | 27 +++++--------- .../main/default/classes/JSONPathTest.cls | 37 +++++++++++++------ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/force-app/main/default/classes/JSONPath.cls b/force-app/main/default/classes/JSONPath.cls index 1c1fabd..c515d4f 100644 --- a/force-app/main/default/classes/JSONPath.cls +++ b/force-app/main/default/classes/JSONPath.cls @@ -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 { } } diff --git a/force-app/main/default/classes/JSONPathTest.cls b/force-app/main/default/classes/JSONPathTest.cls index 2561733..e293ee5 100644 --- a/force-app/main/default/classes/JSONPathTest.cls +++ b/force-app/main/default/classes/JSONPathTest.cls @@ -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, 'Should be a list.' @@ -46,7 +38,9 @@ private class JSONPathTest { JSONPath jpAttributeListFromObjectList = new JSONPath( '[{"name":"John"},{"name":"Mary"}]' ); - List names = (List) jpAttributeListFromObjectList.get('$[].name'); + List names = (List) 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) jpAttributeListFromInnerObjectList.get('$.people[].name'); + names = (List) 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.' + ); } }