Make sure all integers are string in the dumped json

pull/34/head
Raphaël Vinot 2016-09-30 16:06:41 +02:00
parent dcd76ec5c4
commit 5ee23d46c0
3 changed files with 33 additions and 21 deletions

View File

@ -225,7 +225,7 @@
}, },
"Event": { "Event": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event",
"type": "object", "type": "array",
"items": { "items": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0",
"type": "object", "type": "object",

View File

@ -9,15 +9,15 @@
"properties": { "properties": {
"id": { "id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/id",
"type": "integer" "type": "string"
}, },
"orgc_id": { "orgc_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/orgc_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/orgc_id",
"type": "integer" "type": "string"
}, },
"org_id": { "org_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/org_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/org_id",
"type": "integer" "type": "string"
}, },
"date": { "date": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/date", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/date",
@ -25,7 +25,7 @@
}, },
"threat_level_id": { "threat_level_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/threat_level_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/threat_level_id",
"type": "integer" "type": "string"
}, },
"info": { "info": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/info", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/info",
@ -41,19 +41,19 @@
}, },
"attribute_count": { "attribute_count": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/attribute_count", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/attribute_count",
"type": "integer" "type": "string"
}, },
"analysis": { "analysis": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/analysis", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/analysis",
"type": "integer" "type": "string"
}, },
"timestamp": { "timestamp": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/timestamp", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/timestamp",
"type": "integer" "type": "string"
}, },
"distribution": { "distribution": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/distribution", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/distribution",
"type": "integer" "type": "string"
}, },
"proposal_email_lock": { "proposal_email_lock": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/proposal_email_lock", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/proposal_email_lock",
@ -65,11 +65,11 @@
}, },
"publish_timestamp": { "publish_timestamp": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/publish_timestamp", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/publish_timestamp",
"type": "integer" "type": "string"
}, },
"sharing_group_id": { "sharing_group_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/sharing_group_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/sharing_group_id",
"type": "integer" "type": "string"
}, },
"Org": { "Org": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Org", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Org",
@ -136,15 +136,15 @@
}, },
"event_id": { "event_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/event_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/event_id",
"type": "integer" "type": "string"
}, },
"distribution": { "distribution": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/distribution", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/distribution",
"type": "integer" "type": "string"
}, },
"timestamp": { "timestamp": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/timestamp", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/timestamp",
"type": "integer" "type": "string"
}, },
"comment": { "comment": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/comment", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/comment",
@ -152,7 +152,7 @@
}, },
"sharing_group_id": { "sharing_group_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/sharing_group_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/sharing_group_id",
"type": "integer" "type": "string"
}, },
"value": { "value": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/value", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/Attribute/17/value",
@ -225,7 +225,7 @@
}, },
"Event": { "Event": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event",
"type": "object", "type": "array",
"items": { "items": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0",
"type": "object", "type": "object",
@ -240,7 +240,7 @@
}, },
"threat_level_id": { "threat_level_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/threat_level_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/threat_level_id",
"type": "integer" "type": "string"
}, },
"info": { "info": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/info", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/info",
@ -260,7 +260,7 @@
}, },
"timestamp": { "timestamp": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/timestamp", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/timestamp",
"type": "integer" "type": "string"
}, },
"distribution": { "distribution": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/distribution", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/distribution",
@ -268,11 +268,11 @@
}, },
"org_id": { "org_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/org_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/org_id",
"type": "integer" "type": "string"
}, },
"orgc_id": { "orgc_id": {
"id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/orgc_id", "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json/Event/RelatedEvent/0/Event/0/orgc_id",
"type": "integer" "type": "string"
} }
} }
}, },

View File

@ -114,6 +114,7 @@ class MISPAttribute(object):
'comment': self.comment} 'comment': self.comment}
if self.sharing_group_id: if self.sharing_group_id:
to_return['sharing_group_id'] = self.sharing_group_id to_return['sharing_group_id'] = self.sharing_group_id
to_return = _int_to_str(to_return)
return to_return return to_return
def _json_full(self): def _json_full(self):
@ -130,6 +131,7 @@ class MISPAttribute(object):
to_return['ShadowAttribute'] = self.ShadowAttribute to_return['ShadowAttribute'] = self.ShadowAttribute
if self.SharingGroup: if self.SharingGroup:
to_return['SharingGroup'] = self.SharingGroup to_return['SharingGroup'] = self.SharingGroup
to_return = _int_to_str(to_return)
return to_return return to_return
@ -149,6 +151,14 @@ class EncodeFull(JSONEncoder):
return JSONEncoder.default(self, obj) return JSONEncoder.default(self, obj)
def _int_to_str(d):
# transform all integer back to string
for k, v in d.items():
if isinstance(v, int) and not isinstance(v, bool):
d[k] = str(v)
return d
class MISPEvent(object): class MISPEvent(object):
def __init__(self, describe_types=None): def __init__(self, describe_types=None):
@ -300,6 +310,7 @@ class MISPEvent(object):
to_return['Event']['sharing_group_id'] = self.sharing_group_id to_return['Event']['sharing_group_id'] = self.sharing_group_id
if self.Tag: if self.Tag:
to_return['Event']['Tag'] = self.Tag to_return['Event']['Tag'] = self.Tag
to_return['Event'] = _int_to_str(to_return['Event'])
to_return['Event']['Attribute'] = [a._json() for a in self.attributes] to_return['Event']['Attribute'] = [a._json() for a in self.attributes]
jsonschema.validate(to_return, self.json_schema) jsonschema.validate(to_return, self.json_schema)
return to_return return to_return
@ -308,7 +319,7 @@ class MISPEvent(object):
to_return = self._json() to_return = self._json()
if self.locked is not None: if self.locked is not None:
to_return['Event']['locked'] = self.locked to_return['Event']['locked'] = self.locked
if self.attribute_count: if self.attribute_count is not None:
to_return['Event']['attribute_count'] = self.attribute_count to_return['Event']['attribute_count'] = self.attribute_count
if self.RelatedEvent: if self.RelatedEvent:
to_return['Event']['RelatedEvent'] = self.RelatedEvent to_return['Event']['RelatedEvent'] = self.RelatedEvent
@ -326,6 +337,7 @@ class MISPEvent(object):
to_return['Event']['publish_timestamp'] = int(time.mktime(self.publish_timestamp.timetuple())) to_return['Event']['publish_timestamp'] = int(time.mktime(self.publish_timestamp.timetuple()))
if self.timestamp: if self.timestamp:
to_return['Event']['timestamp'] = int(time.mktime(self.timestamp.timetuple())) to_return['Event']['timestamp'] = int(time.mktime(self.timestamp.timetuple()))
to_return['Event'] = _int_to_str(to_return['Event'])
to_return['Event']['Attribute'] = [a._json_full() for a in self.attributes] to_return['Event']['Attribute'] = [a._json_full() for a in self.attributes]
jsonschema.validate(to_return, self.json_schema) jsonschema.validate(to_return, self.json_schema)
return to_return return to_return