# -*- coding: utf-8 -*-

# Copyright(C) 2013 Julien Hebert
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from weboob.tools.json import json

from .iformatter import IFormatter

__all__ = ['JsonFormatter']


class Encoder(json.JSONEncoder):
    "generic weboob object encoder"

    def default(self, obj):
        try:
            return json.JSONEncoder.default(self, obj)
        except TypeError:
            try:
                dct = obj.to_dict()
            except AttributeError:
                return str(obj)
            for z in dct:
                return z


class JsonFormatter(IFormatter):
    def format_dict(self, item):
        return json.dumps(item, cls=Encoder)

@@ -1236,48 +1236,613 @@
 def 
-format_dict(self, item):%0A        return 
+__init__(self):%0A        IFormatter.__init__(self)%0A        self.queue = %5B%5D%0A%0A    def flush(self):%0A        if len(self.queue) == 0:%0A            return%0A        elif len(self.queue) == 1:%0A            print self.queue%5B0%5D%0A        else:%0A            result = u%22%22%0A            first = False%0A            result += u%22%5B%22%0A            for item in self.queue:%0A                if not first:%0A                    first = True%0A                else:%0A                    result += u%22,%22%0A                result += item%0A            result += %22%5D%22%0A            print result%0A%0A    def format_dict(self, item):%0A        self.queue.append(
 json
@@ -1866,9 +1866,10 @@
 Encoder)
+)
 %0A

Fix JSON export