Class JsonBeanEncoder

  • All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

    public class JsonBeanEncoder
    extends JsonCodec
    implements Flushable, Closeable
    Encoder for converting a Java object graph to JSON.

    Objects may be arrays, collections, maps and JavaBeans.

    Arrays and collections are converted to JSON arrays. The type information is lost. Maps and JavaBeans are converted to JSON objects.

    The generated JSON objects can have an additional key/value pair with key “class” and a class name. The class information is generated only if it is needed, i.e. if it cannot be derived from the containing object.

    Given the following classes:

    public static class Person {
    
        private String name;
        private int age;
        private PhoneNumber[] numbers;
    
        public String getName() {
            return name;
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public int getAge() {
            return age;
        }
        
        public void setAge(int age) {
            this.age = age;
        }
        
        public PhoneNumber[] getNumbers() {
            return numbers;
        }
        
        public void setNumbers(PhoneNumber[] numbers) {
            this.numbers = numbers;
        }
    }
    
    public static class PhoneNumber {
        private String name;
        private String number;
    
        public PhoneNumber() {
        }
        
        public String getName() {
            return name;
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public String getNumber() {
            return number;
        }
        
        public void setNumber(String number) {
            this.number = number;
        }
    }
    
    public static class SpecialNumber extends PhoneNumber {
    }
    

    A serialization result may look like this:

    {
        "age": 42,
        "name": "Simon Sample",
        "numbers": [
            {
                "name": "Home",
                "number": "06751 51 56 57"
            },
            {
                "class": "test.json.SpecialNumber",
                "name": "Work",
                "number": "030 77 35 44"
            }
        ]
    } 
    
    • Method Detail

      • addAlias

        public JsonBeanEncoder addAlias​(Class<?> clazz,
                                        String alias)
        Description copied from class: JsonCodec
        Add an alias for the given class.

        If defined, the alias will be used instead of the class name by the encoder.

        Specified by:
        addAlias in class JsonCodec
        Parameters:
        clazz - the class
        alias - the alias
        Returns:
        the object for easy chaining
      • omitClass

        public JsonBeanEncoder omitClass()
        Configure the encoder to not generate the class information even when needed to properly restore the Object graph.

        While this contradicts the initial objective to provide JSON persistence for JavaBeans, this is a valid option if the generated JSON is used for transferring information to an environment where the information provided by class isn’t useful.

        Returns:
        the encoder for easy chaining
      • addExcluded

        public JsonBeanEncoder addExcluded​(String className)
        Add a type to excude from encoding, usually because it cannot be converted to JSON.

        Properties of such types should be marked as Transient. However, sometimes base types don’t follow the rules.

        Parameters:
        className -
        Returns:
        the encoder for easy chaining
      • create

        public static JsonBeanEncoder create​(Writer out)
        Create a new encoder using a default JsonGenerator.
        Parameters:
        out - the sink
        Returns:
        the encoder
      • create

        public static JsonBeanEncoder create()
        Create a new encoder using a default JsonGenerator that writes to an internally created StringWriter.

        The result can be obtained by invoking toJson().

        Returns:
        the encoder
      • create

        public static JsonBeanEncoder create​(com.fasterxml.jackson.core.JsonGenerator generator)
        Create a new encoder using the given JsonGenerator.
        Parameters:
        generator - the generator
        Returns:
        the encoder
      • toJson

        public String toJson()
        Returns the text written to the output.

        Can only be used if the encoder has been created with JsonBeanEncoder().

        Returns:
        the result