Java Encode encode(Object obj)

Here you can find the source of encode(Object obj)

Description

json encode

License

Apache License

Parameter

Parameter Description
obj a parameter

Return

json

Declaration

public static String encode(Object obj) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.Writer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Main {
    /**/*from www.  j a  v  a2  s .  c  om*/
     * json encode
     * @param obj
     * @return json
     * @author sinmetal
     */
    public static String encode(Object obj) {
        try {
            return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS).writeValueAsString(obj);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    public static void encode(Writer writer, Object obj) {
        try {
            new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS).writeValue(writer, obj);
        } catch (IOException e) {
            throw new IllegalStateException();
        }
    }
}

Related

  1. encode(Object o)
  2. encode(Object o)
  3. encode(Serializable s)
  4. encode(T obj)
  5. encode2Encode(String original, String encode1, String encode2)
  6. encode_u8(String what)