mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Add JSON user service
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user