Class Common

java.lang.Object
com.google.genai.Common

@InternalApi public final class Common extends Object
Common utility methods for the GenAI SDK.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A class that holds the path, body, and http options of an API request.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    formatMap(String template, com.fasterxml.jackson.databind.JsonNode data)
     
    static @Nullable Object
    getValueByPath(com.fasterxml.jackson.databind.JsonNode object, String[] keys)
    Gets the value of an object by a path.
    static @Nullable Object
    getValueByPath(com.fasterxml.jackson.databind.JsonNode object, 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 boolean
     
    static void
    moveValueByPath(com.fasterxml.jackson.databind.JsonNode data, Map<String,String> paths)
    Moves values from source paths to destination paths.
    static void
    moveValueRecursive(com.fasterxml.jackson.databind.JsonNode data, String[] sourceKeys, String[] destKeys, int keyIdx, Set<String> excludeKeys)
    Recursively moves values from source path to destination path.
    static void
    setValueByPath(com.fasterxml.jackson.databind.node.ObjectNode jsonObject, String[] path, Object value)
    Sets the value of an object by a path.
    static String
    Converts a snake_case string to camelCase.
    static String
    urlEncode(com.fasterxml.jackson.databind.node.ObjectNode paramsNode)
    Converts a Jackson ObjectNode into a URL-encoded query string.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • setValueByPath

      public static void setValueByPath(com.fasterxml.jackson.databind.node.ObjectNode jsonObject, 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

      public static @Nullable Object getValueByPath(com.fasterxml.jackson.databind.JsonNode object, 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

      public static @Nullable Object getValueByPath(com.fasterxml.jackson.databind.JsonNode object, 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'

    • formatMap

      public static String formatMap(String template, com.fasterxml.jackson.databind.JsonNode data)
    • isZero

      public static boolean isZero(Object obj)
    • urlEncode

      public static String urlEncode(com.fasterxml.jackson.databind.node.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

      public static String snakeToCamel(String str)
      Converts a snake_case string to camelCase.
    • moveValueByPath

      public static void moveValueByPath(com.fasterxml.jackson.databind.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

      public static void moveValueRecursive(com.fasterxml.jackson.databind.JsonNode data, String[] sourceKeys, String[] destKeys, int keyIdx, Set<String> excludeKeys)
      Recursively moves values from source path to destination path.
      Parameters:
      data - The current node being processed
      sourceKeys - The source path keys
      destKeys - The destination path keys
      keyIdx - The current index in the key arrays
      excludeKeys - Keys to exclude when processing wildcards