1.介绍
Gson是一个Java库,可用于将Java对象转换为其JSON表示形式。它还可以用于将JSON字符串转换为等效的Java对象。Gson可以处理任意Java对象,包括您没有源代码的现有对象。
2.参考链接
gitup:https://github.com/google/gson
3.在java工程中的引入
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
4.编写解析器
package net.bingosoft.egw.oapi.tunnel.jsonify;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.*;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Base64;
public class FullRequestAdapter extends TypeAdapter<FullHttpRequest> {
@Override
public void write(JsonWriter out, FullHttpRequest value) throws IOException {
out.beginObject();
out.name("protocol").value(value.protocolVersion().protocolName());
out.name("method").value(value.method().name());
out.name("uri").value(value.uri());
out.name("headers");
out.beginObject();
for (var headerName : value.headers().names()) {
out.name(headerName);
out.beginArray();
for (var headerValue : value.headers().getAll(headerName)) {
out.value(headerValue);
}
out.endArray();
}
out.endObject();
var contentType = value.headers().get(HttpHeaderNames.CONTENT_TYPE);
if (value.content().readableBytes() > 0) {
out.name("body");
var buf = new byte[value.content().readableBytes()];
value.content().readBytes(buf);
if(!(StringUtils.contains(contentType,"text")||StringUtils.contains(contentType,"application"))){
out.value(Base64.getEncoder().encodeToString(buf));
}else{
out.value(new String(buf));
}
buf = null;
}
out.endObject();
}
@Override
public FullHttpRequest read(JsonReader in) throws IOException {
HttpVersion protocolVersion = null;
HttpMethod method = null;
String uri = null;
DefaultHttpHeaders headers = new DefaultHttpHeaders();
ByteBuf body = null;
in.beginObject();
while(in.hasNext()) {
switch (in.nextName()) {
case "protocol":
protocolVersion = HttpVersion.valueOf(in.nextString());
break;
case "method":
method = HttpMethod.valueOf(in.nextString());
break;
case "uri":
uri = in.nextString();
break;
case "headers":
in.beginObject();
while(in.hasNext()) {
var headerName = in.nextName();
in.beginArray();
while(in.hasNext()) {
headers.add(headerName, in.nextString());
}
in.endArray();
}
in.endObject();
break;
case "body":
var contentType = headers.get(HttpHeaderNames.CONTENT_TYPE);
if(!(StringUtils.contains(contentType,"text")||StringUtils.contains(contentType,"application"))){
body = Unpooled.wrappedBuffer(Base64.getDecoder().decode(in.nextString()));
}else{
body = Unpooled.wrappedBuffer(in.nextString().getBytes());
}
break;
}
}
return new DefaultFullHttpRequest(protocolVersion, method, uri, body, headers, new DefaultHttpHeaders());
}
}
5.编写完后如何使用?
- 注册
private Gson gson = new GsonBuilder()
.registerTypeAdapter(FullHttpRequest.class, new FullRequestAdapter())
.create();
- 使用
var jsonString = gson.toJson(new DefaultFullHttpRequest(request.protocolVersion(),
request.method(),
request.uri(),
requestBody,
request.headers(),
new DefaultHttpHeaders()));
gson.fromJson(gson.toJson(responseBody), FullHttpResponse.class);
网友评论