updated JsonLinesItemExporter to new exporters API

This commit is contained in:
Ismael Carnales 2009-08-20 12:58:10 -03:00
parent 3152e66fec
commit 6b7162f3ca
2 changed files with 9 additions and 8 deletions

View File

@ -12,6 +12,8 @@ class JsonLinesItemExporter(BaseItemExporter):
self.file = file
self.encoder = json.JSONEncoder(*args, **kwargs)
def export(self, item):
itemdict = dict(self._get_fields_to_export(item))
def export_item(self, item):
itemdict = {}
for field, value in self._get_fields_to_export(item):
itemdict[field] = self.serialize(item.fields[field], field, value)
self.file.write(self.encoder.encode(itemdict) + '\n')

View File

@ -134,7 +134,7 @@ class XmlItemExporterTest(BaseTest):
self.assertEqual(self.output.getvalue(), expected_value)
class JSONItemExporterTest(BaseTest):
class JsonLinesItemExporterTest(BaseTest):
def setUp(self):
try:
@ -145,18 +145,17 @@ class JSONItemExporterTest(BaseTest):
except ImportError:
raise unittest.SkipTest("simplejson module not available")
from scrapy.contrib.exporter.jsonlines import JsonLinesItemExporter
super(JSONItemExporterTest, self).__init__()
super(JsonLinesItemExporterTest, self).setUp()
def test_export(self):
from scrapy.contrib.exporter.jsonlines import JsonLinesItemExporter
ie = JsonLinesItemExporter(output)
ie = JsonLinesItemExporter(self.output)
ie.start_exporting()
ie.export_item(self.i)
ie.finish_exporting()
self.assertEqual(output.getvalue(), '{"age": "22", "name": "John"}\n')
self.assertEqual(self.output.getvalue(), '{"age": "22", "name": "John"}\n')
if __name__ == '__main__':