Class Common
-
- All Implemented Interfaces:
@InternalApi() public final class CommonCommon utility methods for the GenAI SDK.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classCommon.BuiltRequestA class that holds the path, body, and http options of an API request.
-
Method Summary
Modifier and Type Method Description static voidsetValueByPath(ObjectNode jsonObject, Array<String> path, Object value)Sets the value of an object by a path. static ObjectgetValueByPath(JsonNode object, Array<String> keys)Gets the value of an object by a path. static ObjectgetValueByPath(JsonNode object, Array<String> keys, @Nullable() Object defaultValue)Gets the value of an object by a path, returning a default value if the path does not exist. static StringformatMap(String template, JsonNode data)static booleanisZero(Object obj)static StringurlEncode(ObjectNode paramsNode)Converts a Jackson ObjectNode into a URL-encoded query string. static StringsnakeToCamel(String str)Converts a snake_case string to camelCase. static voidmoveValueByPath(JsonNode data, Map<String, String> paths)Moves values from source paths to destination paths. static voidmoveValueRecursive(JsonNode data, Array<String> sourceKeys, Array<String> destKeys, int keyIdx, Set<String> excludeKeys)Recursively moves values from source path to destination path. -
-
Method Detail
-
setValueByPath
static void setValueByPath(ObjectNode jsonObject, Array<String> path, Object value)
Sets the value of an object by a path.
setValueByPath({}, ['a', 'b'], v) -> {'a': {'b': v}}
setValueByPath({}, ['a', 'b[]', c], [v1, v2]) -> {'a': {'b': [{'c': v1}, {'c': v2}]}}
setValueByPath({'a': {'b':[{'c': v1}, {'c': v2}]}}, ['a', 'b[]', 'd'], v3) -> {'a': {'b': [{'c': v1, 'd': v3}, {'c': v2,'d': v3}]}}
-
getValueByPath
@Nullable() static Object getValueByPath(JsonNode object, Array<String> keys)
Gets the value of an object by a path.
getValueByPath({'a': {'b': v}}, ['a', 'b']) -> v
getValueByPath({'a': {'b': [{'c': v1}, {'c': v2}]}}, ['a', 'b[]', 'c']) -> [v1, v2]
-
getValueByPath
@Nullable() static Object getValueByPath(JsonNode object, Array<String> keys, @Nullable() Object defaultValue)
Gets the value of an object by a path, returning a default value if the path does not exist.
getValueByPath({'a': {'b': v}}, ['a', 'b'], 'default') -> v
getValueByPath({'a': {'c': v}}, ['a', 'b'], 'default') -> 'default'
-
urlEncode
static String urlEncode(ObjectNode paramsNode)
Converts a Jackson ObjectNode into a URL-encoded query string. Assumes values are simple types (text, number, boolean, or null) that can be represented as a single string.
- Parameters:
paramsNode- The ObjectNode containing the parameters to encode.- Returns:
A URL-encoded string (e.g., "key1=value1&key2=value2").
-
snakeToCamel
static String snakeToCamel(String str)
Converts a snake_case string to camelCase.
-
moveValueByPath
static void moveValueByPath(JsonNode data, Map<String, String> paths)
Moves values from source paths to destination paths.
Example: moveValueByPath( {'requests': [{'content': v1}, {'content': v2}]}, {'requests[].*': 'requests[].request.*'} ) -> {'requests': [{'request': {'content': v1}}, {'request': {'content': v2}}]}
-
moveValueRecursive
static void moveValueRecursive(JsonNode data, Array<String> sourceKeys, Array<String> destKeys, int keyIdx, Set<String> excludeKeys)
Recursively moves values from source path to destination path.
- Parameters:
data- The current node being processedsourceKeys- The source path keysdestKeys- The destination path keyskeyIdx- The current index in the key arraysexcludeKeys- Keys to exclude when processing wildcards
-
-
-
-