Skip to content

Commit

Permalink
feat: create json mapper
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Nov 29, 2023
1 parent 97f3d0b commit 250f933
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
18 changes: 16 additions & 2 deletions core/src/main/java/expert/os/harperdb/JSONMapper.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package expert.os.harperdb;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

enum JSONMapper {
Expand All @@ -26,12 +29,23 @@ byte[] writeValueAsBytes(Object value) {
}
}

<T> Optional<T> readSingleValue(byte[] bytes, Class<T> valueType) {
<T> Optional<T> readSingleValue(byte[] bytes, Class<T> type) {
try {
if(bytes == null || bytes.length == 0){
return Optional.empty();
}
return Optional.of(mapper.readValue(bytes, valueType));
return Optional.of(mapper.readValue(bytes, type));
} catch (IOException exception) {
throw new HarperDBException("There is an issue to read from json value", exception);
}
}

<T> List<T> readValue(byte[] bytes, Class<T> type) {
try {
if(bytes == null || bytes.length == 0){
return Collections.emptyList();
}
return mapper.readValue(bytes, new TypeReference<>() {});
} catch (IOException exception) {
throw new HarperDBException("There is an issue to read from json value", exception);
}
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/expert/os/harperdb/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -105,6 +107,23 @@ <T> Optional<T> singleResult(Operation operation, Class<T> type){
}
}

<T> List<T> result(Operation operation, Class<T> type){
HttpRequest request = createRequest()
.POST(ofByteArray(INSTANCE.writeValueAsBytes(operation)))
.build();
try {
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
if(HttpStatus.OK.isEquals(response)){
byte[] body = response.body();
return INSTANCE.readValue(body, type);
} else {
return Collections.emptyList();
}
} catch (IOException| InterruptedException e) {
throw new HarperDBException("There is an issue to execute the operation: " + operation + "message: ", e);
}
}


HttpRequest.Builder createRequest() {
return HttpRequest.newBuilder()
Expand Down

0 comments on commit 250f933

Please sign in to comment.