From 4e15ac5d79f05f96474ef72fae894613acddcf75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Wed, 1 Nov 2017 11:26:45 -0700 Subject: [PATCH] chg: Add to_json helpers --- pymispgalaxies/api.py | 12 ++++++++++++ tests/tests.py | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pymispgalaxies/api.py b/pymispgalaxies/api.py index 0784421..054264e 100644 --- a/pymispgalaxies/api.py +++ b/pymispgalaxies/api.py @@ -50,6 +50,9 @@ class Galaxy(): self.version = self.galaxy['version'] self.uuid = self.galaxy['uuid'] + def to_json(self): + return json.dumps(self, cls=EncodeGalaxies) + def to_dict(self): return {'type': self.type, 'name': self.name, 'description': self.description, 'version': self.version, 'uuid': self.uuid, 'icon': self.icon} @@ -109,6 +112,9 @@ class ClusterValueMeta(): # defined on the schema self.additional_properties = m + def to_json(self): + return json.dumps(self, cls=EncodeClusters) + def to_dict(self): to_return = {} if self.type: @@ -165,6 +171,9 @@ class ClusterValue(): return None return ClusterValueMeta(m) + def to_json(self): + return json.dumps(self, cls=EncodeClusters) + def to_dict(self): to_return = {'value': self.value} if self.description: @@ -217,6 +226,9 @@ class Cluster(collections.Mapping): def __iter__(self): return iter(self.cluster_values) + def to_json(self): + return json.dumps(self, cls=EncodeClusters) + def to_dict(self): to_return = {'name': self.name, 'type': self.type, 'source': self.source, 'authors': self.authors, 'description': self.description, diff --git a/tests/tests.py b/tests/tests.py index d08c393..273051e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- import unittest -from pymispgalaxies import Galaxies, Clusters, UnableToRevertMachinetag, EncodeGalaxies, EncodeClusters +from pymispgalaxies import Galaxies, Clusters, UnableToRevertMachinetag from glob import glob import os import json @@ -69,5 +69,7 @@ class TestPyMISPGalaxies(unittest.TestCase): self.assertIsNot(len(c), 0) def test_json(self): + for g in self.galaxies.values(): + g.to_json() for c in self.clusters.values(): - json.dumps(c, cls=EncodeClusters) + c.to_json()