1# Copyright (c) 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4import json 5 6from tracing.mre import output_formatter 7 8 9class JSONOutputFormatter(output_formatter.OutputFormatter): 10 11 def __init__(self, output_file): 12 # TODO(nduca): Resolve output_file here vs output_stream in base class. 13 super(JSONOutputFormatter, self).__init__(output_file) 14 self.output_file = output_file 15 16 def Format(self, result_list): 17 d = [result.AsDict() for result in result_list] 18 json.dump(d, self.output_file, indent=2) 19 if hasattr(self.output_file, 'flush'): 20 self.output_file.flush() 21