Several simple transformations of complex object collections in Java's Jackson library
- 2020-06-03 06:33:28
- OfStack
Without further ado, see the code:
package com;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* jackson complex A collection of objects Of several simple transformations
* @author lenovo
*
* @param <T>
*/
public class Main<T>
{
static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws JsonParseException,
JsonMappingException, IOException
{
String josn = "{\"UserID\":1,\"LoginName\":\" Tang Gong \",\"Truename\":\" super \",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0}";
User u = mapper.readValue(josn, User.class);
// User u=new Main<User>().jsonStreamConverObject(josn, User.class);
System.out.println(" Turn the object :" + u);
// Turn the collection
String josn2 = "[{\"UserID\":1,\"LoginName\":\" Tang Gong \",\"Truename\":\" super \",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0}]";
JavaType javaType = mapper.getTypeFactory().constructParametricType(
List.class, User.class);
List<User> me = mapper.readValue(josn2, javaType);
System.out.println(" Turn the collection me:" + me);
// In the object A collection of conversion
String josn3 = "{\"UserID\":1,\"LoginName\":\" Tang Gong \",\"Truename\":\" super \",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0,\"RoleList\":[{\"Roleid\":0,\"Name\":\" Super administrator \",\"Show_Name\":\" Super administrator \",\"Remark\":null,\"Type\":1}]}";
User u3 = mapper.readValue(josn3, User.class); // A simple way
// User u3=new Main<User>().jsonConverObject(josn3, User.class); Flow way
System.out.println(" There's a set in a spin object u3:" + u3);
// A collection of object A collection of conversion
String josn4 = "[{\"UserID\":1,\"LoginName\":\" Tang Gong \",\"Truename\":\" super \",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0,\"RoleList\":[{\"Roleid\":0,\"Name\":\" Super administrator \",\"Show_Name\":\" Super administrator \",\"Remark\":null,\"Type\":1}]},{\"UserID\":2,\"LoginName\":\" Tang Gong \",\"Truename\":\" super \",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0,\"RoleList\":[{\"Roleid\":0,\"Name\":\" Super administrator \",\"Show_Name\":\" Super administrator \",\"Remark\":null,\"Type\":1}]}]";
JavaType javaType4 = mapper.getTypeFactory().constructParametricType(
List.class, User.class);
List<User> list = mapper.readValue(josn4, javaType4);
System.out.println(" The set is the object Object has a collection transformation :" + list);
}
/***
* Turn the object
* @param josn
* @param clz
* @return
*/
public T jsonStreamConverObject(String josn, Class<T> clz)
{
T t = null;
// ObjectMapper jacksonMapper = new ObjectMapper();
InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(
josn.getBytes()));
BufferedReader streamReader = new BufferedReader(in);
StringBuilder buff = new StringBuilder();
String inputStr;
try
{
while ((inputStr = streamReader.readLine()) != null)
buff.append(inputStr);
// ObjectMapper mapper = new ObjectMapper();
t = mapper.readValue(buff.toString(), clz);
} catch (IOException e)
{
e.printStackTrace();
}
return t;
}
/***
* Turn the object
* @param josn
* @param clz
* @return
*/
public T jsonConverObject(String josn, Class<T> clz)
{
T t = null;
try
{
t = mapper.readValue(josn, clz);
} catch (JsonParseException e)
{
e.printStackTrace();
} catch (JsonMappingException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return t;
}
/**
* Turn the collection
* @param josn
* @param clz
* @return
*/
public List<T> jsonConverList(String josn, Class<T> clz)
{
List<T> me = null;
try
{
// jacksonMapper
// .disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
// jacksonMapper.enableDefaultTyping();
// jacksonMapper.setVisibility(JsonMethod.FIELD,JsonAutoDetect.Visibility.ANY);
// jacksonMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT,
// false);// formatting
// jacksonMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
// jacksonMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,
// false);
JavaType javaType = mapper.getTypeFactory()
.constructParametricType(List.class, clz);// clz.selGenType().getClass()
me = mapper.readValue(josn, javaType);
} catch (JsonParseException e)
{
e.printStackTrace();
} catch (JsonMappingException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return me;
}
}
/**
* output:
* Turn the object :User [UserID=1, LoginName= Tang Gong , Truename= super , Nickname=null, LoginPwd=E10ADC3949BA59ABBE56E057F20F883E, QQ=, Phone=, Email=null, Remark=, Account_Non_Locked=0, Telelephone=null, Indate=null, IsDelete=0, RoleList=null]
* Turn the collection me:[User [UserID=1, LoginName= Tang Gong , Truename= super , Nickname=null, LoginPwd=E10ADC3949BA59ABBE56E057F20F883E, QQ=, Phone=, Email=null, Remark=, Account_Non_Locked=0, Telelephone=null, Indate=null, IsDelete=0, RoleList=null]]
* There's a set in a spin object u3:User [UserID=1, LoginName= Tang Gong , Truename= super , Nickname=null, LoginPwd=E10ADC3949BA59ABBE56E057F20F883E, QQ=, Phone=, Email=null, Remark=, Account_Non_Locked=0, Telelephone=null, Indate=null, IsDelete=0, RoleList=[Role [Roleid=0, Name= Super administrator , Show_Name= Super administrator , Remark=null, Type=1]]]
* The set is the object Object has a collection transformation :[User [UserID=1, LoginName= Tang Gong , Truename= super , Nickname=null, LoginPwd=E10ADC3949BA59ABBE56E057F20F883E, QQ=, Phone=, Email=null, Remark=, Account_Non_Locked=0, Telelephone=null, Indate=null, IsDelete=0, RoleList=[Role [Roleid=0, Name= Super administrator , Show_Name= Super administrator , Remark=null, Type=1]]], User [UserID=2, LoginName= Tang Gong , Truename= super , Nickname=null, LoginPwd=E10ADC3949BA59ABBE56E057F20F883E, QQ=, Phone=, Email=null, Remark=, Account_Non_Locked=0, Telelephone=null, Indate=null, IsDelete=0, RoleList=[Role [Roleid=0, Name= Super administrator , Show_Name= Super administrator , Remark=null, Type=1]]]]
* */