22
33import com .fasterxml .jackson .databind .ObjectMapper ;
44import com .fasterxml .jackson .databind .jsontype .NamedType ;
5+ import com .fasterxml .jackson .databind .module .SimpleModule ;
56import com .fasterxml .jackson .datatype .guava .GuavaModule ;
67import com .fasterxml .jackson .datatype .jdk8 .Jdk8Module ;
78import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
89import com .fasterxml .jackson .module .paramnames .ParameterNamesModule ;
910import io .digitalstate .stix .bundle .Bundle ;
1011import io .digitalstate .stix .bundle .BundleObject ;
1112import io .digitalstate .stix .bundle .BundleableObject ;
13+ import io .digitalstate .stix .common .StixInstant ;
1214import io .digitalstate .stix .coo .extension .types .*;
1315import io .digitalstate .stix .coo .objects .*;
1416import io .digitalstate .stix .coo .objects .Process ;
2426
2527public 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