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,6 +15,9 @@ from .sro import Relationship
def parse(data): def parse(data):
"""Deserialize a string or file-like object into a STIX object""" """Deserialize a string or file-like object into a STIX object"""
if type(data) is dict:
obj = data
else:
try: try:
obj = json.loads(data) obj = json.loads(data)
except TypeError: except TypeError:

View File

@ -91,8 +91,19 @@ def test_invalid_kwarg_to_malware():
assert str(excinfo.value) == "unexpected keyword arguments: ['my_custom_property']" assert str(excinfo.value) == "unexpected keyword arguments: ['my_custom_property']"
def test_parse_malware(): @pytest.mark.parametrize("data", [
mal = stix2.parse(EXPECTED_MALWARE) 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.type == 'malware'
assert mal.id == MALWARE_ID assert mal.id == MALWARE_ID