Mercurial > hg > orthanc
changeset 5380:97004471a5c5
Toolbox : more set functions (cont)
author | Alain Mazy <am@osimis.io> |
---|---|
date | Thu, 31 Aug 2023 16:34:53 +0200 |
parents | b31c73bc7cb6 |
children | be0b9c407985 |
files | OrthancFramework/Sources/SerializationToolbox.cpp OrthancFramework/Sources/SerializationToolbox.h |
diffstat | 2 files changed, 43 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Sources/SerializationToolbox.cpp Thu Aug 31 15:20:41 2023 +0200 +++ b/OrthancFramework/Sources/SerializationToolbox.cpp Thu Aug 31 16:34:53 2023 +0200 @@ -170,18 +170,18 @@ void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target, - const Json::Value& value, + const Json::Value& valueObject, const std::string& field) { - if (value.type() != Json::objectValue || - !value.isMember(field.c_str()) || - value[field.c_str()].type() != Json::arrayValue) + if (valueObject.type() != Json::objectValue || + !valueObject.isMember(field.c_str()) || + valueObject[field.c_str()].type() != Json::arrayValue) { throw OrthancException(ErrorCode_BadFileFormat, "List of strings expected in field: " + field); } - const Json::Value& arr = value[field.c_str()]; + const Json::Value& arr = valueObject[field.c_str()]; try { @@ -196,26 +196,26 @@ void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target, - const Json::Value& array) + const Json::Value& valueArray) { - if (array.type() != Json::arrayValue) + if (valueArray.type() != Json::arrayValue) { throw OrthancException(ErrorCode_BadFileFormat, "List of strings expected"); } - target.resize(array.size()); + target.resize(valueArray.size()); - for (Json::Value::ArrayIndex i = 0; i < array.size(); i++) + for (Json::Value::ArrayIndex i = 0; i < valueArray.size(); i++) { - if (array[i].type() != Json::stringValue) + if (valueArray[i].type() != Json::stringValue) { throw OrthancException(ErrorCode_BadFileFormat, "List of strings expected"); } else { - target[i] = array[i].asString(); + target[i] = valueArray[i].asString(); } } } @@ -237,11 +237,11 @@ void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target, - const Json::Value& value, + const Json::Value& valueObject, const std::string& field) { std::vector<std::string> tmp; - ReadArrayOfStrings(tmp, value, field); + ReadArrayOfStrings(tmp, valueObject, field); target.clear(); for (size_t i = 0; i < tmp.size(); i++) @@ -252,10 +252,10 @@ void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target, - const Json::Value& value) + const Json::Value& valueArray) { std::vector<std::string> tmp; - ReadArrayOfStrings(tmp, value); + ReadArrayOfStrings(tmp, valueArray); target.clear(); for (size_t i = 0; i < tmp.size(); i++) @@ -414,24 +414,38 @@ } - void SerializationToolbox::WriteSetOfStrings(Json::Value& target, + void SerializationToolbox::WriteSetOfStrings(Json::Value& targetObject, const std::set<std::string>& values, const std::string& field) { - if (target.type() != Json::objectValue || - target.isMember(field.c_str())) + if (targetObject.type() != Json::objectValue || + targetObject.isMember(field.c_str())) { throw OrthancException(ErrorCode_BadFileFormat); } - Json::Value& value = target[field]; + Json::Value& targetArray = targetObject[field]; + + targetArray = Json::arrayValue; + + WriteSetOfStrings(targetArray, values); + } + - value = Json::arrayValue; + void SerializationToolbox::WriteSetOfStrings(Json::Value& targetArray, + const std::set<std::string>& values) + { + if (targetArray.type() != Json::arrayValue) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + + targetArray.clear(); for (std::set<std::string>::const_iterator it = values.begin(); it != values.end(); ++it) { - value.append(*it); + targetArray.append(*it); } }
--- a/OrthancFramework/Sources/SerializationToolbox.h Thu Aug 31 15:20:41 2023 +0200 +++ b/OrthancFramework/Sources/SerializationToolbox.h Thu Aug 31 16:34:53 2023 +0200 @@ -60,22 +60,22 @@ const std::string& field); static void ReadArrayOfStrings(std::vector<std::string>& target, - const Json::Value& value, + const Json::Value& valueObject, const std::string& field); static void ReadArrayOfStrings(std::vector<std::string>& target, - const Json::Value& value); + const Json::Value& valueArray); static void ReadListOfStrings(std::list<std::string>& target, const Json::Value& value, const std::string& field); static void ReadSetOfStrings(std::set<std::string>& target, - const Json::Value& value, + const Json::Value& valueObject, const std::string& field); static void ReadSetOfStrings(std::set<std::string>& target, - const Json::Value& value); + const Json::Value& valueArray); static void ReadSetOfTags(std::set<DicomTag>& target, const Json::Value& value, @@ -97,10 +97,13 @@ const std::list<std::string>& values, const std::string& field); - static void WriteSetOfStrings(Json::Value& target, + static void WriteSetOfStrings(Json::Value& targetObject, const std::set<std::string>& values, const std::string& field); + static void WriteSetOfStrings(Json::Value& targetArray, + const std::set<std::string>& values); + static void WriteSetOfTags(Json::Value& target, const std::set<DicomTag>& tags, const std::string& field);