Skip to content

Commit f821f83

Browse files
committed
Refactors Objectmapper with a base class to generate new mappers from core libs
1 parent bf02fd6 commit f821f83

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>io.digitalstate.stix</groupId>
77
<artifactId>stix</artifactId>
8-
<version>v0.8.0</version>
8+
<version>v0.8.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>STIX 2</name>

src/main/java/io/digitalstate/stix/json/StixParsers.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.databind.jsontype.NamedType;
5+
import com.fasterxml.jackson.databind.module.SimpleModule;
56
import com.fasterxml.jackson.datatype.guava.GuavaModule;
67
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
78
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
89
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
910
import io.digitalstate.stix.bundle.Bundle;
1011
import io.digitalstate.stix.bundle.BundleObject;
1112
import io.digitalstate.stix.bundle.BundleableObject;
13+
import io.digitalstate.stix.common.StixInstant;
1214
import io.digitalstate.stix.coo.extension.types.*;
1315
import io.digitalstate.stix.coo.objects.*;
1416
import io.digitalstate.stix.coo.objects.Process;
@@ -24,19 +26,24 @@
2426

2527
public class StixParsers {
2628

27-
private static ObjectMapper jsonMapper = new ObjectMapper().registerModule(new ParameterNamesModule())
28-
.registerModule(new Jdk8Module())
29-
.registerModule(new JavaTimeModule())
30-
.registerModule(new GuavaModule());
29+
private static ObjectMapper jsonMapper = generateJsonMapperBase();
3130

32-
public static ObjectMapper getJsonMapper() {
33-
return jsonMapper;
31+
/**
32+
* Generates a Base Object Mapper with some generic modules.
33+
* @return
34+
*/
35+
public static ObjectMapper generateJsonMapperBase() {
36+
return new ObjectMapper()
37+
.registerModule(new ParameterNamesModule())
38+
.registerModule(new Jdk8Module())
39+
.registerModule(new JavaTimeModule())
40+
.registerModule(new GuavaModule());
3441
}
3542

3643
public static ObjectMapper getJsonMapper(boolean withSubTypeMappings, NamedType... additionalNamedTypes) {
3744
//@TODO Add config to only serialize/deserialize that have @JsonProperty() annotation
3845
if (withSubTypeMappings) {
39-
return registerBundleMapperSubTypes(additionalNamedTypes);
46+
return registerBundleMapperSubTypes(jsonMapper, additionalNamedTypes);
4047
} else {
4148
return jsonMapper;
4249
}
@@ -51,7 +58,7 @@ public static void setJsonMapper(ObjectMapper objectMapper) {
5158
jsonMapper = objectMapper;
5259
}
5360

54-
public static ObjectMapper registerBundleMapperSubTypes(NamedType... additionalNamedTypes) {
61+
public static ObjectMapper registerBundleMapperSubTypes(ObjectMapper objectMapper, NamedType... additionalNamedTypes) {
5562
Class<?>[] sdoClasses = {AttackPattern.class, Campaign.class, CourseOfAction.class,
5663
Identity.class, Indicator.class, IntrusionSet.class, Malware.class, ObservedData.class,
5764
Report.class, ThreatActor.class, Tool.class, Vulnerability.class};
@@ -108,8 +115,10 @@ public static BundleableObject parseObject(String objectJsonString) throws IOExc
108115
}
109116
}
110117

111-
// public static SimpleModule generateStixInstantModule(){
112-
// SimpleModule module = new SimpleModule();
113-
// module.
114-
// }
118+
public static SimpleModule generateStixInstantModule(){
119+
SimpleModule module = new SimpleModule();
120+
module.addSerializer(StixInstant.class, new StixInstantSerializer());
121+
module.addDeserializer(StixInstant.class, new StixInstantDeserializer());
122+
return module;
123+
}
115124
}

0 commit comments

Comments
 (0)