@@ -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 )
@@ -1580,6 +1588,55 @@ std::shared_ptr<nlohmann::json> JSONIOHandlerImpl::obtainJsonContents(File file)
15801588 break ;
15811589 }
15821590 VERIFY (fh->good (), " [JSON] Failed reading from a file." );
1591+ if (res->contains (JSONDefaults::openpmd_internal))
1592+ {
1593+ auto const &openpmd_internal = res->at (JSONDefaults::openpmd_internal);
1594+ if (openpmd_internal.contains (JSONDefaults::IOMode))
1595+ {
1596+ auto modeOption = openPMD::json::asLowerCaseStringDynamic (
1597+ openpmd_internal.at (JSONDefaults::IOMode));
1598+ if (!modeOption.has_value ())
1599+ {
1600+ std::cerr
1601+ << " [JSON/TOML backend] Warning: Invalid value of "
1602+ " non-string type at internal meta table for entry '"
1603+ << JSONDefaults::IOMode << " '. Will ignore and continue."
1604+ << std::endl;
1605+ }
1606+ else if (modeOption.value () == " dataset" )
1607+ {
1608+ if (m_modeWasManuallySpecified && m_mode == IOMode::Template)
1609+ {
1610+ std::cerr
1611+ << " [JSON/TOML backend] Warning: IO Mode was manually "
1612+ " specified as 'Template', but opened file is in "
1613+ " 'Dataset' mode. Will continue with dataset mode."
1614+ << std::endl;
1615+ }
1616+ m_mode = IOMode::Dataset;
1617+ }
1618+ else if (modeOption.value () == " template" )
1619+ {
1620+ if (m_modeWasManuallySpecified && m_mode == IOMode::Dataset)
1621+ {
1622+ std::cerr
1623+ << " [JSON/TOML backend] Warning: IO Mode was manually "
1624+ " specified as 'Dataset', but opened file is in "
1625+ " 'Template' mode. Will continue with template mode."
1626+ << std::endl;
1627+ }
1628+ m_mode = IOMode::Template;
1629+ }
1630+ else
1631+ {
1632+ std::cerr << " [JSON/TOML backend] Warning: Invalid value '"
1633+ << modeOption.value ()
1634+ << " ' at internal meta table for entry '"
1635+ << JSONDefaults::IOMode
1636+ << " '. Will ignore and continue." << std::endl;
1637+ }
1638+ }
1639+ }
15831640 m_jsonVals.emplace (file, res);
15841641 return res;
15851642}
@@ -1608,8 +1665,12 @@ void JSONIOHandlerImpl::putJsonContents(
16081665 {
16091666 case IOMode::Dataset:
16101667 (*it->second )[" platform_byte_widths" ] = platformSpecifics ();
1668+ (*it->second )[JSONDefaults::openpmd_internal]
1669+ [JSONDefaults::IOMode] = " dataset" ;
16111670 break ;
16121671 case IOMode::Template:
1672+ (*it->second )[JSONDefaults::openpmd_internal]
1673+ [JSONDefaults::IOMode] = " template" ;
16131674 break ;
16141675 }
16151676
0 commit comments