Jackson Json Conversion

Source: Internet
Author: User

Public class JacksonJsonUtil {private static ObjectMapper mapper;/*** get ObjectMapper instance * @ param createNew method: true, new instance; false, existing mapper instance * @ return */public static synchronized ObjectMapper getMapperInstance (boolean createNew) {if (createNew) {return new ObjectMapper ();} else if (mapper = null) {mapper = new ObjectMapper ();} return mapper;}/*** converts a java object to a json string * @ param obj the object to be converted * @ retu Rn json String * @ throws Exception */public static String beanToJson (Object obj) throws Exception {try {ObjectMapper objectMapper = getMapperInstance (false); String json = objectMapper. writeValueAsString (obj); return json;} catch (Exception e) {throw new Exception (e. getMessage () ;}}/*** converts a java object to a json string * @ param obj: the object to be converted * @ param createNew ObjectMapper: true, new instance; false: The mapper instance * @ return json character. String * @ throws Exception */public static String beanToJson (Object obj, Boolean createNew) throws Exception {try {ObjectMapper objectMapper = getMapperInstance (createNew); String json = objectMapper. writeValueAsString (obj); return json;} catch (Exception e) {throw new Exception (e. getMessage ());}} /*** convert a json string to a java object * @ param json the json string to be converted * @ param cls the class to be converted * @ return * @ throws Exception */public s Tatic Object jsonToBean (String json, Class <?> Cls) throws Exception {try {ObjectMapper objectMapper = getMapperInstance (false); Object vo = objectMapper. readValue (json, cls); return vo;} catch (Exception e) {throw new Exception (e. getMessage ());}} /*** convert a json string to a java object * @ param json the json string to be converted * @ param cls the class to be converted * @ param createNew ObjectMapper instance method: true, new instance; false: mapper instance * @ return * @ throws Exception */public static Object jsonToBean (Stri Ng json, Class <?> Cls, Boolean createNew) throws Exception {try {ObjectMapper objectMapper = getMapperInstance (createNew); Object vo = objectMapper. readValue (json, cls); return vo;} catch (Exception e) {throw new Exception (e. getMessage ());}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.