FastJSON是一个很好的java开源json工具类库,相比其他同类的json类库,它的速度的确是快,所以在开发中我们会使用到fastjson,那fastjson如何使用?下面来我们就来给大家讲解一下fastjson使用详解。
FastJson基本使用
将POJO对象转换成Json格式String
JSON.toJSONString(Object)
将Json格式字符串转换成POJO对象
JSON.parseObject(jsonStr, pojoCls)
实例
private static class UserGroup { private int groupId; private String name; public int getGroupId() { return groupId; } public void setGroupId(int groupId) { this.groupId = groupId; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
UserGroup userGroup = new UserGroup(); userGroup.setGroupId(1); userGroup.setName("group1"); //encode String jsonValue = JSON.toJSONString(userGroup); //decode UserGroup group = JSON.parseObject(jsonValue, UserGroup.class);
FastJson自定义序列化与反序列化
1.自定义序列化
a.实现ObjectSerializer
public class OrderActionEnumSerializer implements ObjectSerializer { @Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter writer = serializer.out; OrderActionEnum orderActionEnum = (OrderActionEnum) object; writer.write(orderActionEnum.getValue()); } }
b.注册序列化接口
SerializeConfig.getGlobalInstance() .put(OrderActionEnum.class, new OrderActionEnumSerializer());
2.自定义反序列化
a. 实现反序列化接口
public class OrderActionEnumDeserializer implements ObjectDeserializer { @Override public < T > T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { Integer intValue = parser.parseObject(int.class); OrderActionEnum orderActionEnum = OrderActionEnum.getOrderAction(intValue); if (orderActionEnum != null) { return (T) orderActionEnum; } throw new IllegalStateException(); } @Override public int getFastMatchToken() { return JSONToken.LITERAL_INT; } }
b.注册反序列化
ParserConfig.getGlobalInstance() .putDeserializer(OrderActionEnum.class, new OrderActionEnumDeserializer());
3.序列化和反序列化测试
public void test() { Msg msg = new Msg(); msg.setBody("hello world"); msg.setActionEnum(OrderActionEnum.SUC); String jsonValue = JSON.toJSONString(msg); System.out.println(jsonValue); Msg msg2 = JSON.parseObject(jsonValue, Msg.class); Assert.assertTrue(msg2.getBody() .equals(msg.getBody())); Assert.assertTrue(msg2.getActionEnum() .equals(msg.getActionEnum())); System.out.println(msg2); }
public enum OrderActionEnum { FAIL(1, "FAIL") , SUC(0, "SUC"), ; private int value; private OrderActionEnum(int value, String desc) { this.value = value; } public int getValue() { return value; } public static OrderActionEnum getOrderAction(int value) { for (OrderActionEnum valueEnm: values()) { if (value == valueEnm.getValue()) { return valueEnm; } } return null; } }
public class Msg { private OrderActionEnum actionEnum; private String body; public OrderActionEnum getActionEnum() { return actionEnum; } public void setActionEnum(OrderActionEnum actionEnum) { this.actionEnum = actionEnum; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } }
总之FastJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean,并且虽然FastJson有漏洞,但是也是开发中重要解析库哦!最后大家如果想要了解更多json相关知识,敬请关注奇Q工具网。
推荐阅读: