Created
June 16, 2023 17:11
-
-
Save keksipurkki/49c5b050e7f6fe382eb7f0a006e78754 to your computer and use it in GitHub Desktop.
Vert.x and Jackson Databind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonValue; | |
import io.vertx.core.json.Json; | |
import io.vertx.core.json.JsonArray; | |
import io.vertx.core.json.JsonObject; | |
import io.vertx.core.json.jackson.DatabindCodec; | |
import lombok.extern.slf4j.Slf4j; | |
import java.util.List; | |
import java.util.Map; | |
@Slf4j | |
public class Bug { | |
static { | |
var objectMapper = DatabindCodec.mapper(); | |
objectMapper | |
.addMixIn(JsonObject.class, JsonObjectMixin.class) | |
.addMixIn(JsonArray.class, JsonArrayMixin.class); | |
var prettyMapper = DatabindCodec.prettyMapper(); | |
prettyMapper | |
.addMixIn(JsonObject.class, JsonObjectMixin.class) | |
.addMixIn(JsonArray.class, JsonArrayMixin.class); | |
} | |
public static void main(String... args) { | |
String json = "{\n" + | |
" \"result\": [\n" + | |
" {\n" + | |
" \"id\": \"aaa\",\n" + | |
" \"zone_id\": \"bbb\",\n" + | |
" \"meta\": {\n" + | |
" \"auto_added\": false,\n" + | |
" \"managed_by_apps\": false,\n" + | |
" \"managed_by_argo_tunnel\": false,\n" + | |
" \"source\": \"primary\"\n" + | |
" },\n" + | |
" \"comment\": null,\n" + | |
" \"tags\": [],\n" + | |
" \"created_on\": \"2023-05-09T11:43:07.088802Z\",\n" + | |
" \"modified_on\": \"2023-05-09T11:43:07.088802Z\"\n" + | |
" },\n" + | |
" {\n" + | |
" \"id\": \"ccc\",\n" + | |
" \"zone_id\": \"ddd\",\n" + | |
" \"meta\": {\n" + | |
" \"auto_added\": false,\n" + | |
" \"managed_by_apps\": false,\n" + | |
" \"managed_by_argo_tunnel\": false,\n" + | |
" \"origin_worker_id\": \"eee\",\n" + | |
" \"source\": \"primary\"\n" + | |
" },\n" + | |
" \"comment\": null,\n" + | |
" \"tags\": [],\n" + | |
" \"created_on\": \"2023-05-13T12:30:06.837565Z\",\n" + | |
" \"modified_on\": \"2023-05-13T12:30:06.837565Z\"\n" + | |
" }\n" + | |
" ],\n" + | |
" \"success\": true,\n" + | |
" \"errors\": [],\n" + | |
" \"messages\": [],\n" + | |
" \"result_info\": {\n" + | |
" \"page\": 1,\n" + | |
" \"per_page\": 100,\n" + | |
" \"count\": 4,\n" + | |
" \"total_count\": 4,\n" + | |
" \"total_pages\": 1\n" + | |
" }\n" + | |
"}"; | |
Object object = Json.decodeValue(json); | |
if (object instanceof JsonObject) { | |
log.debug("json is JsonObject"); | |
} | |
try { | |
JsonObject jsonObject = Json.decodeValue(json, JsonObject.class); | |
log.debug("deserialized json: {}", jsonObject); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
static abstract class JsonArrayMixin { | |
@JsonCreator | |
JsonArrayMixin(List<?> list) { | |
} | |
@JsonValue | |
abstract List<?> getList(); | |
} | |
static abstract class JsonObjectMixin { | |
@JsonCreator | |
JsonObjectMixin(Map<String, Object> map) { | |
} | |
@JsonValue | |
abstract Map<String, Object> getMap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment