src/utilities/core/Json.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Json.hpp" | ||
| 7 | |||
| 8 | #include "Assert.hpp" | ||
| 9 | #include "PathHelpers.hpp" | ||
| 10 | #include "Logger.hpp" | ||
| 11 | #include "FilesystemHelpers.hpp" | ||
| 12 | |||
| 13 | #include <OpenStudio.hxx> | ||
| 14 | |||
| 15 | namespace openstudio { | ||
| 16 | |||
| 17 | // assert key is present | ||
| 18 | 352064 | void assertKey(const Json::Value& value, const std::string& key) { | |
| 19 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 352064 times.
|
352064 | if (!checkKey(value, key)) { |
| 20 | ✗ | throw openstudio::Exception(std::string("Cannot find key '" + key + "'")); | |
| 21 | } | ||
| 22 | 352064 | } | |
| 23 | |||
| 24 | // assert type is correct if key is present | ||
| 25 | 945272 | void assertType(const Json::Value& value, const std::string& key, const Json::ValueType& valueType) { | |
| 26 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 945272 times.
|
945272 | if (!checkType(value, key, valueType)) { |
| 27 | ✗ | throw openstudio::Exception(std::string("Key '" + key + "' is of wrong type")); | |
| 28 | } | ||
| 29 | 945272 | } | |
| 30 | |||
| 31 | // assert key is present and type is correct | ||
| 32 | 352023 | void assertKeyAndType(const Json::Value& value, const std::string& key, const Json::ValueType& valueType) { | |
| 33 | 352023 | assertKey(value, key); | |
| 34 | 352023 | assertType(value, key, valueType); | |
| 35 | 352023 | } | |
| 36 | |||
| 37 | /// check key is present, return false if key is not found | ||
| 38 | 390731 | bool checkKey(const Json::Value& value, const std::string& key) { | |
| 39 |
2/2✓ Branch 1 taken 33791 times.
✓ Branch 2 taken 356940 times.
|
390731 | if (!value.isMember(key)) { |
| 40 | 33791 | return false; | |
| 41 | } | ||
| 42 | 356940 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | /// check type is correct if key is present, return false if type is not correct | ||
| 46 | 945374 | bool checkType(const Json::Value& value, const std::string& key, const Json::ValueType& valueType) { | |
| 47 |
2/2✓ Branch 1 taken 931469 times.
✓ Branch 2 taken 13905 times.
|
945374 | if (value.isMember(key)) { |
| 48 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 931469 times.
|
931469 | if (!value[key].isConvertibleTo(valueType)) { |
| 49 | ✗ | return false; | |
| 50 | } | ||
| 51 |
2/2✓ Branch 0 taken 931463 times.
✓ Branch 1 taken 6 times.
|
931469 | if (valueType != Json::nullValue) { |
| 52 |
2/2✓ Branch 2 taken 6 times.
✓ Branch 3 taken 931457 times.
|
931463 | if (value[key].isNull()) { |
| 53 | 6 | return false; | |
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | 945368 | return true; | |
| 58 | } | ||
| 59 | |||
| 60 | /// check key is present and type is correct, return false if key not found or type is not correct | ||
| 61 | 107463 | bool checkKeyAndType(const Json::Value& value, const std::string& key, const Json::ValueType& valueType) { | |
| 62 |
2/2✓ Branch 1 taken 91383 times.
✓ Branch 2 taken 16080 times.
|
107463 | if (value.isMember(key)) { |
| 63 |
1/2✓ Branch 2 taken 91383 times.
✗ Branch 3 not taken.
|
91383 | if (value[key].isConvertibleTo(valueType)) { |
| 64 |
2/2✓ Branch 2 taken 57454 times.
✓ Branch 3 taken 33929 times.
|
91383 | if (value[key].isNull()) { |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57454 times.
|
57454 | if (valueType == Json::nullValue) { |
| 66 | ✗ | return true; | |
| 67 | } else { | ||
| 68 | 57454 | return false; | |
| 69 | } | ||
| 70 | } | ||
| 71 | // not null and is convertible | ||
| 72 | 33929 | return true; | |
| 73 | } else { | ||
| 74 | // not convertible to valueType | ||
| 75 | ✗ | LOG_FREE(Warn, "JSON", "Key '" << key << "' exists but is not the correct type"); | |
| 76 | } | ||
| 77 | } | ||
| 78 | 16080 | return false; | |
| 79 | } | ||
| 80 | |||
| 81 | } // namespace openstudio | ||
| 82 |