Parse dictionaries as well as strings and file-like objects
parent
dd382520d6
commit
168105603b
|
@ -15,10 +15,13 @@ from .sro import Relationship
|
|||
def parse(data):
|
||||
"""Deserialize a string or file-like object into a STIX object"""
|
||||
|
||||
try:
|
||||
obj = json.loads(data)
|
||||
except TypeError:
|
||||
obj = json.load(data)
|
||||
if type(data) is dict:
|
||||
obj = data
|
||||
else:
|
||||
try:
|
||||
obj = json.loads(data)
|
||||
except TypeError:
|
||||
obj = json.load(data)
|
||||
|
||||
if 'type' not in obj:
|
||||
# TODO parse external references, kill chain phases, and granular markings
|
||||
|
|
|
@ -91,8 +91,19 @@ def test_invalid_kwarg_to_malware():
|
|||
assert str(excinfo.value) == "unexpected keyword arguments: ['my_custom_property']"
|
||||
|
||||
|
||||
def test_parse_malware():
|
||||
mal = stix2.parse(EXPECTED_MALWARE)
|
||||
@pytest.mark.parametrize("data", [
|
||||
EXPECTED_MALWARE,
|
||||
{
|
||||
"type": "malware",
|
||||
"id": "malware--fedcba98-7654-3210-fedc-ba9876543210",
|
||||
"created": "2016-05-12T08:17:27Z",
|
||||
"modified": "2016-05-12T08:17:27Z",
|
||||
"labels": ["ransomware"],
|
||||
"name": "Cryptolocker",
|
||||
},
|
||||
])
|
||||
def test_parse_malware(data):
|
||||
mal = stix2.parse(data)
|
||||
|
||||
assert mal.type == 'malware'
|
||||
assert mal.id == MALWARE_ID
|
||||
|
|
Loading…
Reference in New Issue