<commit_before># Copyright 2017 The Chromium Authors.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd.

import json
import sys
import os

from .compat import IsString, ToStringSafe

# For type checking. Not needed at runtime.
try:
  from typing import Any, Optional, List, Dict, Union, Type, Tuple
except ImportError:
  pass


class CodeSearchProtoJsonEncoder(json.JSONEncoder):

  def default(self, o):
    if isinstance(o, Message):
      return o.__dict__
    return o


class CodeSearchProtoJsonSymbolizedEncoder(json.JSONEncoder):

  def default(self, o):
    if isinstance(o, Message):
      rv = {}
      desc = o.__class__.DESCRIPTOR
      for k, v in o.__dict__.items():
        if k in desc and not isinstance(desc[k], list) and issubclass(
            desc[k], Message) and desc[k].IsEnum():
          rv[k] = desc[k].ToSymbol(v)
        else:
          rv[k] = v
      return rv
    return o<commit_msg>When serializing to JSON, omit useless values.<commit_after># Copyright 2017 The Chromium Authors.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd.

import json
import sys
import os

from .compat import IsString, ToStringSafe

# For type checking. Not needed at runtime.
try:
  from typing import Any, Optional, List, Dict, Union, Type, Tuple
except ImportError:
  pass


class CodeSearchProtoJsonEncoder(json.JSONEncoder):

  def default(self, o):
    if isinstance(o, Message):
      return o.__dict__
    return o


class CodeSearchProtoJsonSymbolizedEncoder(json.JSONEncoder):

  def default(self, o):
    if isinstance(o, Message):
      rv = {}
      desc = o.__class__.DESCRIPTOR
      for k, v in o.__dict__.items():
        if k in desc:
          if not isinstance(desc[k], list) and \
              issubclass(desc[k], Message) and \
              desc[k].IsEnum():
            rv[k] = desc[k].ToSymbol(v)
          elif desc[k] == str and IsString(v) and not v:
            pass
          elif isinstance(desc[k], list) and \
              isinstance(v, list) and \
              len(v) == 0:
            pass
          elif v is None:
            pass
          else:
            rv[k] = v
        else:
          rv[k] = v
      return rv
    return o
{'fraction_non_alphanumeric': 0.09437086092715231, 'fraction_numerical': 0.0037251655629139072, 'mean_word_length': 2.4677187948350072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7282433', 'n_tokens_mistral': 733, 'n_tokens_neox': 711, 'n_words': 253}