Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

updated JsonLinesItemExporter to new exporters API

Ismael Carnales6b7162f3caa6
diff --git a/scrapy/contrib/exporter/jsonlines.py b/scrapy/contrib/exporter/jsonlines.py
index f19ec4cf6..285577a29 100644
--- a/scrapy/contrib/exporter/jsonlines.py
+++ b/scrapy/contrib/exporter/jsonlines.py
@@ -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')
diff --git a/scrapy/tests/test_contrib_exporter.py b/scrapy/tests/test_contrib_exporter.py
index 4454fe1f5..9d11894a3 100644
--- a/scrapy/tests/test_contrib_exporter.py
+++ b/scrapy/tests/test_contrib_exporter.py
@@ -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__':