code: import json
  from typing import List
  import numpy
  from ..Spectrum import Spectrum
  
  
  def save_as_json(spectrums: List[Spectrum], filename: str):
      """Save spectrum(s) as json file.
  
      :py:attr:`~matchms.Spectrum.losses` of spectrum will not be saved.
  
      Example:
  
      .. code-block:: python
  
          import numpy
          from matchms import Spectrum
          from matchms.exporting import save_as_json
  
          # Create dummy spectrum
          spectrum = Spectrum(mz=numpy.array([100, 200, 300], dtype="float"),
                              intensities=numpy.array([10, 10, 500], dtype="float"),
                              metadata={"charge": -1,
                                        "inchi": '"InChI=1S/C6H12"',
                                        "precursor_mz": 222.2})
  
          # Write spectrum to test file
          save_as_json(spectrum, "test.json")
  
      Parameters
      ----------
      spectrums:
          Expected input is a list of  :py:class:`~matchms.Spectrum.Spectrum` objects.
      filename:
          Provide filename to save spectrum(s).
      """
      if not isinstance(spectrums, list):
          # Assume that input was single Spectrum
          spectrums = [spectrums]
  
      # Write to json file
      with open(filename, 'w', encoding="utf-8") as fout:
          json.dump(spectrums, fout, cls=SpectrumJSONEncoder)
  
  
  class SpectrumJSONEncoder(json.JSONEncoder):
      # See https://github.com/PyCQA/pylint/issues/414 for reference
      def default(self, o):
          """JSON Encoder which can encode a :py:class:`~matchms.Spectrum.Spectrum` object"""
          if isinstance(o, Spectrum):
              spec = o.clone()
              peaks_list = numpy.vstack((spec.peaks.mz, spec.peaks.intensities)).T.tolist()
  
              # Convert matchms.Spectrum() into dictionaries
              spectrum_dict = {key: spec.metadata[key] for key in spec.metadata}
              spectrum_dict["peaks_json"] = peaks_list
              return spectrum_dict
          return json.JSONEncoder.default(self, o)
   import numpy
  
  
  class Fragments:
      """
      Stores arrays of intensities and M/z values, with some checks on their internal consistency.
  
      For example
  
      .. testcode::
  
          import numpy as np
          from matchms import Fragments
  
          mz = np.array([10, 20, 30], dtype="float")
          intensities = np.array([100, 20, 300], dtype="float")
  
          peaks = Fragments(mz=mz, intensities=intensities)
          print(peaks[2])
  
      Should output
  
      .. testoutput::
  
         [ 30. 300.]
  
      Attributes
      ----------
      mz:
          Numpy array of m/z values.
      intensities:
          Numpy array of peak intensity values.
  
      """
      def __init__(self, mz=None, intensities=None):
          assert isinstance(mz, numpy.ndarray), "Input argument 'mz' should be a numpy.array."
          assert isinstance(intensities, numpy.ndarray), "Input argument 'intensities' should be a numpy.array."
          assert mz.shape == intensities.shape, "Input arguments 'mz' and 'intensities' should be the same shape."
          assert mz.dtype == "float", "Input argument 'mz' should be an array of type float."
          assert intensities.dtype == "float", "Input argument 'intensities' should be an array of type float."
  
          self._mz = mz
          self._intensities = intensities
  
          assert self._is_sorted(), "mz values are out of order."
  
      def __eq__(self, other):
          return \
              self.mz.shape == other.mz.shape and \
              numpy.allclose(self.mz, other.mz) and \
              self.intensities.shape == other.intensities.shape and \
              numpy.allclose(self.intensities, other.intensities)
  
      def __len__(self):
          return self._mz.size
  
      def __getitem__(self, item):
          return numpy.asarray([self.mz[item], self.intensities[item]])
  
      def _is_sorted(self):
          return numpy.all(self.mz[:-1] <= self.mz[1:])
  
      def clone(self):
          return Fragments(self.mz, self.intensities)
  
      @property
      def mz(self):
          """getter method for mz private variable"""
          return self._mz.copy()
  
      @property
      def intensities(self):
          """getter method for intensities private variable"""
          return self._intensities.copy()
  
      @property
      def to_numpy(self):
          """getter method to return stacked numpy array of both peak mz and
          intensities"""
          return numpy.vstack((self.mz, self.intensities)).T
  
apis: numpy.vstack
apis: numpy.vstack numpy.allclose numpy.all numpy.asarray