Skip to content

feat: Support for class io.vertx.core.json.JsonArray #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.quarkiverse.hibernate.types.json.JsonType;
import io.quarkiverse.hibernate.types.json.JsonTypes;
import io.quarkus.runtime.annotations.RegisterForReflection;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

@TypeDef(name = JsonTypes.JSON, typeClass = JsonType.class)
Expand All @@ -33,6 +34,10 @@ public class MyEntity {
@Column(name = "P_VERTX", columnDefinition = JsonTypes.JSON)
private JsonObject vertxObject;

@Type(type = JsonTypes.JSON)
@Column(name = "P_VERTX_ARRAY", columnDefinition = JsonTypes.JSON)
private JsonArray vertxArray;

public String getId() {
return id;
}
Expand Down Expand Up @@ -64,4 +69,12 @@ public JsonObject getVertxObject() {
public void setVertxObject(JsonObject vertxObject) {
this.vertxObject = vertxObject;
}

public JsonArray getVertxArray() {
return vertxArray;
}

public void setVertxArray(JsonArray vertxArray) {
this.vertxArray = vertxArray;
}
}
8 changes: 4 additions & 4 deletions integration-tests/simple/src/main/resources/import.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INSERT INTO MyEntity(ID, P_STRING, P_JSONB, P_VERTX)
VALUES ('1', '{"id":"1","name":"test1"}', '{"id":"1","name":"test1"}', '{"id":"1","name":"test1"}');
INSERT INTO MyEntity(ID, P_STRING, P_JSONB, P_VERTX)
VALUES ('2', '{"id":"2","name":"test2"}', '{"id":"2","name":"test2"}', '{"id":"2","name":"test2"}');
INSERT INTO MyEntity(ID, P_STRING, P_JSONB, P_VERTX, P_VERTX_ARRAY)
VALUES ('1', '{"id":"1","name":"test1"}', '{"id":"1","name":"test1"}', '{"id":"1","name":"test1"}', '[{"id":"1","name":"test1"}]');
INSERT INTO MyEntity(ID, P_STRING, P_JSONB, P_VERTX, P_VERTX_ARRAY)
VALUES ('2', '{"id":"2","name":"test2"}', '{"id":"2","name":"test2"}', '{"id":"2","name":"test2"}', '[{"id":"2","name":"test2"}]');
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ protected void shouldRetrieveEntity(@NotNull String pSourceName, @NotNull String
assertEquals("test" + pEntityID, entity.getJsonObject("jsonb").getString("name"));
assertEquals(pEntityID, entity.getJsonObject("vertxObject").getString("id"));
assertEquals("test" + pEntityID, entity.getJsonObject("vertxObject").getString("name"));
assertEquals(pEntityID, entity.getJsonArray("vertxArray").getJsonObject(0).getString("id"));
assertEquals("test" + pEntityID, entity.getJsonArray("vertxArray").getJsonObject(0).getString("name"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public ObjectMapper get() {
quarkusHibernateTypesModule.addDeserializer(jsonObjClass, new VertxJsonObjectMapper.Deserializer());
}

//noinspection rawtypes add custom JsonArray Mapper
Class jsonArrClass = ReflectionUtils.getClassOrNull("io.vertx.core.json.JsonArray");
if (jsonArrClass != null) {
quarkusHibernateTypesModule.addSerializer(jsonArrClass, new VertxJsonArrayMapper.Serializer());
quarkusHibernateTypesModule.addDeserializer(jsonArrClass, new VertxJsonArrayMapper.Deserializer());
}

objectMapper.registerModule(quarkusHibernateTypesModule);
return objectMapper;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkiverse.hibernate.types.custommappers;

import java.io.IOException;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;

import io.vertx.core.json.JsonArray;

/**
* ObjectMapper to be able to map VertX JsonArray
*/
abstract class VertxJsonArrayMapper {

public static class Serializer extends JsonSerializer<JsonArray> {
@Override
public void serialize(JsonArray value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeRaw(value.encode());
}
}

public static class Deserializer extends JsonDeserializer<JsonArray> {
@Override
public JsonArray deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return new JsonArray(p.readValueAsTree().toString());
}
}

}