Skip to content

Commit

Permalink
fixes #56 add a test case to serialize the ProduceRequest into a JSON (
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu authored May 21, 2021
1 parent 841d073 commit 6c816f3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kafka-producer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.networknt.kafka.producer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import org.junit.jupiter.api.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Test the JSON serialization for the object with the Jackson ObjectMapper
*
* @author Steve Hu
*/
public class ProduceRequestTest {
@Test
public void testToJson() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
JsonNode jsonNode = mapper.readTree(json);
ProduceRecord produceRecord = new ProduceRecord();
produceRecord.setValue(Optional.of(jsonNode));
List<ProduceRecord> records = new ArrayList<>();
records.add(produceRecord);
ProduceRequest produceRequest = new ProduceRequest();
produceRequest.setRecords(records);
String result = mapper.writeValueAsString(produceRequest);
System.out.println(result);

}

}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down

0 comments on commit 6c816f3

Please sign in to comment.