Created
August 15, 2019 18:39
-
-
Save gbutt/842f2adf09a6d961656da08545a41985 to your computer and use it in GitHub Desktop.
JsonReader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsonReader { | |
public Map<String, Object> jsonMap {get; set;} | |
public JsonReader(Map<String, Object> jsonMap) { | |
this.jsonMap = jsonMap; | |
} | |
public String getString(String path) { | |
return (String)getFieldAtPath(path); | |
} | |
public Double getDouble(String path) { | |
return (Double)getFieldAtPath(path); | |
} | |
public Object getFieldAtPath(String path) { | |
String[] pathParts = path.split('\\.'); | |
if (pathParts.size() == 0) { | |
return jsonMap.get(path); | |
} | |
Object jsonTemp = jsonMap; | |
for (Integer i = 0; i < pathParts.size() - 1; i++) { | |
String part = pathParts[i]; | |
if (part.endsWith(']')) { | |
jsonTemp = getFieldWithIndex(jsonTemp, part); | |
} else { | |
jsonTemp = ((Map<String, Object>)jsonTemp).get(part); | |
} | |
} | |
String finalPath = pathParts[pathParts.size() - 1]; | |
if (finalPath.endsWith(']')) { | |
return getFieldWithIndex(jsonTemp, finalPath); | |
} else { | |
return ((Map<String, Object>)jsonTemp).get(finalPath); | |
} | |
} | |
private Object getFieldWithIndex(Object jsonTemp, String pathPart) { | |
String[] partWithIndices = pathPart.split('\\['); | |
String part = partWithIndices.remove(0); | |
Object tempResult = ((Map<String, Object>)jsonTemp).get(part); | |
for (String indexStr : partWithIndices) { | |
Integer idx = Integer.valueOf(indexStr.removeEnd(']')); | |
tempResult = ((Object[])tempResult).get(idx); | |
} | |
return tempResult; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsTest(isParallel =true) | |
public class JsonReaderTest { | |
@IsTest | |
static void it_should_get_field_as_string() { | |
Map<String, Object> jsonMap = new Map<String, Object> { | |
'id' => '123', | |
'data' => new Map<String, Object> { | |
'name' => 'steve', | |
'address' => new Map<String, Object> { | |
'line1' => '123 Sesame St' | |
} | |
} | |
}; | |
JsonReader jsonReader = new JsonReader(jsonMap); | |
System.assertEquals('123', jsonReader.getString('id')); | |
System.assertEquals('steve', jsonReader.getString('data.name')); | |
System.assertEquals('123 Sesame St', jsonReader.getString('data.address.line1')); | |
System.assertEquals(null, jsonReader.getString('data.address.line2')); | |
} | |
@IsTest | |
static void it_should_get_field_as_double() { | |
Map<String, Object> jsonMap = new Map<String, Object> { | |
'id' => 123, | |
'data' => new Map<String, Object> { | |
'name' => 45.6, | |
'address' => new Map<String, Object> { | |
'line1' => null | |
} | |
} | |
}; | |
JsonReader jsonReader = new JsonReader(jsonMap); | |
System.assertEquals(123, jsonReader.getDouble('id')); | |
System.assertEquals(45.6, jsonReader.getDouble('data.name')); | |
System.assertEquals(null, jsonReader.getDouble('data.address.line1')); | |
} | |
@IsTest | |
static void it_should_get_array_field_as_string() { | |
Map<String, Object> jsonMap = new Map<String, Object> { | |
'test1' => new Object[] { | |
1, 2, 3 | |
}, | |
'test2' => new Object[] { | |
new Object[] { | |
1, 2, 3 | |
}, | |
new Object[] { | |
4, 5, 6 | |
} | |
}, | |
'test3' => new Object[] { | |
new Map<String, Object> { | |
'name' => 'seadog' | |
} | |
} | |
}; | |
JsonReader jsonReader = new JsonReader(jsonMap); | |
System.assertEquals(1, jsonReader.getDouble('test1[0]')); | |
System.assertEquals(6, jsonReader.getDouble('test2[1][2]')); | |
System.assertEquals('seadog', jsonReader.getString('test3[0].name')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment