Skip to content

Commit

Permalink
feat: add default mapper and some doc
Browse files Browse the repository at this point in the history
  • Loading branch information
NyouB committed Feb 27, 2021
1 parent 2825a2b commit 5e82731
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Changelog
---

### 1.1.0
- add method to get a default configured mapper
#### 1.0.0
- first release
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

A Json exporter to serializer deserialize jmonkey engine object using the existing interfaces

## MAVEN/GRADLE

Add this repository : https://dl.bintray.com/exratio/jme-tool

Maven:
```xml
<dependency>
<groupId>fr.exratio</groupId>
<artifactId>JME-JsonExporter</artifactId>
<version>1.1.0</version>
<type>pom</type>
</dependency>
```
Gradle
> implementation 'fr.exratio:JME-JsonExporter:1.1.0'

## Quickstart example

> ObjectMapper mapper = ObjectMapperHelper.defaultMapper();
> mapper.writeValue(new File("savable.json"), new Box);
> Box mybox = mapper.readValue(new File("savable.json"), Box.class);
## Default Configuration
A default working ObjectMapper is provided via the ObjectMapperHelper class. It works out of the box for all Savable implementation declared
in com.jme3 package.
Expand All @@ -28,7 +52,7 @@ Some example:

### Serialization

First declare a jackson Serializer or use the default provided Serializer (SavableJsonSerializer.class)
First declare a jackson Serializer or use the default provided Serializer (``SavableJsonSerializer.class)

> public class SavableJsonSerializer extends JsonSerializer< Savable > {
>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>fr.exratio</groupId>
<artifactId>JME-JsonExporter</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<distributionManagement>
<repository>
<id>bintray-exratio-jme-tool</id>
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/fr/exratio/jme/exporter/ObjectMapperHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public class ObjectMapperHelper {

private ObjectMapperHelper() {}

public static ObjectMapper defaultMapper() {
return configure(null, null);
}

/**
* This method help to configure (or to create if the object mapper parameter provided is null) an
* ObjectMapper that will serialize and deserialize savable and scenes out of the box.
Expand All @@ -36,6 +40,8 @@ private ObjectMapperHelper() {}
public static ObjectMapper configure(
ObjectMapper objectMapper, List<Class> polymorphClasses, String... acceptedPackages) {
ObjectMapper result = objectMapper != null ? objectMapper : new ObjectMapper();

//Allow subtype of those classes to be serialize/deserialize
Builder builder =
BasicPolymorphicTypeValidator.builder()
.allowIfSubType(Map.class)
Expand All @@ -50,11 +56,14 @@ public static ObjectMapper configure(
PolymorphicTypeValidator ptv = builder.build();

result.activateDefaultTyping(ptv);
//only serialize public field or public getter
result.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY);
result.setVisibility(PropertyAccessor.FIELD, Visibility.PUBLIC_ONLY);


String[] scannedPackages = ArrayUtils.add(acceptedPackages, JME_SAVABLE_PACKAGE);
;
//Scan packages to find Savable implementation and setup the SavableMixIn which define the
//way of encoding the class and the deserializer to use
try (ScanResult scanResult =
new ClassGraph().enableClassInfo().acceptPackages(scannedPackages).scan()) {
ClassInfoList classes = scanResult.getClassesImplementing(Savable.class.getName());
Expand Down

0 comments on commit 5e82731

Please sign in to comment.