@@ -7,6 +7,8 @@ package attackpattern
77
88import (
99 "encoding/json"
10+
11+ "github.com/freetaxii/libstix2/defs"
1012)
1113
1214// ----------------------------------------------------------------------
@@ -19,50 +21,20 @@ object along with any errors. */
1921func Decode (data []byte ) (* AttackPattern , error ) {
2022 var o AttackPattern
2123
22- err := json .Unmarshal (data , & o )
23- if err != nil {
24+ if err := json .Unmarshal (data , & o ); err != nil {
2425 return nil , err
2526 }
2627
27- o .SetRawData (data )
28-
2928 return & o , nil
3029}
3130
32- /* UnmarshalJSON - This method will over right the default UnmarshalJSON method
31+ /* UnmarshalJSON - This method will over write the default UnmarshalJSON method
3332to enable custom properties that this library does not know about. It will store
34- them as map of byte arrays. This way a tool that does know how to deal with them
35- can then further process them after this is done. */
33+ them as map where the value of each key is a byte arrays. This way a tool that
34+ does know how to deal with them can then further process them after this is
35+ done. This will also allow the storage of the raw JSON data. */
3636func (o * AttackPattern ) UnmarshalJSON (b []byte ) error {
37- // First thing is to capture all of the properties in a map so we can remove
38- // what we know about. This will leave us with just the custom properties.
39- var customProperties map [string ]* json.RawMessage
40- if err := json .Unmarshal (b , & customProperties ); err != nil {
41- return err
42- }
4337
44- // Now delete the properties we know about
45- delete (customProperties , "type" )
46- delete (customProperties , "spec_version" )
47- delete (customProperties , "id" )
48- delete (customProperties , "created_by_ref" )
49- delete (customProperties , "created" )
50- delete (customProperties , "modified" )
51- delete (customProperties , "revoked" )
52- delete (customProperties , "labels" )
53- delete (customProperties , "confidence" )
54- delete (customProperties , "lang" )
55- delete (customProperties , "external_references" )
56- delete (customProperties , "object_marking_refs" )
57- delete (customProperties , "granular_markings" )
58-
59- delete (customProperties , "name" )
60- delete (customProperties , "description" )
61- delete (customProperties , "aliases" )
62- delete (customProperties , "kill_chain_phases" )
63-
64- // Unmarshal the properties that we understand. We need to alias the object
65- // so that we do not recursively call Unmarshal on this object.
6638 type alias AttackPattern
6739 temp := & struct {
6840 * alias
@@ -73,13 +45,19 @@ func (o *AttackPattern) UnmarshalJSON(b []byte) error {
7345 return err
7446 }
7547
76- // If there are any custom properties left store them in the custom property
77- if len (customProperties ) > 0 {
78- o .Custom = make (map [string ][]byte )
79- for k , v := range customProperties {
80- o .Custom [k ] = * v
81- }
48+ // This will create a map of all of the custom properties and store them in a
49+ // property called o.Custom
50+ if err := o .FindCustomProperties (b , o .GetPropertyList ()); err != nil {
51+ return err
52+ }
53+
54+ // This will store a complete copy of the original JSON in a byte array called
55+ // o.Raw. This could be useful if you need to digitally sign the JSON or do
56+ // verification on what was actually received.
57+ if defs .KEEP_RAW_DATA == true {
58+ o .SetRawData (b )
8259 }
60+
8361 return nil
8462}
8563
0 commit comments