106 std::shared_ptr<util::EntityMap> m_entityMapping;
136 inputStream >> *
root();
153 std::ifstream jsonFile;
154 jsonFile.exceptions(std::ios::failbit | std::ios::badbit);
155 jsonFile.open(inputPath);
156 if (!jsonFile.is_open()) {
157 throw std::runtime_error(
"Unable to open JSON file: " + inputPath.string());
171 template <
typename Byte,
172 typename = std::enable_if_t<
173 std::is_integral_v<Byte> &&
174 (
sizeof(Byte) == 1) &&
175 !std::is_same_v<std::remove_cv_t<Byte>,
bool>>>
178 if (!data && size != 0) {
179 throw std::invalid_argument(
"JSON buffer pointer is null.");
181 auto root = std::make_shared<json>(nlohmann::json::parse(data, data + size));
191 void save(
const std::filesystem::path& outputPath, std::optional<int> indentSpaces)
const
194 file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
195 file.open(outputPath, std::ios::out | std::ios::binary);
196 if (!file.is_open()) {
197 throw std::runtime_error(
"Unable to write to JSON file: " + outputPath.string());
199 file <<
root()->dump(indentSpaces.value_or(-1));
208 const std::optional<ErrorHandler>& errorHandler = std::nullopt);
213 MNX_ASSERT_IF(!m_entityMapping) {
214 throw std::logic_error(
"Call buildEntityMap before calling getEntityMap.");
216 return *m_entityMapping;
220 [[nodiscard]]
bool hasEntityMap()
const {
return static_cast<bool>(m_entityMapping); }
228static_assert(std::is_move_constructible<mnx::Document>::value,
"Document must be move constructible");
Represents an MNX array, encapsulating property access.
Definition BaseTypes.h:499
Base class wrapper for all MNX JSON nodes.
Definition BaseTypes.h:198
T parent() const
Returns the parent object for this node.
Definition BaseTypes.h:246
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:280
Represents an MNX document and provides methods for loading and saving.
Definition Document.h:103
void save(const std::filesystem::path &outputPath, std::optional< int > indentSpaces) const
Saves the MNX document to a file.
Definition Document.h:191
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:176
static Document create(const std::filesystem::path &inputPath)
Creates a Document from a JSON file.
Definition Document.h:151
Document(const Document &src)
Copy constructor that zaps the id mapping, if any.
Definition Document.h:126
Document()
Constructs an empty MNX document. The resulting instance contains all required fields and should vali...
Definition Document.h:113
Document(const std::shared_ptr< json > &root)
Wrap a document around a root element.
Definition Document.h:123
const util::EntityMap & getEntityMap() const
Gets a reference to the entity mapping instance for the document.
Definition Document.h:211
Document(std::istream &inputStream)
Constructs a Document from an input stream.
Definition Document.h:134
bool hasEntityMap() const
Returns whether an entity mapping currently exists.
Definition Document.h:220
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:129
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:404
MNX_REQUIRED_CHILD(MnxMetaData, mnx)
Metadata for the MNX document.
const std::shared_ptr< json > & root() const
Returns the root.
Definition BaseTypes.h:280
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:404
Represents an MNX object, encapsulating property access.
Definition BaseTypes.h:412
Object(const std::shared_ptr< json > &root, json_pointer pointer)
Wraps an Object class around an existing JSON object node.
Definition BaseTypes.h:417
void setExtension(const std::string &key, const json &value)
Sets a vendor extension value in _x, creating _x when needed.
Definition BaseTypes.h:435
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
const std::string & getMnxSchemaId()
Returns the MNX schema id from the embedded schema.
Definition SchemaValidate.cpp:76
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:47
bool ottavasRespectVoiceTargets
When false, ottava spans ignore voice-specific targeting (ottavas apply to the entire staff).
Definition Document.h:51
bool ottavasRespectGraceTargets
When false, ottava spans ignore grace-note targeting (graces follow the main event).
Definition Document.h:49