Class JsonUtils


  • public final class JsonUtils
    extends java.lang.Object
    Utility methods for interacting with JSON.
    Since:
    3.0.0
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected JsonUtils()
      Protected constructor for a utility class.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String joinArguments​(java.util.List<java.lang.String> commandArgs)
      Given an ordered list of command line arguments join them back together as a space delimited String where each argument is wrapped in '.
      static java.lang.String marshall​(java.lang.Object value)
      Convert a Java object to a JSON string.
      static java.util.List<java.lang.String> splitArguments​(java.lang.String commandArgs)
      Given a flat string of command line arguments this method will attempt to tokenize the string and split it for use in DTOs.
      static <T extends java.util.Collection>
      T
      unmarshall​(java.lang.String source, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
      Convert a JSON string of a collection back to a Java object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JsonUtils

        protected JsonUtils()
        Protected constructor for a utility class.
    • Method Detail

      • marshall

        public static java.lang.String marshall​(java.lang.Object value)
                                         throws GenieException
        Convert a Java object to a JSON string.
        Parameters:
        value - The Java object to marshall
        Returns:
        The JSON string
        Throws:
        GenieException - For any marshalling exception
      • unmarshall

        public static <T extends java.util.Collection> T unmarshall​(java.lang.String source,
                                                                    com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
                                                             throws GenieException
        Convert a JSON string of a collection back to a Java object.
        Type Parameters:
        T - The type of the collection ie Set of String
        Parameters:
        source - The JSON string
        typeReference - The type reference of the collection to unmarshall to
        Returns:
        The Java object
        Throws:
        GenieException - For any exception during unmarshalling
      • splitArguments

        @Nonnull
        public static java.util.List<java.lang.String> splitArguments​(java.lang.String commandArgs)
        Given a flat string of command line arguments this method will attempt to tokenize the string and split it for use in DTOs. The split will occur on whitespace (tab, space, new line, carriage return) while respecting single quotes to keep those elements together.

        Example: "/bin/bash -xc 'echo "hello" world!'" results in ["/bin/bash", "-xc", "echo "hello" world!"]

        Parameters:
        commandArgs - The original string representation of the command arguments
        Returns:
        An ordered list of arguments
      • joinArguments

        @Nullable
        public static java.lang.String joinArguments​(java.util.List<java.lang.String> commandArgs)
        Given an ordered list of command line arguments join them back together as a space delimited String where each argument is wrapped in '.
        Parameters:
        commandArgs - The command arguments to join back together
        Returns:
        The command arguments joined together or null if there weren't any arguments