Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:03

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/cont/ArrayIO.json.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <nlohmann/json.hpp>
0010 
0011 #include "corecel/Assert.hh"
0012 
0013 #include "Array.hh"
0014 
0015 namespace celeritas
0016 {
0017 //---------------------------------------------------------------------------//
0018 /*!
0019  * Read an array from a JSON file.
0020  */
0021 template<class T, size_type N>
0022 void from_json(nlohmann::json const& j, Array<T, N>& value)
0023 {
0024     CELER_VALIDATE(j.size() == N,
0025                    << "unexpected array size (" << j.size()
0026                    << ") in JSON input: should be " << N);
0027     for (size_type i = 0; i != N; ++i)
0028     {
0029         value[i] = j[i].get<T>();
0030     }
0031 }
0032 
0033 //---------------------------------------------------------------------------//
0034 /*!
0035  * Write an array to a JSON file.
0036  */
0037 template<class T, size_type N>
0038 void to_json(nlohmann::json& j, Array<T, N> const& value)
0039 {
0040     j = nlohmann::json::array();
0041     for (size_type i = 0; i != N; ++i)
0042     {
0043         j.push_back(value[i]);
0044     }
0045 }
0046 
0047 //---------------------------------------------------------------------------//
0048 }  // namespace celeritas