Class JsonUtils

java.lang.Object
uk.co.evoco.webdriver.utils.JsonUtils

public final class JsonUtils
extends java.lang.Object
A simple class that helps serialize and deserialize JSON objects in various formats and inputs
  • Constructor Summary

    Constructors 
    Constructor Description
    JsonUtils()  
  • Method Summary

    Modifier and Type Method Description
    static <T> T fromFile​(java.io.File file, com.fasterxml.jackson.core.type.TypeReference<T> type)  
    static <T> T fromFile​(java.io.File file, java.lang.Class<T> type)
    Deserialize a JSON from a File to a given class type
    static <T> T fromFile​(java.io.InputStream jsonStream, java.lang.Class<T> type)
    Deserialize a JSON from a File to a given class type
    static <T> T fromFile​(java.lang.String filePath, java.lang.Class<T> type)
    Deserialize a JSON from a File to a given class type
    static <T> T fromString​(java.lang.String jsonString, com.fasterxml.jackson.core.type.TypeReference<T> type)
    Deserialize a JSON from a String to a given class type
    static <T> T fromString​(java.lang.String jsonString, java.lang.Class<T> type)
    Deserialize a JSON from a String to a given class type
    static java.lang.String toString​(java.lang.Object object)
    Serializes a given object instance into a JSON and returns it as a String

    Methods inherited from class java.lang.Object

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

  • Method Details

    • fromString

      public static <T> T fromString​(java.lang.String jsonString, com.fasterxml.jackson.core.type.TypeReference<T> type) throws com.fasterxml.jackson.core.JsonProcessingException
      Deserialize a JSON from a String to a given class type
      Type Parameters:
      T - generic type
      Parameters:
      jsonString - json string
      type - target class type or collection
      Returns:
      generic class for mapper
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - if JSON cannot be processed
    • fromString

      public static <T> T fromString​(java.lang.String jsonString, java.lang.Class<T> type) throws com.fasterxml.jackson.core.JsonProcessingException
      Deserialize a JSON from a String to a given class type
      Type Parameters:
      T - generic type
      Parameters:
      jsonString - json string
      type - target class type or collection
      Returns:
      generic class for mapper
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - if JSON cannot be processed
    • fromFile

      public static <T> T fromFile​(java.io.File file, java.lang.Class<T> type) throws java.io.IOException
      Deserialize a JSON from a File to a given class type
      Type Parameters:
      T - generic type
      Parameters:
      file - json file
      type - target class type or collection
      Returns:
      generic class for mapper
      Throws:
      java.io.IOException - if file cannot be found
    • fromFile

      public static <T> T fromFile​(java.io.File file, com.fasterxml.jackson.core.type.TypeReference<T> type) throws java.io.IOException
      Type Parameters:
      T - generic type
      Parameters:
      file - json file
      type - target class type of collection
      Returns:
      generic class for mapper
      Throws:
      java.io.IOException - if file cannot be found
    • fromFile

      public static <T> T fromFile​(java.io.InputStream jsonStream, java.lang.Class<T> type) throws java.io.IOException
      Deserialize a JSON from a File to a given class type
      Type Parameters:
      T - generic type
      Parameters:
      jsonStream - json stream
      type - target class type or collection
      Returns:
      generic class for mapper
      Throws:
      java.io.IOException - if file cannot be found
    • fromFile

      public static <T> T fromFile​(java.lang.String filePath, java.lang.Class<T> type) throws java.io.IOException
      Deserialize a JSON from a File to a given class type
      Type Parameters:
      T - generic type
      Parameters:
      filePath - json file path
      type - target class type or collection
      Returns:
      generic class for mapper
      Throws:
      java.io.IOException - if file cannot be found
    • toString

      public static java.lang.String toString​(java.lang.Object object) throws com.fasterxml.jackson.core.JsonProcessingException
      Serializes a given object instance into a JSON and returns it as a String
      Parameters:
      object - json object
      Returns:
      String that represents JSON
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - if JSON cannot be parsed