Skip to content

Commit d25741b

Browse files
authored
Merge pull request #67 from jwdeveloper/develop-1.5.1
Fix for mapping of HttpResponse & HttpRequest and more!
2 parents 6178bc2 + 560a8d7 commit d25741b

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

Client/src/main/java/io/github/jwdeveloper/tiktok/common/ActionResult.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package io.github.jwdeveloper.tiktok.common;
22

33
import com.google.gson.*;
4+
import io.github.jwdeveloper.tiktok.http.mappers.*;
45
import lombok.Data;
56
import lombok.experimental.Accessors;
67

8+
import java.net.http.*;
79
import java.util.Optional;
810
import java.util.function.Function;
911

1012
@Data
1113
public class ActionResult<T> {
1214

15+
private static final Gson gson = new Gson().newBuilder().disableHtmlEscaping()
16+
.registerTypeHierarchyAdapter(HttpResponse.class, new HttpResponseJsonMapper())
17+
.registerTypeHierarchyAdapter(HttpRequest.class, new HttpRequestJsonMapper())
18+
.setPrettyPrinting().create();
19+
1320
private boolean success = true;
1421
private T content;
1522
private String message;
@@ -93,14 +100,14 @@ public static <T> ActionResult<T> failure() {
93100
public JsonObject toJson() {
94101
JsonObject map = new JsonObject();
95102
map.addProperty("success", success);
96-
map.add("content", new Gson().toJsonTree(content));
103+
map.add("content", gson.toJsonTree(content));
97104
map.addProperty("message", message);
98105
map.add("previous", hasPrevious() ? previous.toJson() : null);
99106
return map;
100107
}
101108

102109
@Override
103110
public String toString() {
104-
return "ActionResult: "+new Gson().newBuilder().setPrettyPrinting().create().toJson(toJson());
111+
return "ActionResult: "+gson.toJson(toJson());
105112
}
106113
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.jwdeveloper.tiktok.http.mappers;
2+
3+
import com.google.gson.*;
4+
5+
import java.lang.reflect.Type;
6+
import java.net.http.HttpRequest;
7+
8+
public class HttpRequestJsonMapper implements JsonSerializer<HttpRequest>
9+
{
10+
@Override
11+
public JsonElement serialize(HttpRequest src, Type typeOfSrc, JsonSerializationContext context) {
12+
JsonObject object = new JsonObject();
13+
object.addProperty("method", src.method());
14+
object.add("timeout", context.serialize(src.timeout().toString()));
15+
object.addProperty("expectContinue", src.expectContinue());
16+
object.add("uri", context.serialize(src.uri()));
17+
object.add("version", context.serialize(src.version().toString()));
18+
object.add("headers", context.serialize(src.headers().map()));
19+
return object;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.jwdeveloper.tiktok.http.mappers;
2+
3+
import com.google.gson.*;
4+
5+
import java.lang.reflect.Type;
6+
import java.net.http.HttpResponse;
7+
8+
public class HttpResponseJsonMapper implements JsonSerializer<HttpResponse>
9+
{
10+
@Override
11+
public JsonElement serialize(HttpResponse src, Type typeOfSrc, JsonSerializationContext context) {
12+
JsonObject object = new JsonObject();
13+
object.addProperty("statusCode", src.statusCode());
14+
object.add("request", context.serialize(src.request()));
15+
object.add("headers", context.serialize(src.headers().map()));
16+
object.add("body", context.serialize(src.body()));
17+
object.add("uri", context.serialize(src.uri().toString()));
18+
object.add("version", context.serialize(src.version().toString()));
19+
return object;
20+
}
21+
}

Client/src/main/java/io/github/jwdeveloper/tiktok/http/mappers/LiveUserDataMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public LiveUserData.Response map(String json, Logger logger) {
6565
};
6666

6767
return new LiveUserData.Response(json, statusEnum, roomId, startTime);
68-
} catch (JsonSyntaxException e) {
68+
} catch (JsonSyntaxException | IllegalStateException e) {
6969
logger.warning("Malformed Json: '"+json+"' - Error Message: "+e.getMessage());
7070
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
7171
}

0 commit comments

Comments
 (0)