| 345 | // --------------------------------------------------------------------------- |
| 346 | |
| 347 | std::unique_ptr<MasterFile> MasterFile::parse(const std::string &text) { |
| 348 | std::unique_ptr<MasterFile> dmmf(new MasterFile()); |
| 349 | json j; |
| 350 | try { |
| 351 | j = json::parse(text); |
| 352 | } catch (const std::exception &e) { |
| 353 | throw ParsingException(e.what()); |
| 354 | } |
| 355 | if (!j.is_object()) { |
| 356 | throw ParsingException("Not an object"); |
| 357 | } |
| 358 | dmmf->mFileType = getReqString(j, "file_type"); |
| 359 | dmmf->mFormatVersion = getReqString(j, "format_version"); |
| 360 | dmmf->mName = getOptString(j, "name"); |
| 361 | dmmf->mVersion = getOptString(j, "version"); |
| 362 | dmmf->mLicense = getOptString(j, "license"); |
| 363 | dmmf->mDescription = getOptString(j, "description"); |
| 364 | dmmf->mPublicationDate = getOptString(j, "publication_date"); |
| 365 | |
| 366 | if (j.contains("authority")) { |
| 367 | const json jAuthority = j["authority"]; |
| 368 | if (!jAuthority.is_object()) { |
| 369 | throw ParsingException("authority is not a object"); |
| 370 | } |
| 371 | dmmf->mAuthority.name = getOptString(jAuthority, "name"); |
| 372 | dmmf->mAuthority.url = getOptString(jAuthority, "url"); |
| 373 | dmmf->mAuthority.address = getOptString(jAuthority, "address"); |
| 374 | dmmf->mAuthority.email = getOptString(jAuthority, "email"); |
| 375 | } |
| 376 | |
| 377 | if (j.contains("links")) { |
| 378 | const json jLinks = j["links"]; |
| 379 | if (!jLinks.is_array()) { |
| 380 | throw ParsingException("links is not an array"); |
| 381 | } |
| 382 | for (const json &jLink : jLinks) { |
| 383 | if (!jLink.is_object()) { |
| 384 | throw ParsingException("links[] item is not an object"); |
| 385 | } |
| 386 | Link link; |
| 387 | link.href = getOptString(jLink, "href"); |
| 388 | link.rel = getOptString(jLink, "rel"); |
| 389 | link.type = getOptString(jLink, "type"); |
| 390 | link.title = getOptString(jLink, "title"); |
| 391 | dmmf->mLinks.emplace_back(std::move(link)); |
| 392 | } |
| 393 | } |
| 394 | dmmf->mSourceCRS = getReqString(j, "source_crs"); |
| 395 | dmmf->mTargetCRS = getReqString(j, "target_crs"); |
| 396 | dmmf->mDefinitionCRS = getReqString(j, "definition_crs"); |
| 397 | if (dmmf->mSourceCRS != dmmf->mDefinitionCRS) { |
| 398 | throw ParsingException( |
| 399 | "source_crs != definition_crs not currently supported"); |
| 400 | } |
| 401 | dmmf->mReferenceEpoch = getOptString(j, "reference_epoch"); |
| 402 | dmmf->mUncertaintyReferenceEpoch = |
| 403 | getOptString(j, "uncertainty_reference_epoch"); |
| 404 | dmmf->mHorizontalOffsetUnit = getOptString(j, "horizontal_offset_unit"); |