fastjson快速上手(3)

Fastjson toJSONString

Fastjson将java对象序列化为JSON字符串,fastjson提供了一个最简单的入口

package com.alibaba.fastjson;

public abstract class JSON {
    public static String toJSONString(Object object);
}

Sample

import com.alibaba.fastjson.JSON;

Model model = new Model();
model.id = 1001;

String json = JSON.toJSONString(model);

Fastjson writeJSONString

Fastjson在1.2.11版本中,JSON类新增对OutputStream/Writer直接支持。

package com.alibaba.fastjson;

public abstract class JSON {
   public static final int writeJSONString(OutputStream os, // 
                                             Object object, // 
                                             SerializerFeature... features) throws IOException;

    public static final int writeJSONString(OutputStream os, //
                                             Charset charset, //  
                                             Object object, // 
                                             SerializerFeature... features) throws IOException;

   public static final int writeJSONString(Writer os, // 
                                             Object object, // 
                                             SerializerFeature... features) throws IOException;
}

Sample

import com.alibaba.fastjson;
import java.nio.charset.Charset;
class Model {
    public int value;
}

Model model = new Model();
model.value = 1001;

OutputStream os = ...;
JSON.writeJSONString(os, model);
JSON.writeJSONString(os, Charset.from("GB18030"), model);

Writer writer = ...;
JSON.writeJSONString(writer, model);

Fastjson parseObject InputStream

在1.2.11版本中,fastjson新增加了对InputStream的支持支持。

package com.alibaba.fastjson;

public abstract class JSON {
    public static <T> T parseObject(InputStream is, //
                                    Type type, //
                                    Feature... features) throws IOException;

    public static <T> T parseObject(InputStream is, //
                                    Charset charset, //
                                    Type type, //
                                    Feature... features) throws IOException;
}

Sample

import com.alibaba.fastjson;
import java.nio.charset.Charset;
class Model {
    public int value;
}

InputStream is = ...
Model model = JSON.parseObject(is, Model.class);
Model model2 = JSON.parseObject(is, Charset.from("UTF-8"), Model.class);

Fastjson当需要处理超大JSON文本时,需要Stream API,在fastjson-1.1.32版本中开始提供Stream API。

超大JSON数组序列化

如果你的JSON格式是一个巨大的JSON数组,有很多元素,则先调用startArray,然后挨个写入对象,然后调用endArray。

  JSONWriter writer = new JSONWriter(new FileWriter("/tmp/huge.json"));
  writer.startArray();
  for (int i = 0; i < 1000 * 1000; ++i) {
        writer.writeValue(new VO());
  }
  writer.endArray();
  writer.close();

超大JSON对象序列化

如果你的JSON格式是一个巨大的JSONObject,有很多Key/Value对,则先调用startObject,然后挨个写入Key和Value,然后调用endObject。

  JSONWriter writer = new JSONWriter(new FileWriter("/tmp/huge.json"));
  writer.startObject();
  for (int i = 0; i < 1000 * 1000; ++i) {
        writer.writeKey("x" + i);
        writer.writeValue(new VO());
  }
  writer.endObject();
  writer.close();

反序列化

例3

  JSONReader reader = new JSONReader(new FileReader("/tmp/huge.json"));
  reader.startArray();
  while(reader.hasNext()) {
        VO vo = reader.readObject(VO.class);
        // handle vo ...
  }
  reader.endArray();
  reader.close();

例4

  JSONReader reader = new JSONReader(new FileReader("/tmp/huge.json"));
  reader.startObject();
  while(reader.hasNext()) {
        String key = reader.readString();
        VO vo = reader.readObject(VO.class);
        // handle vo ...
  }
  reader.endObject();
  reader.close();

Fastjson DataBind数据库绑定

Fastjson 完全支持数据库绑定方法databind, 也非常易于使用.

Encode

import com.alibaba.fastjson.JSON;

Group group = new Group();
group.setId(0L);
group.setName("admin");

User guestUser = new User();
guestUser.setId(2L);
guestUser.setName("guest");

User rootUser = new User();
rootUser.setId(3L);
rootUser.setName("root");

group.addUser(guestUser);
group.addUser(rootUser);

String jsonString = JSON.toJSONString(group);

System.out.println(jsonString);

Output

{"id":0,"name":"admin","users":[{"id":2,"name":"guest"},{"id":3,"name":"root"}]}

Decode

String jsonString = ...;
Group group = JSON.parseObject(jsonString, Group.class);

 

转载于:https://my.oschina.net/hutaishi/blog/1523654

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值