From feabd6c8b3b74f61198a5452c32dc67aeacde69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Fri, 30 Nov 2018 13:17:06 +0100 Subject: [PATCH] Remove object_type key --- docs/source/backends/json.rst | 20 ++++------ docs/source/backends/json_example.json | 14 ++----- include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp | 5 ++- src/IO/JSON/JSONIOHandlerImpl.cpp | 40 +++++++++++-------- 4 files changed, 36 insertions(+), 43 deletions(-) diff --git a/docs/source/backends/json.rst b/docs/source/backends/json.rst index 4ef5ddf002..42b55844b7 100644 --- a/docs/source/backends/json.rst +++ b/docs/source/backends/json.rst @@ -16,34 +16,28 @@ A JSON file uses the file ending ``.json``. The JSON backend is chosen by creati a ``Series`` object with a filename that has this file ending. The top-level JSON object is a group representing the openPMD root group ``"/"``. -Any **openPMD group** is represented in JSON as a JSON object with two keys: +Any **openPMD group** is represented in JSON as a JSON object with two reserved keys: - * ``object_type`` For groups, this key points to the string ``"group"``. This makes - it possible to distinguish groups from datasets. * ``attributes``: Attributes associated with the group. This key may be null or not be present at all, thus indicating a group without attributes. + * ``platform_byte_widths`` (root group only): Byte widths specific to the writing platform. + Will be overwritten every time that a JSON value is stored to disk, hence this information + is only available about the last platform writing the JSON value. All datasets and subgroups contained in this group are represented as a further key of -the group object. ``object_type``, ``attributes`` and ``platform_byte_widths`` have +the group object. ``attributes`` and ``platform_byte_widths`` have hence the character of reserved keywords and cannot be used for group and dataset names when working with the JSON backend. Datasets and groups have the same namespace, meaning that there may not be a subgroup and a dataset with the same name contained in one group. -Additionally to the two mentioned keys, the top-level group stores information about -the byte widths specific to the writing platform behind the key ``platform_byte_widths``. -Will be overwritten every time that a JSON value is stored to disk, hence this information -is only available about the last platform writing the JSON value. +Any **openPMD dataset** is a JSON object with three keys: -Any **openPMD dataset** is a JSON object with five keys: - - * ``object_type`` For datasets, this key points to the string ``"dataset"``. This makes - it possible to distinguish datasets from groups. * ``attributes``: Attributes associated with the dataset. May be ``null`` or not present if no attributes are associated with the dataset. * ``datatype``: A string describing the type of the stored data. - * ``extent``: A JSON array describing the extent of the dataset in every dimension. * ``data`` A nested array storing the actual data in row-major manner. The data needs to be consistent with the fields ``datatype`` and ``extent``. + Checking whether this key points to an array can be (and is internally) used to distinguish groups from datasets. **Attributes** are stored as a JSON object with a key for each attribute. Every such attribute is itself a JSON object with two keys: diff --git a/docs/source/backends/json_example.json b/docs/source/backends/json_example.json index e23a69c8e6..2fad4fc451 100644 --- a/docs/source/backends/json_example.json +++ b/docs/source/backends/json_example.json @@ -42,7 +42,6 @@ } }, "meshes": { - "object_type": "group", "rho": { "attributes": { "axisLabels": { @@ -119,17 +118,10 @@ 8 ] ], - "datatype": "DOUBLE", - "extent": [ - 3, - 3 - ], - "object_type": "dataset" + "datatype": "DOUBLE" } - }, - "object_type": "group" - }, - "object_type": "group" + } + } }, "platform_byte_widths": { "BOOL": 1, diff --git a/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp b/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp index 1e4a8f8b27..ef4e1a2154 100644 --- a/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp +++ b/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp @@ -404,9 +404,10 @@ namespace openPMD File ); - static bool isGroup( nlohmann::json const & j ); + // need to check the name too in order to exclude "attributes" key + static bool isGroup( nlohmann::json::const_iterator it ); - static bool isDataset( nlohmann::json const & j ); + static bool isDataset( nlohmann::json const & j ); // check whether the json reference contains a valid dataset diff --git a/src/IO/JSON/JSONIOHandlerImpl.cpp b/src/IO/JSON/JSONIOHandlerImpl.cpp index d3f40001a2..cb26694d26 100644 --- a/src/IO/JSON/JSONIOHandlerImpl.cpp +++ b/src/IO/JSON/JSONIOHandlerImpl.cpp @@ -214,7 +214,6 @@ namespace openPMD ); auto & dset = jsonVal[name]; dset["datatype"] = datatypeToString( parameter.dtype ); - dset["object_type"] = "dataset"; dset["data"] = initializeNDArray( parameter.extent ); writable->written = true; m_dirty.emplace( file ); @@ -719,7 +718,7 @@ namespace openPMD ->clear( ); for( auto it = j.begin( ); it != j.end( ); it++ ) { - if( isGroup( it.value( ) ) ) + if( isGroup( it ) ) { parameters.paths ->push_back( it.key( ) ); @@ -742,7 +741,7 @@ namespace openPMD ->clear( ); for( auto it = j.begin( ); it != j.end( ); it++ ) { - if( isDataset( it.value( ) ) ) + if( isDataset( it.value() ) ) { parameters.datasets ->push_back( it.key( ) ); @@ -918,17 +917,20 @@ namespace openPMD { // idea: begin from the innermost shale and copy the result into the // outer shales - nlohmann::json accum; // null + nlohmann::json accum; + nlohmann::json old; + auto * accum_ptr = & accum; + auto * old_ptr = & old; for( auto it = extent.rbegin( ); it != extent.rend( ); it++ ) { - nlohmann::json old = accum; - accum = nlohmann::json {}; + std::swap(old_ptr, accum_ptr); + *accum_ptr = nlohmann::json {}; for( Extent::value_type i = 0; i < *it; i++ ) { - accum[i] = old; // copy boi + (*accum_ptr)[i] = *old_ptr; // copy boi } } - return accum; + return *accum_ptr; } @@ -994,11 +996,14 @@ namespace openPMD ); for( std::string & group: groups ) { - // This also enforces a JSON object + // Enforce a JSON object // the library will automatically create a list if the first // key added to it is parseable as an int jsonp = &( *jsonp )[group]; - ( *jsonp )["object_type"] = "group"; + if (jsonp->is_null()) + { + *jsonp = nlohmann::json::object(); + } } } @@ -1231,19 +1236,20 @@ namespace openPMD { return false; } - auto it = j.find( "object_type" ); - return it != j.end( ) && it.value( ) == "dataset"; + auto i = j.find( "data" ); + return i != j.end( ) && i.value( ).is_array(); } - bool JSONIOHandlerImpl::isGroup( nlohmann::json const & j ) + bool JSONIOHandlerImpl::isGroup( nlohmann::json::const_iterator it ) { - if( !j.is_object( ) ) + auto & j = it.value(); + if( it.key() == "attributes" || it.key() == "platform_byte_widths" || !j.is_object( ) ) { return false; } - auto it = j.find( "object_type" ); - return it != j.end( ) && it.value( ) == "group"; + auto i = j.find( "data" ); + return i == j.end( ) || !i.value( ).is_array(); } @@ -1253,7 +1259,7 @@ namespace openPMD nlohmann::json & j ) { - VERIFY_ALWAYS( j["object_type"] == "dataset", + VERIFY_ALWAYS( isDataset(j), "Specified dataset does not exist or is not a dataset." ); try