Skip to content

Commit

Permalink
Changed Jackson to Gson (fixes #1)
Browse files Browse the repository at this point in the history
- Removed old code
- Loving more Google
  • Loading branch information
Cadiducho committed Feb 20, 2016
1 parent 9e0c677 commit 1dee616
Show file tree
Hide file tree
Showing 28 changed files with 30 additions and 192 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.cadiducho</groupId>
<artifactId>Minegram</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -103,10 +103,10 @@

<!--General libs -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.2</version>
</dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
Expand Down
102 changes: 22 additions & 80 deletions src/com/cadiducho/minegram/TelegramBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import com.cadiducho.minegram.api.*;
import com.cadiducho.minegram.api.exception.TelegramException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.BaseRequest;
Expand All @@ -18,7 +17,10 @@
import java.util.List;
import java.util.Map;
import org.bukkit.plugin.Plugin;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import org.json.JSONObject;


Expand All @@ -27,7 +29,7 @@ public class TelegramBot implements BotAPI {
private final String apiUrl;
private final Plugin bukkitPlugin;

private final ObjectMapper mapper = new ObjectMapper();
private final Gson gson = new Gson();

public TelegramBot(String token, Plugin plugin){
apiUrl = "https://api.telegram.org/bot" + token + "/";
Expand All @@ -39,11 +41,7 @@ public TelegramBot(String token, Plugin plugin){
public User getMe() throws TelegramException {
final String resultBody = handleRequest(Unirest.get(apiUrl + "getMe"));

try {
return mapper.readValue(resultBody, User.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, User.class);
}

@Override
Expand Down Expand Up @@ -87,12 +85,8 @@ private Map<String, Object> safe(String str, Object obj) throws TelegramExceptio

//Check markup style if exists
if(obj != null && str.equals("reply_markup")) {
try {
parameters.put("reply_markup", mapper.writeValueAsString(obj));
return parameters;
} catch (IOException e) {
throw new TelegramException("Could not serialize reply markup!", e);
}
parameters.put("reply_markup", gson.toJson(obj));
return parameters;
}
//Return normal values (check optionals -> null)
if (obj != null) parameters.put(str, obj);
Expand All @@ -119,11 +113,7 @@ public Message sendMessage(Integer chat_id, String text, String parse_mode, Bool

final String resultBody = handleRequest(Unirest.post(apiUrl + "sendMessage").fields(par));

try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}

@Override
Expand All @@ -136,11 +126,7 @@ public Message forwardMessage(Integer chat_id, Integer from_chat_id, Integer mes

final String resultBody = handleRequest(Unirest.get(apiUrl + "forwardMessage").queryString(par));

try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}

@Override
Expand Down Expand Up @@ -174,11 +160,7 @@ public Message sendPhoto(Integer chat_id, Object photo, String caption, Integer
throw new IllegalArgumentException("The photo must be a string or a file!");
}

try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}

@Override
Expand Down Expand Up @@ -214,11 +196,7 @@ public Message sendAudio(Integer chat_id, Object audio, Integer duration, String
throw new IllegalArgumentException("The audio must be a string or a file!");
}

try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}

@Override
Expand Down Expand Up @@ -251,11 +229,7 @@ public Message sendDocument(Integer chat_id, Object document, Integer reply_to_m
throw new IllegalArgumentException("The document must be a string or a file!");
}

try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}

@Override
Expand Down Expand Up @@ -288,11 +262,7 @@ public Message sendSticker(Integer chat_id, Object sticker, Integer reply_to_mes
throw new IllegalArgumentException("The sticker must be a string or a file!");
}

try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}

@Override
Expand Down Expand Up @@ -328,11 +298,7 @@ public Message sendVideo(Integer chat_id, Object video, Integer duration, String
throw new IllegalArgumentException("The video must be a string or a file!");
}

try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}

@Override
Expand Down Expand Up @@ -366,11 +332,7 @@ public Message sendVoice(Integer chat_id, Object voice, Integer duration, Intege
throw new IllegalArgumentException("The voice must be a string or a file!");
}

try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}

@Override
Expand All @@ -390,11 +352,7 @@ public Message sendLocation(Integer chat_id, Float latitude, Float longitude, In
par.putAll(safe("reply_markup", reply_markup));

final String resultBody = handleRequest(Unirest.post(apiUrl + "sendLocation").fields(par));
try {
return mapper.readValue(resultBody, Message.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, Message.class);
}


Expand Down Expand Up @@ -428,11 +386,7 @@ public UserProfilePhotos getUserProfilePhotos(Integer user_id, Integer offset, I
par.putAll(safe("limit", limit));

final String resultBody = handleRequest(Unirest.get(apiUrl + "getUserProfilePhotos").queryString(par));
try {
return mapper.readValue(resultBody, UserProfilePhotos.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, UserProfilePhotos.class);
}

@Override
Expand All @@ -442,11 +396,7 @@ public File getFile(String file_id) throws TelegramException {
par.putAll(safe("file_id", file_id));

final String resultBody = handleRequest(Unirest.get(apiUrl + "getFile").queryString(par));
try {
return mapper.readValue(resultBody, File.class);
} catch (IOException e) {
throw new TelegramException("Could not deserialize response!", e);
}
return gson.fromJson(resultBody, File.class);
}

@Override
Expand All @@ -458,12 +408,8 @@ public List<Update> getUpdates(Integer offset, Integer limit, Integer timeout) t
par.putAll(safe("timeout", timeout));

final String resultBody = handleRequest(Unirest.get(apiUrl + "getUpdates").queryString(par));
try {
return mapper.readValue(resultBody,
mapper.getTypeFactory().constructCollectionType(List.class, Update.class));
} catch (IOException e) {
throw new TelegramException("Could not deserialize response! (getUpdates)", e);
}
Type listType = new TypeToken<ArrayList<Update>>() {}.getType();
return gson.fromJson(resultBody, listType);
}

@Override
Expand All @@ -488,11 +434,7 @@ public Boolean answerInlineQuery(String inlineQueryId, List<InlineQueryResult> r
final Map<String, Object> par = new HashMap<>();
par.putAll(safe("inline_query_id", inlineQueryId));

try {
par.put("results", mapper.writeValueAsString(results));
} catch (IOException e) {
throw new TelegramException("Error occurs while serializing the list of results!", e);
}
par.put("results", gson.toJson(results));

par.putAll(safe("cache_time", cacheTime));
par.putAll(safe("is_personal", isPersonal));
Expand Down
2 changes: 0 additions & 2 deletions src/com/cadiducho/minegram/api/Audio.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.cadiducho.minegram.api;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -17,7 +16,6 @@
*/
@ToString
@Getter @Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Audio {

/**
Expand Down
29 changes: 3 additions & 26 deletions src/com/cadiducho/minegram/api/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

package com.cadiducho.minegram.api;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
Expand All @@ -23,11 +21,8 @@
*/
@ToString
@Getter @Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Chat {

private final ObjectMapper mapper = new ObjectMapper();

/**
* Unique identifier for this user or bot
*/
Expand Down Expand Up @@ -107,33 +102,15 @@ public boolean isGroupChat() {
* by calling {@link Chat#isUser()}.
*
* @return This chat as a {@link User} object
* @throws java.io.IOException
*/
public User asUser() throws IOException {
public User asUser() {
final Map<String, Object> par = new HashMap<>();
par.put("id", id);
par.put("first_name", first_name);
par.put("last_name", last_name);
par.put("username", username);

JSONObject obj = new JSONObject(par);
return mapper.readValue(obj.toString(), User.class);
}

/**
* Returns this chat as a {@link GroupChat}.
* Before invoking this method, check whether this chat is actually a group chat
* by calling {@link Chat#isGroupChat()}.
*
* @return This chat as a {@link GroupChat} object
* @throws java.io.IOException
*/
public GroupChat asGroupChat() throws IOException {
final Map<String, Object> par = new HashMap<>();
par.put("id", id);
par.put("title", title);

JSONObject obj = new JSONObject(par);
return mapper.readValue(obj.toString(), GroupChat.class);
return new Gson().fromJson(obj.toString(), User.class);
}
}
2 changes: 0 additions & 2 deletions src/com/cadiducho/minegram/api/ChosenInlineResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.cadiducho.minegram.api;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -17,7 +16,6 @@
*/
@ToString
@Getter @Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ChosenInlineResult {

/**
Expand Down
2 changes: 0 additions & 2 deletions src/com/cadiducho/minegram/api/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.cadiducho.minegram.api;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -17,7 +16,6 @@
*/
@ToString
@Getter @Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Contact {

/**
Expand Down
2 changes: 0 additions & 2 deletions src/com/cadiducho/minegram/api/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.cadiducho.minegram.api;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -17,7 +16,6 @@
*/
@ToString
@Getter @Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Document {

/**
Expand Down
2 changes: 0 additions & 2 deletions src/com/cadiducho/minegram/api/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.cadiducho.minegram.api;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -20,7 +19,6 @@
*/
@ToString
@Getter @Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class File {

/**
Expand Down
Loading

0 comments on commit 1dee616

Please sign in to comment.