Package org.apache.hadoop.util
Class JsonSerialization<T>
java.lang.Object
org.apache.hadoop.util.JsonSerialization<T>
- Type Parameters:
T- Type to marshal.
Support for marshalling objects to and from JSON.
It constructs an object mapper as an instance field.
and synchronizes access to those methods
which use the mapper.
This class was extracted from
org.apache.hadoop.registry.client.binding.JsonSerDeser,
which is now a subclass of this class.-
Constructor Summary
ConstructorsConstructorDescriptionJsonSerialization(Class<T> classType, boolean failOnUnknownProperties, boolean pretty) Create an instance bound to a specific type. -
Method Summary
Modifier and TypeMethodDescriptionfromBytes(byte[] bytes) Deserialize from a byte array.fromInstance(T instance) clone by converting to JSON and back again.Convert from JSON.fromJsonStream(InputStream stream) Read from an input stream.fromResource(String resource) Convert from a JSON file.com.fasterxml.jackson.databind.ObjectMapperGet the mapper of this class.getName()Get the simple name of the class type to be marshalled.Load from a JSON text file.load(FileSystem fs, Path path) Load from a Hadoop filesystem.load(FileSystem fs, Path path, FileStatus status) Load from a Hadoop filesystem.static com.fasterxml.jackson.databind.ObjectReadervoidSave to a local file.voidsave(FileSystem fs, Path path, T instance, boolean overwrite) Save to a Hadoop filesystem.byte[]Convert JSON to bytes.Convert an instance to a JSON string.Convert an instance to a string form for output.voidwriteJsonAsBytes(T instance, OutputStream dataOutputStream) Write the JSON as bytes, then close the stream.static com.fasterxml.jackson.databind.ObjectWriterwriter()
-
Constructor Details
-
JsonSerialization
Create an instance bound to a specific type.- Parameters:
classType- class to marshallfailOnUnknownProperties- fail if an unknown property is encountered.pretty- generate pretty (indented) output?
-
-
Method Details
-
writer
public static com.fasterxml.jackson.databind.ObjectWriter writer()- Returns:
- an ObjectWriter which pretty-prints its output
-
mapReader
public static com.fasterxml.jackson.databind.ObjectReader mapReader()- Returns:
- an ObjectReader which returns simple Maps.
-
getName
Get the simple name of the class type to be marshalled.- Returns:
- the name of the class being marshalled
-
getMapper
public com.fasterxml.jackson.databind.ObjectMapper getMapper()Get the mapper of this class.- Returns:
- the mapper
-
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 problemscom.fasterxml.jackson.core.JsonParseException- If the input is not well-formattedcom.fasterxml.jackson.databind.JsonMappingException- failure to map from the JSON to this class
-
fromJsonStream
Read from an input stream.- Parameters:
stream- stream to read from- Returns:
- the parsed entity
- Throws:
IOException- IO problemscom.fasterxml.jackson.core.JsonParseException- If the input is not well-formattedcom.fasterxml.jackson.databind.JsonMappingException- failure to map from the JSON to this class
-
load
public T load(File jsonFile) throws IOException, com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.databind.JsonMappingException Load from a JSON text file.- Parameters:
jsonFile- input file- Returns:
- the parsed JSON
- Throws:
IOException- IO problemscom.fasterxml.jackson.core.JsonParseException- If the input is not well-formattedcom.fasterxml.jackson.databind.JsonMappingException- failure to map from the JSON to this class
-
save
Save to a local file. Any existing file is overwritten unless the OS blocks that.- Parameters:
file- fileinstance- instance- Throws:
IOException- IO exception
-
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 problemscom.fasterxml.jackson.core.JsonParseException- If the input is not well-formattedcom.fasterxml.jackson.databind.JsonMappingException- failure to map from the JSON to this class
-
fromInstance
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- IO problems.
-
load
Load from a Hadoop filesystem.- Parameters:
fs- filesystempath- path- Returns:
- a loaded object
- Throws:
PathIOException- JSON parse problemIOException- IO problems
-
load
Load from a Hadoop filesystem. If a file status is supplied, it's passed in to the openFile() call so that FS implementations can optimize their opening.- Parameters:
fs- filesystempath- pathstatus- status of the file to open.- Returns:
- a loaded object
- Throws:
PathIOException- JSON parse problemEOFException- file status references an empty fileIOException- IO problems
-
save
Save to a Hadoop filesystem.- Parameters:
fs- filesystempath- pathoverwrite- should any existing file be overwritteninstance- instance- Throws:
IOException- IO exception.
-
writeJsonAsBytes
Write the JSON as bytes, then close the stream.- Parameters:
instance- instance to writedataOutputStream- an output stream that will always be closed- Throws:
IOException- on any failure
-
toBytes
Convert JSON to bytes.- Parameters:
instance- instance to convert- Returns:
- a byte array
- Throws:
IOException- IO problems
-
fromBytes
Deserialize from a byte array.- Parameters:
bytes- byte array- Returns:
- byte array.
- Throws:
IOException- IO problemsEOFException- not enough data
-
toJson
Convert an instance to a JSON string.- Parameters:
instance- instance to convert- Returns:
- a JSON string description
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- Json generation problems
-
toString
Convert an instance to a string form for output. This is a robust operation which will convert any JSON-generating exceptions into error text.- Parameters:
instance- non-null instance- Returns:
- a JSON string
-