Class JsonSerDeser<T>

java.lang.Object
org.apache.hadoop.yarn.service.utils.JsonSerDeser<T>
Type Parameters:
T -
Direct Known Subclasses:
ApplicationReportSerDeser

public class JsonSerDeser<T> extends Object
Support for marshalling objects to and from JSON. This class is NOT thread safe; it constructs an object mapper as an instance field.
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonSerDeser(Class<T> classType)
    Create an instance bound to a specific type
    JsonSerDeser(Class<T> classType, com.fasterxml.jackson.databind.PropertyNamingStrategy namingStrategy)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    fromBytes(byte[] b)
    Deserialize from a byte array
    fromFile(File jsonFile)
    Convert from a JSON file
    fromInstance(T instance)
    clone by converting to JSON and back again.
    Convert from JSON
    fromResource(String resource)
    Convert from a JSON file
    Convert from an input stream, closing the stream afterwards.
    load(org.apache.hadoop.fs.FileSystem fs, org.apache.hadoop.fs.Path path)
    Load from a Hadoop filesystem
    void
    save(org.apache.hadoop.fs.FileSystem fs, org.apache.hadoop.fs.Path path, T instance, boolean overwrite)
    Save to a hadoop filesystem
    void
    save(T instance, File file)
    Save an instance to a file
    toJson(T instance)
    Convert an object to a JSON string

    Methods inherited from class java.lang.Object

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

    • JsonSerDeser

      public JsonSerDeser(Class<T> classType)
      Create an instance bound to a specific type
      Parameters:
      classType - class type
    • JsonSerDeser

      public JsonSerDeser(Class<T> classType, com.fasterxml.jackson.databind.PropertyNamingStrategy namingStrategy)
  • Method Details

    • fromJson

      public T fromJson(String json) throws IOException, com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.databind.JsonMappingException
      Convert from JSON
      Parameters:
      json - input
      Returns:
      the parsed JSON
      Throws:
      IOException - IO
      com.fasterxml.jackson.databind.JsonMappingException - failure to map from the JSON to this class
      com.fasterxml.jackson.core.JsonParseException
    • fromFile

      public T fromFile(File jsonFile) throws IOException, com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.databind.JsonMappingException
      Convert from a JSON file
      Parameters:
      jsonFile - input file
      Returns:
      the parsed JSON
      Throws:
      IOException - IO problems
      com.fasterxml.jackson.databind.JsonMappingException - failure to map from the JSON to this class
      com.fasterxml.jackson.core.JsonParseException
    • fromResource

      public T fromResource(String resource) throws IOException, com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.databind.JsonMappingException
      Convert from a JSON file
      Parameters:
      resource - input file
      Returns:
      the parsed JSON
      Throws:
      IOException - IO problems
      com.fasterxml.jackson.databind.JsonMappingException - failure to map from the JSON to this class
      com.fasterxml.jackson.core.JsonParseException
    • fromStream

      public T fromStream(InputStream stream) throws IOException
      Convert from an input stream, closing the stream afterwards.
      Parameters:
      stream -
      Returns:
      the parsed JSON
      Throws:
      IOException - IO problems
    • fromInstance

      public T fromInstance(T instance) throws IOException
      clone by converting to JSON and back again. This is much less efficient than any Java clone process.
      Parameters:
      instance - instance to duplicate
      Returns:
      a new instance
      Throws:
      IOException - problems.
    • fromBytes

      public T fromBytes(byte[] b) throws IOException
      Deserialize from a byte array
      Parameters:
      b -
      Returns:
      the deserialized value
      Throws:
      IOException - parse problems
    • load

      public T load(org.apache.hadoop.fs.FileSystem fs, org.apache.hadoop.fs.Path path) throws IOException
      Load from a Hadoop filesystem
      Parameters:
      fs - filesystem
      path - path
      Returns:
      a loaded CD
      Throws:
      IOException - IO problems
      com.fasterxml.jackson.core.JsonParseException - parse problems
      com.fasterxml.jackson.databind.JsonMappingException - O/J mapping problems
    • save

      public void save(org.apache.hadoop.fs.FileSystem fs, org.apache.hadoop.fs.Path path, T instance, boolean overwrite) throws IOException
      Save to a hadoop filesystem
      Parameters:
      fs - filesystem
      path - path
      instance - instance to save
      overwrite - should any existing file be overwritten
      Throws:
      IOException - IO exception
    • save

      public void save(T instance, File file) throws IOException
      Save an instance to a file
      Parameters:
      instance - instance to save
      file - file
      Throws:
      IOException
    • toJson

      public String toJson(T instance) throws com.fasterxml.jackson.core.JsonProcessingException
      Convert an object to a JSON string
      Parameters:
      instance - instance to convert
      Returns:
      a JSON string description
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - parse problems