Created
March 1, 2019 20:22
-
-
Save Szandor72/47f6ab504d4dfba8254de684143acfca to your computer and use it in GitHub Desktop.
This file contains 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 JSONObject { | |
public class JSONObjectException extends Exception {} | |
public static String NAMESPACEPREFIX { | |
get { | |
if (namespacePrefix == null) { | |
namespacePrefix = JSONObject.class.getName().substringBefore('.JSONObject'); | |
namespacePrefix = namespacePrefix.length() > 0 && namespacePrefix != 'JSONObject' ? namespacePrefix + '__' : ''; | |
} | |
return namespacePrefix; | |
} | |
private set; | |
} | |
@testvisible | |
private Map<String,Object> propertyMap = new Map<String,Object>(); | |
@testvisible | |
private class PropertyNameToValueBridge { | |
private String propertyName; | |
private Map<String,Object> propertyMap; | |
private List<String> propertyNameParts = new List<String>(); | |
private Integer deepness; | |
public PropertyNameToValueBridge(String propertyName, Map<String,Object> propertyMap) { | |
this.propertyName = propertyName; | |
this.propertyMap = propertyMap; | |
parsePropertyName(); | |
} | |
public void setValue(Object value) { | |
switch on deepness { | |
when 1 { | |
propertyMap.put(propertyName, value); | |
} | |
when 2 { | |
try { | |
Map<String,Object> lookupObject = (Map<String,Object>)propertyMap.get(propertyNameParts[0]); | |
lookupObject.put(propertyNameParts[1], value); | |
propertyMap.put(propertyNameParts[0], lookupObject); | |
} catch (exception e) { | |
throw new JSONObjectException('Make sure to only set related properties if the relation is present in the original. ' +e.getMessage()); | |
} | |
} | |
} | |
} | |
public Object getValue() { | |
switch on deepness { | |
when 1 { | |
return propertyMap.get(propertyName); | |
} | |
when 2 { | |
try { | |
Map<String,Object> nextObject = (Map<String,Object>)propertyMap.get(propertyNameParts[0]); | |
return nextObject.get(propertyNameParts[1]); | |
} catch (exception e) { | |
throw new JSONObjectException('Make sure to request only present relations. ' +e.getMessage()); | |
} | |
} | |
} | |
return null; | |
} | |
private void parsePropertyName() { | |
propertyNameParts = propertyName.split('\\.'); | |
this.deepness = propertyNameParts.size(); | |
if (deepness > 2) { | |
throw new JSONObjectException('Traversion is only supported for a deepnees of 1.'); | |
} | |
} | |
} | |
public JSONObject(String singleObjectJSON) { | |
try { | |
JSON.deserializeUntyped(singleObjectJSON); | |
} catch (exception e) { | |
throw new JSONOBjectException('This is not a valid JSON String.'); | |
} | |
try { | |
propertyMap = (Map<String,Object>)JSON.deserializeUntyped(singleObjectJSON); | |
} catch (exception e) { | |
throw new JSONOBjectException('A single JSON Object is expected. The JSON String contains a list of Objects.'); | |
} | |
} | |
public JSONObject(Object anyObject) { | |
this(JSON.serialize(anyObject)); | |
} | |
public Boolean hasProperty(String propertyName) { | |
return propertyMap.get(propertyName) != null ? true : false; | |
} | |
public Object get(String propertyName) { | |
return new PropertyNameToValueBridge(propertyName, propertyMap).getValue(); | |
} | |
public void set(String propertyName, Object value) { | |
new PropertyNameToValueBridge(propertyName, propertyMap).setValue(value); | |
} | |
public void set(String propertyName, Object value, Boolean appendNamespace) { | |
if(appendNamespace){ | |
propertyName = NAMESPACEPREFIX + propertyName; | |
} | |
new PropertyNameToValueBridge(propertyName, propertyMap).setValue(value); | |
} | |
public Object deserialize(System.Type apexType) { | |
return JSON.deserializeStrict(toJSON(), apexType); | |
} | |
@testVisible | |
private String toJSON() { | |
return JSON.serialize(propertyMap); | |
} | |
@testVisible | |
private static Boolean hasProperty(String json, String propertyName) { | |
return json.contains(propertyName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment