Added IllegalStateException to LiveUserDataMapper to catch getAsJsonObject exception.

Created HttpRequestJsonMapper and HttpResponseJsonMapper for ActionResult gson parser.
This commit is contained in:
kohlerpop1
2024-03-01 16:08:05 -05:00
parent 4e1ab35a60
commit 560a8d7c3b
4 changed files with 52 additions and 3 deletions

View File

@@ -1,15 +1,22 @@
package io.github.jwdeveloper.tiktok.common; package io.github.jwdeveloper.tiktok.common;
import com.google.gson.*; import com.google.gson.*;
import io.github.jwdeveloper.tiktok.http.mappers.*;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.net.http.*;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
@Data @Data
public class ActionResult<T> { public class ActionResult<T> {
private static final Gson gson = new Gson().newBuilder().disableHtmlEscaping()
.registerTypeHierarchyAdapter(HttpResponse.class, new HttpResponseJsonMapper())
.registerTypeHierarchyAdapter(HttpRequest.class, new HttpRequestJsonMapper())
.setPrettyPrinting().create();
private boolean success = true; private boolean success = true;
private T content; private T content;
private String message; private String message;
@@ -93,7 +100,7 @@ public class ActionResult<T> {
public JsonObject toJson() { public JsonObject toJson() {
JsonObject map = new JsonObject(); JsonObject map = new JsonObject();
map.addProperty("success", success); map.addProperty("success", success);
map.add("content", new Gson().toJsonTree(content)); map.add("content", gson.toJsonTree(content));
map.addProperty("message", message); map.addProperty("message", message);
map.add("previous", hasPrevious() ? previous.toJson() : null); map.add("previous", hasPrevious() ? previous.toJson() : null);
return map; return map;
@@ -101,6 +108,6 @@ public class ActionResult<T> {
@Override @Override
public String toString() { public String toString() {
return "ActionResult: "+new Gson().newBuilder().setPrettyPrinting().create().toJson(toJson()); return "ActionResult: "+gson.toJson(toJson());
} }
} }

View File

@@ -0,0 +1,21 @@
package io.github.jwdeveloper.tiktok.http.mappers;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.net.http.HttpRequest;
public class HttpRequestJsonMapper implements JsonSerializer<HttpRequest>
{
@Override
public JsonElement serialize(HttpRequest src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.addProperty("method", src.method());
object.add("timeout", context.serialize(src.timeout().toString()));
object.addProperty("expectContinue", src.expectContinue());
object.add("uri", context.serialize(src.uri()));
object.add("version", context.serialize(src.version().toString()));
object.add("headers", context.serialize(src.headers().map()));
return object;
}
}

View File

@@ -0,0 +1,21 @@
package io.github.jwdeveloper.tiktok.http.mappers;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.net.http.HttpResponse;
public class HttpResponseJsonMapper implements JsonSerializer<HttpResponse>
{
@Override
public JsonElement serialize(HttpResponse src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.addProperty("statusCode", src.statusCode());
object.add("request", context.serialize(src.request()));
object.add("headers", context.serialize(src.headers().map()));
object.add("body", context.serialize(src.body()));
object.add("uri", context.serialize(src.uri().toString()));
object.add("version", context.serialize(src.version().toString()));
return object;
}
}

View File

@@ -65,7 +65,7 @@ public class LiveUserDataMapper
}; };
return new LiveUserData.Response(json, statusEnum, roomId, startTime); return new LiveUserData.Response(json, statusEnum, roomId, startTime);
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException | IllegalStateException e) {
logger.warning("Malformed Json: '"+json+"' - Error Message: "+e.getMessage()); logger.warning("Malformed Json: '"+json+"' - Error Message: "+e.getMessage());
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1); return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, "", -1);
} }