Jackjson
配置:
1 2 3 4 5 6 7 8 9 10 11 12 ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false ); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" )); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false ); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
yml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 spring: jackson: property-naming-strategy: SNAKE_CASE date-format: yyyy-MM-dd HH:mm:ss locale: zh time-zone: GMT+8 default-property-inclusion: NON_NULL serialization: WRITE_DATES_AS_TIMESTAMPS: true FAIL_ON_EMPTY_BEANS: true deserialization: FAIL_ON_UNKNOWN_PROPERTIES: false mapper: USE_GETTERS_AS_SETTERS: true parser: ALLOW_SINGLE_QUOTES: true
各种转换:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 System.out.println("==== map->对象 =====" ); User user = objectMapper.convertValue(map, User.class ) ; System.out.println("==== 对象->json串 =====" ); String jsonStr = objectMapper.writeValueAsString(map); System.out.println(jsonStr); System.out.println("==== json串-->json节点对象 =====" ); JsonNode jsonNode = objectMapper.readTree(jsonStr); System.out.println(jsonNode); System.out.println(jsonNode.get("name" ).asText()); System.out.println(jsonNode.get("age" ).asInt()); System.out.println("==== json串-->实体对象(map) =====" ); Map param = objectMapper.readValue(jsonStr, Map.class ) ; Person person = objectMapper.readValue(jsonStr, Person.class ) ; System.out.println(param); System.out.println(person); System.out.println("==== json串 --> 集合对象 =====" ); List<Person> list = new ArrayList<>(); list.add(new Person("李四" ,24 )); String listStr = objectMapper.writeValueAsString(list); JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class , Person .class ) ; List<Person> persons = (List<Person>)objectMapper.readValue(listStr, javaType); System.out.println(persons); List<Person> persons2 = objectMapper.readValue(listStr, new TypeReference<List<Person>>() {}) System.out.println("==== xml串 --> 对象 =====" ); XmlMapper xmlMapper = new XmlMapper(); User user = xmlMapper.readValue(xml, User.class ) ; System.out.println("==== 对象 --> xml =====" ); XmlMapper xmlMapper = new XmlMapper(); String xmlStr = xmlMapper.writeValueAsString(user)
Fastjson 各种转换:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 System.out.println("==== 对象->json串 =====" ); Person p = new Person("李四" , 24 ); String jsonString = JSON.toJSONString(p); System.out.println(jsonString); System.out.println("==== json串-->实体对象(map) =====" ); Person person = JSON.parseObject(jsonString, Person.class ) ; Map map = JSON.parseObject(jsonString, Map.class ) ; JSONObject jsonObject = JSON.parseObject(jsonString); System.out.println(person); System.out.println(map); System.out.println(jsonObject); System.out.println("==== json串 --> 集合对象 =====" ); List<Person> list = new ArrayList<>(); list.add(p); String listStr = JSON.toJSONString(list); List<Person> peoples = JSON.parseArray(listStr, Person.class ) ; JSONArray jsonArray = JSON.parseArray(listStr); System.out.println(peoples); System.out.println(jsonArray); @Configuration public class HttpConverterConfig { @Bean public HttpMessageConverters fastJsonHttpMessageConverters () { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } }
Jackson2HashMapper org.springframework.data.redis.hash
1 2 3 4 5 Jackson2HashMapper jackson2HashMapper = new Jackson2HashMapper(objectMapper,false ); Map<String, Object> map = jackson2HashMapper.toHash(user); Object o = jackson2HashMapper.fromHash(map);