163 lines
4.5 KiB
Java
163 lines
4.5 KiB
Java
package com.xscm.moduleutil.event;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.TypeAdapter;
|
|
import com.google.gson.annotations.JsonAdapter;
|
|
import com.google.gson.stream.JsonReader;
|
|
import com.google.gson.stream.JsonToken;
|
|
import com.google.gson.stream.JsonWriter;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import lombok.Data;
|
|
|
|
/**
|
|
* @Author lxj$
|
|
* @Time $ $
|
|
* @Description 从声望获取到的参数$
|
|
*/
|
|
@Data
|
|
public class MqttBean {
|
|
private String room_id;
|
|
@JsonAdapter(ListBeanListAdapter.class)
|
|
private List<ListBean> list;
|
|
|
|
public String getRoom_id() {
|
|
return room_id == null ? "" : room_id;
|
|
}
|
|
|
|
public void setRoom_id(String room_id) {
|
|
this.room_id = room_id == null ? "" : room_id;
|
|
}
|
|
|
|
public List<ListBean> getList() {
|
|
return list;
|
|
}
|
|
|
|
public void setList(List<ListBean> list) {
|
|
this.list = list;
|
|
}
|
|
|
|
|
|
// 自定义TypeAdapter来处理有时是对象有时是数组的情况
|
|
public static class ListBeanListAdapter extends TypeAdapter<List<ListBean>> {
|
|
@Override
|
|
public void write(JsonWriter out, List<ListBean> value) throws IOException {
|
|
if (value == null) {
|
|
out.nullValue();
|
|
return;
|
|
}
|
|
Gson gson = new Gson();
|
|
out.beginArray();
|
|
for (ListBean item : value) {
|
|
gson.toJson(item, ListBean.class, out);
|
|
}
|
|
out.endArray();
|
|
}
|
|
|
|
@Override
|
|
public List<ListBean> read(JsonReader in) throws IOException {
|
|
List<ListBean> list = new ArrayList<>();
|
|
Gson gson = new Gson();
|
|
|
|
JsonToken token = in.peek();
|
|
if (token == JsonToken.BEGIN_ARRAY) {
|
|
// 正常的数组情况
|
|
in.beginArray();
|
|
while (in.hasNext()) {
|
|
ListBean item = gson.fromJson(in, ListBean.class);
|
|
list.add(item);
|
|
}
|
|
in.endArray();
|
|
} else if (token == JsonToken.BEGIN_OBJECT) {
|
|
// 单个对象的情况,转换为包含一个元素的列表
|
|
ListBean item = gson.fromJson(in, ListBean.class);
|
|
list.add(item);
|
|
} else if (token == JsonToken.NULL) {
|
|
in.nextNull();
|
|
}
|
|
// 其他情况(如字符串、数字等)直接跳过
|
|
|
|
return list;
|
|
}
|
|
}
|
|
|
|
@Data
|
|
public static class ListBean {
|
|
private String text;
|
|
private String gift_picture;
|
|
private String room_id;
|
|
private String fromUserName;
|
|
private String toUserName;
|
|
private String giftName;
|
|
private String roomId;
|
|
private String number;
|
|
|
|
public String getText() {
|
|
return text == null ? "" : text;
|
|
}
|
|
|
|
public void setText(String text) {
|
|
this.text = text == null ? "" : text;
|
|
}
|
|
|
|
public String getGift_picture() {
|
|
return gift_picture == null ? "" : gift_picture;
|
|
}
|
|
|
|
public void setGift_picture(String gift_picture) {
|
|
this.gift_picture = gift_picture == null ? "" : gift_picture;
|
|
}
|
|
|
|
public String getRoom_id() {
|
|
return room_id == null ? "" : room_id;
|
|
}
|
|
|
|
public void setRoom_id(String room_id) {
|
|
this.room_id = room_id == null ? "" : room_id;
|
|
}
|
|
|
|
public String getFromUserName() {
|
|
return fromUserName == null ? "" : fromUserName;
|
|
}
|
|
|
|
public void setFromUserName(String fromUserName) {
|
|
this.fromUserName = fromUserName == null ? "" : fromUserName;
|
|
}
|
|
|
|
public String getToUserName() {
|
|
return toUserName == null ? "" : toUserName;
|
|
}
|
|
|
|
public void setToUserName(String toUserName) {
|
|
this.toUserName = toUserName == null ? "" : toUserName;
|
|
}
|
|
|
|
public String getGiftName() {
|
|
return giftName == null ? "" : giftName;
|
|
}
|
|
|
|
public void setGiftName(String giftName) {
|
|
this.giftName = giftName == null ? "" : giftName;
|
|
}
|
|
|
|
public String getRoomId() {
|
|
return roomId == null ? "" : roomId;
|
|
}
|
|
|
|
public void setRoomId(String roomId) {
|
|
this.roomId = roomId == null ? "" : roomId;
|
|
}
|
|
|
|
public String getNumber() {
|
|
return number == null ? "" : number;
|
|
}
|
|
|
|
public void setNumber(String number) {
|
|
this.number = number == null ? "" : number;
|
|
}
|
|
}
|
|
}
|