Parse dictionaries as well as strings and file-like objects

stix2.1
clenk 2017-04-10 10:42:07 -04:00
parent dd382520d6
commit 168105603b
2 changed files with 20 additions and 6 deletions

View File

@ -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

View File

@ -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