Java Generate/Merge Files(5)Jackson and JSON

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值