Mercurial > hg > orthanc
comparison Core/Toolbox.cpp @ 2071:27fd34970c52
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 14 Jul 2016 14:21:29 +0200 |
parents | 272094362301 |
children | 14ce887a9182 |
comparison
equal
deleted
inserted
replaced
2070:7e6afa0beaf6 | 2071:27fd34970c52 |
---|---|
1588 target.push_back('%'); | 1588 target.push_back('%'); |
1589 target.push_back(a < 10 ? a + '0' : a - 10 + 'A'); | 1589 target.push_back(a < 10 ? a + '0' : a - 10 + 'A'); |
1590 target.push_back(b < 10 ? b + '0' : b - 10 + 'A'); | 1590 target.push_back(b < 10 ? b + '0' : b - 10 + 'A'); |
1591 } | 1591 } |
1592 } | 1592 } |
1593 } | 1593 } |
1594 | |
1595 | |
1596 static bool HasField(const Json::Value& json, | |
1597 const std::string& key, | |
1598 Json::ValueType expectedType) | |
1599 { | |
1600 if (json.type() != Json::objectValue || | |
1601 !json.isMember(key)) | |
1602 { | |
1603 return false; | |
1604 } | |
1605 else if (json[key].type() == expectedType) | |
1606 { | |
1607 return true; | |
1608 } | |
1609 else | |
1610 { | |
1611 throw OrthancException(ErrorCode_BadParameterType); | |
1612 } | |
1613 } | |
1614 | |
1615 | |
1616 std::string Toolbox::GetJsonStringField(const Json::Value& json, | |
1617 const std::string& key, | |
1618 const std::string& defaultValue) | |
1619 { | |
1620 if (HasField(json, key, Json::stringValue)) | |
1621 { | |
1622 return json[key].asString(); | |
1623 } | |
1624 else | |
1625 { | |
1626 return defaultValue; | |
1627 } | |
1628 } | |
1629 | |
1630 | |
1631 bool Toolbox::GetJsonBooleanField(const ::Json::Value& json, | |
1632 const std::string& key, | |
1633 bool defaultValue) | |
1634 { | |
1635 if (HasField(json, key, Json::booleanValue)) | |
1636 { | |
1637 return json[key].asBool(); | |
1638 } | |
1639 else | |
1640 { | |
1641 return defaultValue; | |
1642 } | |
1643 } | |
1644 | |
1645 | |
1646 int Toolbox::GetJsonIntegerField(const ::Json::Value& json, | |
1647 const std::string& key, | |
1648 int defaultValue) | |
1649 { | |
1650 if (HasField(json, key, Json::intValue)) | |
1651 { | |
1652 return json[key].asInt(); | |
1653 } | |
1654 else | |
1655 { | |
1656 return defaultValue; | |
1657 } | |
1658 } | |
1659 | |
1660 | |
1661 unsigned int Toolbox::GetJsonUnsignedIntegerField(const ::Json::Value& json, | |
1662 const std::string& key, | |
1663 unsigned int defaultValue) | |
1664 { | |
1665 int v = GetJsonIntegerField(json, key, defaultValue); | |
1666 | |
1667 if (v < 0) | |
1668 { | |
1669 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
1670 } | |
1671 else | |
1672 { | |
1673 return static_cast<unsigned int>(v); | |
1674 } | |
1675 } | |
1594 } | 1676 } |