All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ejlchina.okhttps.fastjson.FastjsonMsgConvertor Maven / Gradle / Ivy

There is a newer version: 3.5.3
Show newest version
package com.ejlchina.okhttps.fastjson;

import com.alibaba.fastjson.JSON;
import com.ejlchina.data.fastjson.FastjsonDataConvertor;
import com.ejlchina.okhttps.ConvertProvider;
import com.ejlchina.okhttps.MsgConvertor;
import com.ejlchina.okhttps.OkHttpsException;
import okio.Okio;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

public class FastjsonMsgConvertor extends FastjsonDataConvertor implements MsgConvertor, ConvertProvider {

	@Override
	public String mediaType() {
		return "application/json; charset={charset}";
	}

	@Override
	public  T toBean(Type type, InputStream in, Charset charset) {
		return JSON.parseObject(toString(in, charset), type);
	}

	@Override
	public MsgConvertor getConvertor() {
		return new FastjsonMsgConvertor();
	}

	@Override
	protected String toString(InputStream in, Charset charset) {
		try {
			return Okio.buffer(Okio.source(in)).readString(charset);
		} catch (IOException e) {
			throw new OkHttpsException("读取文本异常", e);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy