@@ -58,6 +58,13 @@ namespace openPMD
5858 throw std::runtime_error ((TEXT)); \
5959 }
6060
61+ namespace JSONDefaults
62+ {
63+ using const_str = char const *const ;
64+ constexpr const_str openpmd_internal = " __openPMD_internal" ;
65+ constexpr const_str IOMode = " IO_mode" ;
66+ } // namespace JSONDefaults
67+
6168namespace
6269{
6370 struct DefaultValue
@@ -166,6 +173,7 @@ JSONIOHandlerImpl::JSONIOHandlerImpl(
166173 " Invalid value: '" + mode +
167174 " ' (accepted values are 'dataset' and 'template'." );
168175 }
176+ m_modeWasManuallySpecified = true ;
169177 }
170178 auto shadow = jsonConfig.invertShadow ();
171179 if (shadow.size () > 0 )
@@ -1600,6 +1608,55 @@ std::shared_ptr<nlohmann::json> JSONIOHandlerImpl::obtainJsonContents(File file)
16001608 break ;
16011609 }
16021610 VERIFY (fh->good (), " [JSON] Failed reading from a file." );
1611+ if (res->contains (JSONDefaults::openpmd_internal))
1612+ {
1613+ auto const &openpmd_internal = res->at (JSONDefaults::openpmd_internal);
1614+ if (openpmd_internal.contains (JSONDefaults::IOMode))
1615+ {
1616+ auto modeOption = openPMD::json::asLowerCaseStringDynamic (
1617+ openpmd_internal.at (JSONDefaults::IOMode));
1618+ if (!modeOption.has_value ())
1619+ {
1620+ std::cerr
1621+ << " [JSON/TOML backend] Warning: Invalid value of "
1622+ " non-string type at internal meta table for entry '"
1623+ << JSONDefaults::IOMode << " '. Will ignore and continue."
1624+ << std::endl;
1625+ }
1626+ else if (modeOption.value () == " dataset" )
1627+ {
1628+ if (m_modeWasManuallySpecified && m_mode == IOMode::Template)
1629+ {
1630+ std::cerr
1631+ << " [JSON/TOML backend] Warning: IO Mode was manually "
1632+ " specified as 'Template', but opened file is in "
1633+ " 'Dataset' mode. Will continue with dataset mode."
1634+ << std::endl;
1635+ }
1636+ m_mode = IOMode::Dataset;
1637+ }
1638+ else if (modeOption.value () == " template" )
1639+ {
1640+ if (m_modeWasManuallySpecified && m_mode == IOMode::Dataset)
1641+ {
1642+ std::cerr
1643+ << " [JSON/TOML backend] Warning: IO Mode was manually "
1644+ " specified as 'Dataset', but opened file is in "
1645+ " 'Template' mode. Will continue with template mode."
1646+ << std::endl;
1647+ }
1648+ m_mode = IOMode::Template;
1649+ }
1650+ else
1651+ {
1652+ std::cerr << " [JSON/TOML backend] Warning: Invalid value '"
1653+ << modeOption.value ()
1654+ << " ' at internal meta table for entry '"
1655+ << JSONDefaults::IOMode
1656+ << " '. Will ignore and continue." << std::endl;
1657+ }
1658+ }
1659+ }
16031660 m_jsonVals.emplace (file, res);
16041661 return res;
16051662}
@@ -1628,8 +1685,12 @@ void JSONIOHandlerImpl::putJsonContents(
16281685 {
16291686 case IOMode::Dataset:
16301687 (*it->second )[" platform_byte_widths" ] = platformSpecifics ();
1688+ (*it->second )[JSONDefaults::openpmd_internal]
1689+ [JSONDefaults::IOMode] = " dataset" ;
16311690 break ;
16321691 case IOMode::Template:
1692+ (*it->second )[JSONDefaults::openpmd_internal]
1693+ [JSONDefaults::IOMode] = " template" ;
16331694 break ;
16341695 }
16351696
0 commit comments