Skip to content
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v24.13.0
lts/*
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.reactivecommons.async.commons.converters.json;

import io.cloudevents.jackson.JsonFormat;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

public class DefaultObjectMapperSupplier implements ObjectMapperSupplier {

@Override
public ObjectMapper get() {
public JsonMapper get() {
return JsonMapper.builder()
.addModule(JsonFormat.getCloudEventJacksonModule())
.findAndAddModules()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.reactivecommons.async.commons.converters.MessageConverter;
import org.reactivecommons.async.commons.exceptions.MessageConversionException;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

@RequiredArgsConstructor
public abstract class JacksonMessageConverter implements MessageConverter {
Expand All @@ -19,26 +19,26 @@ public abstract class JacksonMessageConverter implements MessageConverter {
public static final String APPLICATION_CLOUD_EVENT_JSON = "application/cloudevents+json";
public static final String APPLICATION_JSON = "application/json";

protected final ObjectMapper objectMapper;
protected final JsonMapper jsonMapper;

@Override
public <T> AsyncQuery<T> readAsyncQuery(Message message, Class<T> bodyClass) {
final AsyncQueryJson asyncQueryJson = readValue(message, AsyncQueryJson.class);
final T value = objectMapper.treeToValue(asyncQueryJson.getQueryData(), bodyClass);
final T value = jsonMapper.treeToValue(asyncQueryJson.getQueryData(), bodyClass);
return new AsyncQuery<>(asyncQueryJson.getResource(), value);
}

@Override
public <T> DomainEvent<T> readDomainEvent(Message message, Class<T> bodyClass) {
final DomainEventJson domainEventJson = readValue(message, DomainEventJson.class);
final T value = objectMapper.treeToValue(domainEventJson.getData(), bodyClass);
final T value = jsonMapper.treeToValue(domainEventJson.getData(), bodyClass);
return new DomainEvent<>(domainEventJson.getName(), domainEventJson.getEventId(), value);
}

@Override
public <T> Command<T> readCommand(Message message, Class<T> bodyClass) {
final CommandJson commandJson = readValue(message, CommandJson.class);
final T value = objectMapper.treeToValue(commandJson.getData(), bodyClass);
final T value = jsonMapper.treeToValue(commandJson.getData(), bodyClass);
return new Command<>(commandJson.getName(), commandJson.getCommandId(), value);
}

Expand All @@ -50,7 +50,7 @@ public CloudEvent readCloudEvent(Message message) {
@Override
public <T> T readValue(Message message, Class<T> valueClass) {
try {
return objectMapper.readValue(message.getBody(), valueClass);
return jsonMapper.readValue(message.getBody(), valueClass);
} catch (Exception e) {
throw new MessageConversionException(FAILED_TO_CONVERT_MESSAGE_CONTENT, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.reactivecommons.async.commons.converters.json;

import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import java.util.function.Supplier;

public interface ObjectMapperSupplier extends Supplier<ObjectMapper> {
public interface ObjectMapperSupplier extends Supplier<JsonMapper> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import java.time.LocalDateTime;
import java.util.Date;
Expand All @@ -15,30 +15,29 @@ class DefaultObjectMapperSupplierTest {

private final DefaultObjectMapperSupplier defaultObjectMapperSupplier = new DefaultObjectMapperSupplier();


@Test
void shouldMapWithUnknownProperties() {
ObjectMapper objectMapper = defaultObjectMapperSupplier.get();
JsonMapper jsonMapper = defaultObjectMapperSupplier.get();

SampleClassExtra base = new SampleClassExtra("23", "one", new Date(), 45L);
final String serialized = objectMapper.writeValueAsString(base);
final String serialized = jsonMapper.writeValueAsString(base);

final SampleClass result = objectMapper.readValue(serialized, SampleClass.class);
final SampleClass result = jsonMapper.readValue(serialized, SampleClass.class);

assertThat(result).usingRecursiveComparison().isEqualTo(base);
}

@Test
void shouldSerializeAndDeserializeLocalDateTime() {
ObjectMapper objectMapper = defaultObjectMapperSupplier.get();
JsonMapper jsonMapper = defaultObjectMapperSupplier.get();

LocalDateTime now = LocalDateTime.of(2025, 12, 10, 14, 30, 0);
SampleClassWithLocalDateTime sample = new SampleClassWithLocalDateTime("123", "Test", now);

final String serialized = objectMapper.writeValueAsString(sample);
final String serialized = jsonMapper.writeValueAsString(sample);
assertThat(serialized).contains("2025-12-10");

final SampleClassWithLocalDateTime result = objectMapper.readValue(serialized, SampleClassWithLocalDateTime.class);
final SampleClassWithLocalDateTime result = jsonMapper.readValue(serialized, SampleClassWithLocalDateTime.class);

assertThat(result.getId()).isEqualTo("123");
assertThat(result.getName()).isEqualTo("Test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import org.reactivecommons.async.commons.exceptions.MessageConversionException;
import org.reactivecommons.async.kafka.KafkaMessage;
import org.reactivecommons.async.kafka.KafkaMessage.KafkaMessageProperties;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class KafkaJacksonMessageConverter extends JacksonMessageConverter {

public KafkaJacksonMessageConverter(ObjectMapper objectMapper) {
super(objectMapper);
public KafkaJacksonMessageConverter(JsonMapper jsonMapper) {
super(jsonMapper);
}

@Override
Expand All @@ -26,7 +26,7 @@ public Message toMessage(Object object) {
}
byte[] bytes;
try {
String jsonString = this.objectMapper.writeValueAsString(object);
String jsonString = this.jsonMapper.writeValueAsString(object);
bytes = jsonString.getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
throw new MessageConversionException(FAILED_TO_CONVERT_MESSAGE_CONTENT, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.reactivecommons.async.commons.communications.Message;
import org.reactivecommons.async.commons.converters.json.DefaultObjectMapperSupplier;
import org.reactivecommons.async.commons.converters.json.ObjectMapperSupplier;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import java.net.URI;
import java.time.OffsetDateTime;
Expand All @@ -23,13 +23,13 @@

class KafkaJacksonMessageConverterTest {
private static KafkaJacksonMessageConverter converter;
private static ObjectMapper objectMapper;
private static JsonMapper jsonMapper;

@BeforeAll
static void setUp() {
ObjectMapperSupplier supplier = new DefaultObjectMapperSupplier();
objectMapper = supplier.get();
converter = new KafkaJacksonMessageConverter(objectMapper);
jsonMapper = supplier.get();
converter = new KafkaJacksonMessageConverter(jsonMapper);
}

@Test
Expand Down Expand Up @@ -61,21 +61,21 @@ void shouldSerializeCloudEvent() {
.withType("test")
.withDataContentType("application/json")
.withTime(dateTime)
.withData(objectMapper.writeValueAsBytes(event))
.withData(jsonMapper.writeValueAsBytes(event))
.build();

String expectedJson = "{\"specversion\":\"1.0\",\"id\":\"" + id +
"\",\"source\":\"https://reactivecommons.org/events\",\"type\":\"test\"," +
"\"datacontenttype\":\"application/json\",\"time\":\"" + dateTime +
"\",\"data\":{\"name\":\"name\",\"age\":1}}";
JsonCloudEvent expectedJsonNode = objectMapper.readValue(expectedJson, JsonCloudEvent.class);
JsonCloudEvent expectedJsonNode = jsonMapper.readValue(expectedJson, JsonCloudEvent.class);
// Act
Message message = converter.toMessage(testCloudEvent);
// Assert
assertEquals("test", message.getProperties().getTopic());
assertEquals(id, message.getProperties().getKey());
assertEquals("application/cloudevents+json", message.getProperties().getContentType());
JsonCloudEvent receivedJsonNode = objectMapper.readValue(new String(message.getBody()), JsonCloudEvent.class);
JsonCloudEvent receivedJsonNode = jsonMapper.readValue(new String(message.getBody()), JsonCloudEvent.class);
assertEquals(expectedJsonNode, receivedJsonNode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import org.reactivecommons.async.commons.communications.Message;
import org.reactivecommons.async.commons.converters.json.JacksonMessageConverter;
import org.reactivecommons.async.rabbit.RabbitMessage;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import java.nio.charset.StandardCharsets;

public class RabbitJacksonMessageConverter extends JacksonMessageConverter {

public RabbitJacksonMessageConverter(ObjectMapper objectMapper) {
super(objectMapper);
public RabbitJacksonMessageConverter(JsonMapper jsonMapper) {
super(jsonMapper);
}

@Override
Expand All @@ -20,7 +20,7 @@ public Message toMessage(Object object) {
return rabbitMessage;
}
byte[] bytes;
String jsonString = this.objectMapper.writeValueAsString(object);
String jsonString = this.jsonMapper.writeValueAsString(object);
bytes = jsonString.getBytes(StandardCharsets.UTF_8);
RabbitMessage.RabbitMessageProperties props = new RabbitMessage.RabbitMessageProperties();
if (object instanceof CloudEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import reactor.rabbitmq.SendOptions;
import reactor.rabbitmq.Sender;
import reactor.test.StepVerifier;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import java.time.Duration;
import java.util.List;
Expand Down Expand Up @@ -326,7 +326,7 @@ private void senderMock() {

private void mockReply() {
Message message = mock(Message.class);
ObjectMapper mapper = new ObjectMapper();
JsonMapper mapper = new JsonMapper();
when(message.getBody()).thenReturn(mapper.writeValueAsString(new DummyMessage()).getBytes());
final Sinks.One<Message> processor = Sinks.one();
processor.tryEmitValue(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class ReactiveMessageSenderTest {

private ReactiveMessageSender messageSender;

private final JsonMapper objectMapper = new JsonMapper();
private final JsonMapper jsonMapper = new JsonMapper();

@Mock
private Sender sender;

@Spy
private final MessageConverter messageConverter = new RabbitJacksonMessageConverter(objectMapper);
private final MessageConverter messageConverter = new RabbitJacksonMessageConverter(jsonMapper);

@Mock
private final SendOptions sendOptions = new SendOptions();
Expand All @@ -56,7 +56,7 @@ void init() {
});
when(sender.send(any(Publisher.class))).then(invocation -> {
final Flux<OutboundMessage> argument = invocation.getArgument(0);
argument.subscribe(); // Suscribirse para inicializar el sink
argument.subscribe(); // Subscribe to initialize the sink
return Mono.empty();
});
String sourceApplication = "TestApp";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.reactivecommons.async.commons.communications.Message;
import org.reactivecommons.async.commons.converters.json.DefaultObjectMapperSupplier;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import java.net.URI;
import java.util.Date;
Expand All @@ -23,12 +23,12 @@
class JacksonMessageConverterTest {

private static RabbitJacksonMessageConverter converter;
private static ObjectMapper objectMapper;
private static JsonMapper jsonMapper;

@BeforeAll
static void setUp() {
objectMapper = new DefaultObjectMapperSupplier().get();
converter = new RabbitJacksonMessageConverter(objectMapper);
jsonMapper = new DefaultObjectMapperSupplier().get();
converter = new RabbitJacksonMessageConverter(jsonMapper);
}

@Test
Expand All @@ -42,15 +42,15 @@ void toMessage() {
void toMessageWhenDataIsNull() {
final Message message = converter.toMessage(null);

final JsonNode jsonNode = objectMapper.readTree(message.getBody());
final JsonNode jsonNode = jsonMapper.readTree(message.getBody());
assertThat(jsonNode.isNull()).isTrue();
}

@Test
void toMessageWhenDataIsEmpty() {
final Message message = converter.toMessage("");

final JsonNode jsonNode = objectMapper.readTree(message.getBody());
final JsonNode jsonNode = jsonMapper.readTree(message.getBody());
assertThat(jsonNode.asString()).isEmpty();
}

Expand All @@ -71,7 +71,7 @@ void readCloudEvent() {
.withSource(URI.create("https://spring.io/foos"))//
.withType("command")
.withData("application/json", JsonCloudEventData.wrap(
objectMapper.valueToTree(new SampleClass("35", "name1", date)))
jsonMapper.valueToTree(new SampleClass("35", "name1", date)))
)
.build();
Message message = converter.toMessage(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import reactor.rabbitmq.ExchangeSpecification;
import reactor.rabbitmq.Receiver;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -65,7 +65,7 @@ public abstract class ListenerReporterTestSuperClass {
protected final CustomReporter errorReporter = mock(CustomReporter.class);
protected final Semaphore semaphore = new Semaphore(0);
protected final Semaphore successSemaphore = new Semaphore(0);
private final ObjectMapper mapper = new DefaultObjectMapperSupplier().get();
private final JsonMapper mapper = new DefaultObjectMapperSupplier().get();
private final Receiver receiver = mock(Receiver.class);
protected final ReactiveMessageListener reactiveMessageListener = new ReactiveMessageListener(
receiver, topologyCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void serialize(CloudEvent value, JsonGenerator gen, SerializationContext
if (value.getData() != null) {
CloudEventData data = value.getData();
if (data instanceof JsonCloudEventData jsonCloudEventData) {
gen.writePOJOProperty("data", jsonCloudEventData.getNode());
gen.writePOJOProperty("data", jsonCloudEventData.node());
} else {
byte[] dataBytes = data.toBytes();
String contentType = value.getDataContentType();
Expand All @@ -100,10 +100,8 @@ public void serialize(CloudEvent value, JsonGenerator gen, SerializationContext
gen.writeName("data_base64");
gen.writeBinary(dataBytes);
} else if (JsonFormat.dataIsJsonContentType(contentType)) {
// TODO really bad b/c it allocates stuff, is there another solution out there?
char[] dataAsString = new String(dataBytes, StandardCharsets.UTF_8).toCharArray();
gen.writeName("data");
gen.writeRawValue(dataAsString, 0, dataAsString.length);
gen.writeRawValue(new String(dataBytes, StandardCharsets.UTF_8));
} else {
gen.writeName("data");
gen.writeUTF8String(dataBytes, 0, dataBytes.length);
Expand Down
Loading
Loading