28#include "MnxdomProvenance.h"
72 auto extensions = ensure__x();
100 [[nodiscard]] std::optional<MnxdomExtensionData>
mnxdom()
const
111 if (
auto existing =
mnxdom()) {
115 auto extensions = ensure__x();
123 if (
auto extensions = _x(); extensions && extensions->empty()) {
137 std::shared_ptr<util::EntityMap> m_entityMapping;
167 inputStream >> *
root();
184 std::ifstream jsonFile;
185 jsonFile.exceptions(std::ios::failbit | std::ios::badbit);
186 jsonFile.open(inputPath);
187 if (!jsonFile.is_open()) {
188 throw std::runtime_error(
"Unable to open JSON file: " + inputPath.string());
202 template <
typename Byte,
203 typename = std::enable_if_t<
204 std::is_integral_v<Byte> &&
205 (
sizeof(Byte) == 1) &&
206 !std::is_same_v<std::remove_cv_t<Byte>,
bool>>>
209 if (!data && size != 0) {
210 throw std::invalid_argument(
"JSON buffer pointer is null.");
212 auto root = std::make_shared<json>(nlohmann::json::parse(data, data + size));
222 void save(
const std::filesystem::path& outputPath, std::optional<int> indentSpaces)
const
225 file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
226 file.open(outputPath, std::ios::out | std::ios::binary);
227 if (!file.is_open()) {
228 throw std::runtime_error(
"Unable to write to JSON file: " + outputPath.string());
230 file <<
root()->dump(indentSpaces.value_or(-1));
239 const std::optional<ErrorHandler>& errorHandler = std::nullopt);
244 MNX_ASSERT_IF(!m_entityMapping) {
245 throw std::logic_error(
"Call buildEntityMap before calling getEntityMap.");
247 return *m_entityMapping;
251 [[nodiscard]]
bool hasEntityMap()
const {
return static_cast<bool>(m_entityMapping); }
259static_assert(std::is_move_constructible<mnx::Document>::value,
"Document must be move constructible");
Represents an MNX array, encapsulating property access.
Definition BaseTypes.h:531
Base class wrapper for all MNX JSON nodes.
Definition BaseTypes.h:212
json_pointer pointer() const
Returns the json_pointer for this node.
Definition BaseTypes.h:274
T parent() const
Returns the parent object for this node.
Definition BaseTypes.h:260
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:297
Represents an MNX document and provides methods for loading and saving.
Definition Document.h:134
void save(const std::filesystem::path &outputPath, std::optional< int > indentSpaces) const
Saves the MNX document to a file.
Definition Document.h:222
MNX_OPTIONAL_CHILD(Array< Layout >, layouts)
List of layouts for the MNX document.
MNX_OPTIONAL_CHILD(Array< Score >, scores)
List of scores for the MNX document.
MNX_REQUIRED_CHILD(Array< Part >, parts)
List of parts for the MNX document.
static Document create(const Byte *data, std::size_t size)
Creates a Document from a data buffer containing JSON.
Definition Document.h:207
static Document create(const std::filesystem::path &inputPath)
Creates a Document from a JSON file.
Definition Document.h:182
Document(const Document &src)
Copy constructor that zaps the id mapping, if any.
Definition Document.h:157
Document()
Constructs an empty MNX document. The resulting instance contains all required fields and should vali...
Definition Document.h:144
Document(const std::shared_ptr< json > &root)
Wrap a document around a root element.
Definition Document.h:154
const util::EntityMap & getEntityMap() const
Gets a reference to the entity mapping instance for the document.
Definition Document.h:242
Document(std::istream &inputStream)
Constructs a Document from an input stream.
Definition Document.h:165
bool hasEntityMap() const
Returns whether an entity mapping currently exists.
Definition Document.h:251
void buildEntityMap(EntityMapPolicies policies={}, const std::optional< ErrorHandler > &errorHandler=std::nullopt)
Builds or rebuilds the ID mapping for the document, replacing any existing mapping.
Definition Implementations.cpp:130
std::optional< Layout > findFullScoreLayout() const
Finds a layout that matches the canonical full score layout, where each part staff appears in order o...
Definition Implementations.cpp:372
MNX_REQUIRED_CHILD(MnxMetaData, mnx)
Metadata for the MNX document.
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:297
MNX_REQUIRED_CHILD(Global, global)
Global data for the MNX document.
Represents the global section of an MNX document, containing global measures.
Definition Global.h:405
Provenance data stored under _x.mnxdom.
Definition MnxdomProvenance.h:125
Represents an MNX object, encapsulating property access.
Definition BaseTypes.h:429
void clearExtension(const std::string_view key)
Removes a vendor extension value from _x, if it exists.
Definition BaseTypes.h:475
Object()
Constructor fresh document or detached instance.
Definition BaseTypes.h:432
std::optional< json > getExtension(const std::string_view key) const
Gets a vendor extension value from _x.
Definition BaseTypes.h:463
Provides type-safe ID-based lookup for elements in an MNX document.
Definition EntityMap.h:109
object model for MNX format
nlohmann::json json
JSON class for MNX.
Definition BaseTypes.h:67
json::json_pointer json_pointer
JSON pointer class for MNX.
Definition BaseTypes.h:68
constexpr std::string_view MNXDOM_PROVENANCE_KEY
Key used for the mnxdom provenance payload inside _x.
Definition MnxdomProvenance.h:35
int getMnxSchemaVersion()
Returns the MNX version extracted from the trailing segment of schema $id.
Definition SchemaValidate.cpp:81
Controls optional behaviors when building an EntityMap.
Definition Document.h:48
bool ottavasRespectVoiceTargets
When false, ottava spans ignore voice-specific targeting (ottavas apply to the entire staff).
Definition Document.h:52
bool ottavasRespectGraceTargets
When false, ottava spans ignore grace-note targeting (graces follow the main event).
Definition Document.h:50