class_docstr: Write scalars to a json file.
  
  It saves scalars as one json per line (instead of a big json) for easy parsing.
  
  Examples parsing such a json file:
  ::
      $ cat metrics.json | jq -s '.[0:2]'
      [
        {
          "data_time": 0.008433341979980469,
          "iteration": 19,
          "loss": 1.9228371381759644,
          "loss_box_reg": 0.050025828182697296,
          "loss_classifier": 0.5316952466964722,
          "loss_mask": 0.7236229181289673,
          "loss_rpn_box": 0.0856662318110466,
          "loss_rpn_cls": 0.48198649287223816,
          "lr": 0.007173333333333333,
          "time": 0.25401854515075684
        },
        {
          "data_time": 0.007216215133666992,
          "iteration": 39,
          "loss": 1.282649278640747,
          "loss_box_reg": 0.06222952902317047,
          "loss_classifier": 0.30682939291000366,
          "loss_mask": 0.6970193982124329,
          "loss_rpn_box": 0.038663312792778015,
          "loss_rpn_cls": 0.1471673548221588,
          "lr": 0.007706666666666667,
          "time": 0.2490077018737793
        }
      ]
  
      $ cat metrics.json | jq '.loss_mask'
      0.7126231789588928
      0.689423680305481
      0.6776131987571716
      ...
code: def write(self):
      storage = get_event_storage()
      to_save = defaultdict(dict)
  
      for k, (v, iter) in storage.latest_with_smoothing_hint(
          self._window_size
      ).items():
          # keep scalars that have not been written
          if iter <= self._last_write:
              continue
          to_save[iter][k] = v
      if len(to_save):
          all_iters = sorted(to_save.keys())
          self._last_write = max(all_iters)
  
      for itr, scalars_per_iter in to_save.items():
          scalars_per_iter["iteration"] = itr
          self._file_handle.write(
              json.dumps(scalars_per_iter, sort_keys=True) + "\n"
          )
      self._file_handle.flush()
      try:
          os.fsync(self._file_handle.fileno())
      except AttributeError:
          pass
  
imports: import datetime
  import json
  import logging
  import os
  import time
  from collections import defaultdict
  from contextlib import contextmanager
  from functools import cached_property
  from typing import Optional
  import torch
  from fvcore.common.history_buffer import HistoryBuffer
  from detectron2.utils.file_io import PathManager
label_desc: Use image node_id 2 for calling the JSONWriter obj's underlying member method code with example usage: obj.write() without return types