Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.dropwizard.json;
  
  
 import java.io.*;
A basic class for JSON parsing and generating.

By default, Json is configured to:

  • Automatically close JSON content, if possible.
  • Automatically close input and output streams.
  • Quote field names.
  • Allow both C-style line and block comments.
  • Not fail when encountering unknown properties.
  • Read and write enums using toString().
  • Use snake_case for property names when encoding and decoding classes annotated with JsonSnakeCase.
 
 public class Json {
     protected JsonFactory factory;
     protected ObjectMapper mapper;
     protected TypeFactory typeFactory;

    
Creates a new Json instance.
 
     public Json() {
         this. = new MappingJsonFactory();
 
         this. = (ObjectMapper.getCodec();
         .registerModule(new GuavaModule());
         .registerModule(new Log4jModule());
 
         this. = .getTypeFactory();
     }

    
Registers a module that can extend functionality provided by this class; for example, by adding providers for custom serializers and deserializers.

 
     public void registerModule(Module module) {
         .registerModule(module);
     }

    
Returns true if the given org.codehaus.jackson.map.DeserializationConfig.Feature is enabled.

Parameters:
feature a given feature
Returns:
true if feature is enabled
See also:
org.codehaus.jackson.map.ObjectMapper.isEnabled(org.codehaus.jackson.map.DeserializationConfig.Feature)
 
     public boolean isEnabled(DeserializationConfig.Feature feature) {
         return .isEnabled(feature);
     }

    
 
     public void enable(DeserializationConfig.Feature... features) {
         .enable(features);
     }

    
 
     public void disable(DeserializationConfig.Feature... features) {
         .disable(features);
     }

    
Returns true if the given org.codehaus.jackson.map.SerializationConfig.Feature is enabled.

Parameters:
feature a given feature
Returns:
true if feature is enabled
See also:
org.codehaus.jackson.map.ObjectMapper.isEnabled(org.codehaus.jackson.map.SerializationConfig.Feature)
    public boolean isEnabled(SerializationConfig.Feature feature) {
        return .isEnabled(feature);
    }

    
    public void enable(SerializationConfig.Feature... features) {
        .enable(features);
    }

    
    public void disable(SerializationConfig.Feature... features) {
        .disable(features);
    }

    
Returns true if the given org.codehaus.jackson.JsonGenerator.Feature is enabled.

Parameters:
feature a given feature
Returns:
true if feature is enabled
See also:
org.codehaus.jackson.map.ObjectMapper.isEnabled(org.codehaus.jackson.JsonGenerator.Feature)
    public boolean isEnabled(JsonGenerator.Feature feature) {
        return .isEnabled(feature);
    }

    
    public void enable(JsonGenerator.Feature... features) {
        for (JsonGenerator.Feature feature : features) {
            .enable(feature);
        }
    }

    
    public void disable(JsonGenerator.Feature... features) {
        for (JsonGenerator.Feature feature : features) {
            .disable(feature);
        }
    }

    
Returns true if the given org.codehaus.jackson.JsonParser.Feature is enabled.

Parameters:
feature a given feature
Returns:
true if feature is enabled
See also:
org.codehaus.jackson.map.ObjectMapper.isEnabled(org.codehaus.jackson.JsonParser.Feature)
    public boolean isEnabled(JsonParser.Feature feature) {
        return .isEnabled(feature);
    }

    
    public void enable(JsonParser.Feature... features) {
        for (JsonParser.Feature feature : features) {
            .enable(feature);
        }
    }

    
Disables the given org.codehaus.jackson.JsonParser.Features.

Parameters:
features a set of features to disable
See also:
org.codehaus.jackson.JsonFactory.disable(org.codehaus.jackson.JsonParser.Feature)
    public void disable(JsonParser.Feature... features) {
        for (JsonParser.Feature feature : features) {
            .disable(feature);
        }
    }

    
Returns true if the mapper can find a serializer for instances of given class (potentially serializable), false otherwise (not serializable).

Parameters:
type the type of object to serialize
Returns:
true if instances of type are potentially serializable
    public boolean canSerialize(Class<?> type) {
        return .canSerialize(type);
    }

    
Returns true if the mapper can find a deserializer for instances of given class (potentially deserializable), false otherwise (not deserializable).

Parameters:
type the type of object to deserialize
Returns:
true if instances of type are potentially deserializable
    public boolean canDeserialize(Class<?> type) {
        return .canDeserialize(constructType(type));
    }

    
Deserializes the given java.io.File as an instance of the given type.

Parameters:
src a JSON java.io.File
valueType the java.lang.Class to deserialize src as
<T> the type of valueType
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readValue(File srcClass<T> valueTypethrows IOException {
        return .readValue(srcvalueType);
    }

    
Deserializes the given java.io.File as an instance of the given type.

Parameters:
src a JSON java.io.File
valueTypeRef a org.codehaus.jackson.type.TypeReference of the type to deserialize src as
<T> the type of valueTypeRef
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readValue(File srcTypeReference<T> valueTypeRefthrows IOException {
        return .readValue(srcvalueTypeRef);
    }

    
Deserializes the given java.lang.String as an instance of the given type.

Parameters:
content a JSON java.lang.String
valueType the java.lang.Class to deserialize content as
<T> the type of valueType
Returns:
content as an instance of T
Throws:
java.io.IOException if there is an error parsing content
    public <T> T readValue(String contentClass<T> valueTypethrows IOException {
        return .readValue(contentvalueType);
    }

    
Deserializes the given java.lang.String as an instance of the given type.

Parameters:
content a JSON java.lang.String
valueTypeRef a org.codehaus.jackson.type.TypeReference of the type to deserialize content as
<T> the type of valueTypeRef
Returns:
content as an instance of T
Throws:
java.io.IOException if there is an error parsing content
    public <T> T readValue(String contentTypeReference<T> valueTypeRefthrows IOException {
        return .readValue(contentvalueTypeRef);
    }

    
Deserializes the given java.io.Reader as an instance of the given type.

Parameters:
src a JSON java.io.Reader
valueType the java.lang.Class to deserialize src as
<T> the type of valueType
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readValue(Reader srcClass<T> valueTypethrows IOException {
        return .readValue(srcvalueType);
    }

    
Deserializes the given java.io.Reader as an instance of the given type.

Parameters:
src a JSON java.io.Reader
valueTypeRef a org.codehaus.jackson.type.TypeReference of the type to deserialize src as
<T> the type of valueTypeRef
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readValue(Reader srcTypeReference<T> valueTypeRefthrows IOException {
        return .readValue(srcvalueTypeRef);
    }

    
Deserializes the given java.io.InputStream as an instance of the given type.

Parameters:
src a JSON java.io.InputStream
valueType the java.lang.Class to deserialize src as
<T> the type of valueType
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readValue(InputStream srcClass<T> valueTypethrows IOException {
        return .readValue(srcvalueType);
    }

    
Deserializes the given java.io.InputStream as an instance of the given type.

Parameters:
src a JSON java.io.InputStream
valueTypeRef a org.codehaus.jackson.type.TypeReference of the type to deserialize src as
<T> the type of valueTypeRef
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readValue(InputStream srcTypeReference<T> valueTypeRefthrows IOException {
        return .readValue(srcvalueTypeRef);
    }

    
Deserializes the given java.io.InputStream as an instance of the given type.

Parameters:
src a JSON java.io.InputStream
valueType the java.lang.reflect.Type to deserialize src as
<T> the type of valueType
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readValue(InputStream srcType valueTypethrows IOException {
        return .readValue(srcconstructType(valueType));
    }

    
Deserializes the given byte array as an instance of the given type.

Parameters:
src a JSON byte array
valueType the java.lang.Class to deserialize src as
<T> the type of valueType
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error parsing src
    public <T> T readValue(byte[] srcClass<T> valueTypethrows IOException {
        return .readValue(srcvalueType);
    }

    
Deserializes a subset of the given byte array as an instance of the given type.

Parameters:
src a JSON byte array
offset the offset into src of the subset
len the length of the subset of src
valueType the java.lang.Class to deserialize src as
<T> the type of valueType
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error parsing src
    public <T> T readValue(byte[] srcint offsetint lenClass<T> valueTypethrows IOException {
        return .readValue(srcoffsetlenvalueType);
    }

    
Deserializes the given byte array as an instance of the given type.

Parameters:
src a JSON byte array
valueTypeRef a org.codehaus.jackson.type.TypeReference of the type to deserialize src as
<T> the type of valueTypeRef
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error parsing src
    public <T> T readValue(byte[] srcTypeReference<T> valueTypeRefthrows IOException {
        return .readValue(srcvalueTypeRef);
    }

    
Deserializes a subset of the given byte array as an instance of the given type.

Parameters:
src a JSON byte array
offset the offset into src of the subset
len the length of the subset of src
valueTypeRef a org.codehaus.jackson.type.TypeReference of the type to deserialize src as
<T> the type of valueTypeRef
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error parsing src
    public <T> T readValue(byte[] srcint offsetint lenTypeReference<T> valueTypeRefthrows IOException {
        return .readValue(srcoffsetlenvalueTypeRef);
    }

    
Deserializes the given org.codehaus.jackson.JsonNode as an instance of the given type.

Parameters:
root a org.codehaus.jackson.JsonNode
valueType the java.lang.Class to deserialize src as
<T> the type of valueType
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error mapping src to T
    public <T> T readValue(JsonNode rootClass<T> valueTypethrows IOException {
        return .readValue(rootvalueType);
    }

    
Deserializes the given org.codehaus.jackson.JsonNode as an instance of the given type.

Parameters:
root a org.codehaus.jackson.JsonNode
valueTypeRef a org.codehaus.jackson.type.TypeReference of the type to deserialize src as
<T> the type of valueTypeRef
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error mapping src to T
    public <T> T readValue(JsonNode rootTypeReference<T> valueTypeRefthrows IOException {
        return .readValue(rootvalueTypeRef);
    }

    
Serializes the given object to the given java.io.File.

Parameters:
output the java.io.File to which the JSON will be written
value the object to serialize into output
Throws:
java.io.IOException if there is an error writing to output or serializing value
    public void writeValue(File outputObject valuethrows IOException {
        .writeValue(outputvalue);
    }

    
Serializes the given object to the given java.io.OutputStream.

Parameters:
output the java.io.OutputStream to which the JSON will be written
value the object to serialize into output
Throws:
java.io.IOException if there is an error writing to output or serializing value
    public void writeValue(OutputStream outputObject valuethrows IOException {
        .writeValue(outputvalue);
    }

    
Serializes the given object to the given java.io.Writer.

Parameters:
output the java.io.Writer to which the JSON will be written
value the object to serialize into output
Throws:
java.io.IOException if there is an error writing to output or serializing value
    public void writeValue(Writer outputObject valuethrows IOException {
        .writeValue(outputvalue);
    }

    
Returns the given object as a JSON string.

Parameters:
value an object
Returns:
value as a JSON string
Throws:
java.lang.IllegalArgumentException if there is an error encoding value
    public String writeValueAsString(Object valuethrows IllegalArgumentException {
        try {
            return .writeValueAsString(value);
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }

    
Returns the given object as a JSON byte array.

Parameters:
value an object
Returns:
value as a JSON byte array
Throws:
java.lang.IllegalArgumentException if there is an error encoding value
    public byte[] writeValueAsBytes(Object valuethrows IllegalArgumentException {
        try {
            return .writeValueAsBytes(value);
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }

    
Returns the given object as a org.codehaus.jackson.JsonNode.

Parameters:
value an object
Returns:
value as a org.codehaus.jackson.JsonNode
Throws:
java.lang.IllegalArgumentException if there is an error encoding value
    public JsonNode writeValueAsTree(Object valuethrows IllegalArgumentException {
        return .valueToTree(value);
    }

    
Deserializes the given YAML java.io.File as an instance of the given type.

N.B.: All tags, comments, and non-JSON elements of the YAML file will be elided.

Parameters:
src a YAML java.io.File
valueType the java.lang.Class to deserialize src as
<T> the type of valueType
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readYamlValue(File srcClass<T> valueTypethrows IOException {
        final YamlConverter converter = new YamlConverter(this);
        return .readValue(converter.convert(src), valueType);
    }

    
Deserializes the given YAML java.io.File as an instance of the given type.

N.B.: All tags, comments, and non-JSON elements of the YAML file will be elided.

Parameters:
src a YAML java.io.File
valueTypeRef a org.codehaus.jackson.type.TypeReference of the type to deserialize src as
<T> the type of valueTypeRef
Returns:
the contents of src as an instance of T
Throws:
java.io.IOException if there is an error reading from src or parsing its contents
    public <T> T readYamlValue(File srcTypeReference<T> valueTypeRefthrows IOException {
        final YamlConverter converter = new YamlConverter(this);
        return .readValue(converter.convert(src), valueTypeRef);
    }
    private JavaType constructType(Type type) {
        return .constructType(type);
    }
New to GrepCode? Check out our FAQ X