/* * Copyright (c) 2009-2014. 上海诺诺镑客 All rights reserved. * @(#) TripleTest.java 2014-10-27 16:47 */ package com.nonobank.common.tuple; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import java.util.HashMap; //import org.hamcrest.core.Is; //import org.hamcrest.core.IsEqual; //import org.hamcrest.core.IsInstanceOf; //import org.hamcrest.core.IsNull; import org.hamcrest.core.Is; import org.hamcrest.core.IsEqual; import org.hamcrest.core.IsInstanceOf; import org.hamcrest.core.IsNull; import org.junit.Test; import com.fasterxml.jackson.core.type.TypeReference; import com.nonobank.common.domain.MyTriple; import com.nonobank.common.json.JsonMapper; /** * {@link Triple} test case. * * @author fuchun */ public class TripleTest { @Test public void testJsonProperty() throws Exception { Triple<Short, Integer, Long> nt = Triple.of((short) 1, 2, 3L); String numTripleJson = JsonMapper.getDefault().toJSONString(nt); String targetJson = "{\"l\":1,\"m\":2,\"r\":3}"; System.out.println("numTripleJson:"+numTripleJson); assertNotNull(numTripleJson); assertThat(numTripleJson, Is.is(targetJson)); Triple<Short, Integer, Long> nt2 = JsonMapper.getDefault().readValue(targetJson, new TypeReference<Triple<Short, Integer, Long>>(){}.getType()); MyTriple mt=JsonMapper.getDefault().readValue(targetJson, MyTriple.class); System.out.println(mt); // MyTriple mt=JsonMapper.getDefault().readValue2(targetJson, // MyTriple.class); // System.out.println(mt); assertThat(nt2, IsNull.notNullValue()); assertThat(nt2, IsEqual.equalTo(nt)); assertThat(nt2, IsInstanceOf.instanceOf(ImmutableTriple.class)); assertThat(nt.toString(), Is.is(String.format( "(%s,%s,%s)", nt.getLeft(), nt.getMiddle(), nt.getRight()))); } @Test public void testHamCrest(){ int a=1; int b=1; } @Test public void testMapper(){ String sourceJson = "{\"l\":1,\"m\":2,\"r\":3}"; HashMap<String, Integer> hm; hm=(HashMap<String, Integer>) JsonMapper.getDefault().readToMap(sourceJson, String.class, Integer.class); System.out.println(hm); /*String targetJson=null; targetJson=JsonMapper.getDefault().toJSONString(hm); System.out.println(targetJson);*/ } @Test public void testJsonWrite(){ //1.构造Java Bean MyTriple mt=new MyTriple(); mt.setL((short)1); mt.setM(2); mt.setR(3L); //2.利用框架的Util类,生成JsonString String targetJson=null; targetJson=JsonMapper.getDefault().toJSONString(mt); System.out.println(targetJson); } @Test public void testJsonRead(){ //1.构造目标Json // String targetJson = "{\"l\":1,\"m\":2,\"r\":3,\"g\":4}"; String targetJson = "{\"l\":1,\"m\":2,\"r\":3}"; //2.构造目标 Java Bean MyTriple mt=JsonMapper.getDefault().readValue(targetJson, MyTriple.class); System.out.println(mt); } }
jackson demo
猜你喜欢
转载自wandejun1012.iteye.com/blog/2229164
今日推荐
周排行