-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/javers/javers/issues/1350
added PathTypeAdapter for java.nio.file.Path
- Loading branch information
1 parent
b263ea8
commit 8713f7c
Showing
11 changed files
with
155 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
javers-core/src/main/java/org/javers/core/json/AbstractJsonTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.javers.core.json; | ||
|
||
public interface AbstractJsonTypeAdapter { | ||
} |
44 changes: 44 additions & 0 deletions
44
javers-core/src/main/java/org/javers/core/json/BasicStringSuperTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.javers.core.json; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public abstract class BasicStringSuperTypeAdapter<T> implements JsonAdvancedTypeAdapter<T> { | ||
|
||
/** | ||
* Example serialization for LocalDateTime: | ||
* <pre> | ||
* public String serialize(LocalDateTime sourceValue) { | ||
* return ISO_DATE_TIME_FORMATTER.print(sourceValue); | ||
* } | ||
* </pre> | ||
* @param sourceValue not null | ||
*/ | ||
public abstract String serialize(T sourceValue); | ||
|
||
/** | ||
* Example deserialization for LocalDateTime: | ||
* <pre> | ||
* public LocalDateTime deserialize(String serializedValue) { | ||
* return ISO_DATE_TIME_FORMATTER.parseLocalDateTime(serializedValue); | ||
* } | ||
* </pre> | ||
* | ||
* @param serializedValue not null | ||
*/ | ||
public abstract T deserialize(String serializedValue); | ||
|
||
@Override | ||
public T fromJson(JsonElement json, Type typeOfT, JsonDeserializationContext jsonDeserializationContext ) { | ||
return deserialize(json.getAsJsonPrimitive().getAsString()); | ||
} | ||
|
||
@Override | ||
public JsonElement toJson(T sourceValue, Type typeOfT, JsonSerializationContext context) { | ||
return new JsonPrimitive(serialize(sourceValue)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
javers-core/src/main/java/org/javers/core/json/typeadapter/util/PathTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.javers.core.json.typeadapter.util; | ||
|
||
import org.javers.core.json.BasicStringSuperTypeAdapter; | ||
import java.nio.file.Path; | ||
|
||
public class PathTypeAdapter extends BasicStringSuperTypeAdapter<Path> { | ||
|
||
@Override | ||
public String serialize(Path sourceValue) { | ||
return sourceValue.toString(); | ||
} | ||
|
||
@Override | ||
public Path deserialize(String serializedValue) { | ||
return Path.of(serializedValue); | ||
} | ||
|
||
@Override | ||
public Class<Path> getTypeSuperclass() { | ||
return Path.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
javers-core/src/test/groovy/org/javers/core/cases/PathInaccessibleObjectExceptionTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import org.javers.core.JaversBuilder | ||
import org.javers.core.metamodel.annotation.Id | ||
import spock.lang.Specification | ||
|
||
import java.nio.file.Path | ||
|
||
/** | ||
* case https://github.com/javers/javers/issues/1350 | ||
*/ | ||
class PathInaccessibleObjectExceptionTest extends Specification { | ||
|
||
class Entity { | ||
@Id int id | ||
Path path | ||
} | ||
|
||
def "should support java.nio.file.Path" () { | ||
given: | ||
def javers = JaversBuilder.javers().build() | ||
|
||
when: | ||
def diff = javers.compare( | ||
new Entity(id:1, path: Path.of("foo")), | ||
new Entity(id:1, path: Path.of("bar"))) | ||
|
||
then: | ||
println(diff) | ||
diff.changes.size() == 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters