Skip to content

Commit

Permalink
Model fixes (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
denis256 authored Mar 10, 2024
1 parent 4f089a8 commit c533ac2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
6 changes: 0 additions & 6 deletions polydata-model/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ java {
}

dependencies {

api(
"com.unidev.platform:unidev-common:$unidevPlatformVersion",
"com.unidev.platform:unidev-logger:$unidevPlatformVersion",
"com.unidev.platform:unidev-exception:$unidevPlatformVersion"
)
implementation "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.unidev.platform.common.DataTransform;
import com.unidev.platform.common.exception.UnidevRuntimeException;
import lombok.Getter;
import lombok.Setter;

Expand Down Expand Up @@ -280,7 +278,7 @@ public <P extends PolyList> P getPolyList(String key, P defaultValue) {
try {
return (P) objectMapper.readValue(objectMapper.writeValueAsString(item), BasicPolyList.class);
} catch (JsonProcessingException e) {
throw new UnidevRuntimeException(e);
throw new RuntimeException(e);
}
}
return (P) item;
Expand Down Expand Up @@ -311,7 +309,7 @@ public <P extends PolyMap> P getPolyMap(String key, P defaultValue) {
try {
return (P) objectMapper.readValue(objectMapper.writeValueAsString(item), BasicPolyMap.class);
} catch (JsonProcessingException e) {
throw new UnidevRuntimeException(e);
throw new RuntimeException(e);
}
}
return (P) item;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.unidev.polydata4.domain;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;
import java.util.Optional;

@AllArgsConstructor
public class DataTransform {

@Getter
@Setter
private final ObjectMapper objectMapper;

public Optional<Map> toMap(Object object) {
try {
return Optional.ofNullable(objectMapper.readValue(objectMapper.writeValueAsBytes(object), Map.class));
} catch (Exception e) {
return Optional.empty();
}
}

public <T> Optional<T> toObject(Object object, Class<T> clazz) {
try {
return Optional.ofNullable(objectMapper.readValue(objectMapper.writeValueAsBytes(object), clazz));
} catch (Exception e) {
return Optional.empty();
}
}

}

0 comments on commit c533ac2

Please sign in to comment.