Add JSON user service

This commit is contained in:
Draycia committed 2020-07-02 06:55:42 -07:00
1 parent 06470f46da
commit fc8b11c83c
5 files changed
+184 -18

No files matched your search

@@ -0,0 +1,48 @@
package net.draycia.simplechat.util;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.draycia.simplechat.SimpleChat;
import net.draycia.simplechat.channels.ChatChannel;
import java.io.IOException;
public class ChatChannelAdapter extends TypeAdapter<ChatChannel> {
private SimpleChat simpleChat;
public ChatChannelAdapter(SimpleChat simpleChat) {
this.simpleChat = simpleChat;
}
@Override
public void write(JsonWriter out, ChatChannel value) throws IOException {
if (value != null) {
out.beginObject();
out.name("name").value(value.getName());
out.endObject();
}
}
@Override
public ChatChannel read(JsonReader in) throws IOException {
ChatChannel channel;
in.beginObject();
if (in.hasNext()) {
String key = in.nextName();
String value = in.nextString();
channel = simpleChat.getChannel(value);
} else {
channel = simpleChat.getDefaultChannel();
}
in.endObject();
return channel;
}
}