Java Generate/Merge Files(5)Jackson and JSON
In java, we usually use jackson to convert JSON to string and string to JSON. I write a general class to support that.
pom.xml dependencies
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
My major core java codes to do the converting
package com.j2c.feeds2g.services;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.j2c.feeds2g.services.base.BaseService;
public class JSONMappingServiceJacksonImpl<T> extends BaseService implements JSONMappingService<T> {
private ObjectMapper jsonMapper;
public void init() {
jsonMapper = new ObjectMapper();
}
public T toJava(String json, Class<T> type) {
try {
return (T) jsonMapper.readValue(json, type);
} catch (JsonGenerationException e) {
logger.error("unmashall string [" + json + "] fail, exceptions: " , e);
} catch (JsonMappingException e) {
logger.error("unmashall string [" + json + "] fail, exceptions: " , e);
} catch (IOException e) {
logger.error("unmashall string [" + json + "] fail, exceptions: " , e);
}
return null;
}
public String toJSON(T objClass) {
try {
return jsonMapper.writeValueAsString(objClass);
} catch (JsonGenerationException e) {
logger.error("mashall object [" + objClass + "] fail, exceptions: " , e);
} catch (JsonMappingException e) {
logger.error("mashall object [" + objClass + "] fail, exceptions: " , e);
} catch (IOException e) {
logger.error("mashall object [" + objClass + "] fail, exceptions: " , e);
}
return null;
}
}
My unit tests
package com.j2c.feeds2g.services;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.j2c.feeds2g.models.Job;
import com.j2c.feeds2g.services.base.BaseService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class JsonMappingServiceTest extends BaseService {
@Autowired
@Qualifier("jsonMappingService")
private JSONMappingService<Job> jsonMappingService;
@Test
public void dummy() {
Assert.assertTrue(true);
}
@Test
public void json() throws IOException {
String jsonFile = FileUtils.readFileToString(new File(getFile("data/job.json")), StandardCharsets.UTF_8);
logger.trace("JsonFile content " + jsonFile);
Job job = jsonMappingService.toJava(jsonFile, Job.class);
Assert.assertNotNull(job);
logger.info("Job [" + job + "]");
logger.debug("cities [" + job.getCities() + "]");
logger.debug("stateIDs [" + job.getStateIDs() + "]");
logger.debug("posted [" + job.getPosted() + "]");
String decodeJson = jsonMappingService.toJSON(job);
Assert.assertNotNull(decodeJson);
logger.info("json " + decodeJson);
}
}
Some annotation in the POJO
@JsonProperty
private String action;
@JsonProperty("customer_id")
private Long customerID;
@JsonProperty
private DateTime posted;
@JsonProperty("industry_ids")
private List<Integer> industryIDs;
References:
http://websystique.com/java/json/jackson-json-annotations-example/
https://github.com/FasterXML/jackson-annotations
http://sillycat.iteye.com/blog/2119381
Java Generate/Merge Files(5)Jackson and JSON
猜你喜欢
转载自sillycat.iteye.com/blog/2368707
今日推荐
周排行