class_docstr: A class to store and visualize the resulting counterfactual explanations.
class_docstr_tok: ["A","class","to","store","and","visualize","the","resulting","counterfactual","explanations","."]
code: def to_json(self, serialization_version):
      if self.final_cfs_df_sparse is not None:
          df = self.final_cfs_df_sparse
      else:
          df = self.final_cfs_df
  
      dummy_data_interface = None
      if hasattr(self.data_interface, "data_df"):
          dummy_data_interface = DummyDataInterface(
              self.data_interface.outcome_name, "dummy_data"
          )
      else:
          dummy_data_interface = DummyDataInterface(
              self.data_interface.outcome_name
          )
  
      if serialization_version == _SchemaVersions.V1:
          obj = {
              _DiverseCFV1SchemaConstants.DATA_INTERFACE: dummy_data_interface,
              _DiverseCFV1SchemaConstants.MODEL_TYPE: self.model_type,
              _DiverseCFV1SchemaConstants.DESIRED_CLASS: self.desired_class,
              _DiverseCFV1SchemaConstants.DESIRED_RANGE: self.desired_range,
              _DiverseCFV1SchemaConstants.TEST_INSTANCE_DF: self.test_instance_df,
              _DiverseCFV1SchemaConstants.FINAL_CFS_DF: df,
          }
          return json.dumps(obj, default=json_converter)
      elif serialization_version == _SchemaVersions.V2:
          dummy_data_interface_dict = dummy_data_interface.to_json()
          feature_names_including_target = (
              self.test_instance_df.columns.tolist()
          )
          feature_names = self.test_instance_df.columns.tolist().copy()
          feature_names.remove(dummy_data_interface.outcome_name)
          test_instance_df_as_list = (
              self.test_instance_df.values.tolist()
          )
          if df is not None:
              final_cfs_df_as_as_list = df.values.tolist()
          else:
              final_cfs_df_as_as_list = None
  
          alternate_obj = {
              _DiverseCFV2SchemaConstants.TEST_INSTANCE_LIST: test_instance_df_as_list,
              _DiverseCFV2SchemaConstants.FINAL_CFS_LIST: final_cfs_df_as_as_list,
              _DiverseCFV2SchemaConstants.DATA_INTERFACE: dummy_data_interface_dict,
              _DiverseCFV2SchemaConstants.FEATURE_NAMES: feature_names,
              _DiverseCFV2SchemaConstants.FEATURE_NAMES_INCLUDING_TARGET: feature_names_including_target,
              _DiverseCFV2SchemaConstants.MODEL_TYPE: self.model_type,
              _DiverseCFV2SchemaConstants.DESIRED_CLASS: self.desired_class,
              _DiverseCFV2SchemaConstants.DESIRED_RANGE: self.desired_range,
          }
          return json.dumps(alternate_obj, default=json_converter)
  
imports: import copy
  import json
  import math
  import numpy
  import pandas
  from dice_ml.constants import ModelTypes, _SchemaVersions
  from dice_ml.utils.serialize import DummyDataInterface
label_desc: Use image node_id 9 for calling the CounterfactualExamples obj's underlying member method code with example usage: obj.to_json(serialization_version) and returns: json, json