summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-12 00:50:17 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-12 00:50:17 +0100
commit51684c7207a119600c1f818baa13abc64d609690 (patch)
tree1afd5c499df3e1f844509b34c8f0453382ce24d5 /src
parente9a0eaaef261b99e0a1e605f3c77285eeaa2bddd (diff)
Improved error messages of VariantConverter
Diffstat (limited to 'src')
-rw-r--r--src/core/common/VariantConverter.cpp33
-rw-r--r--src/core/common/VariantWriter.cpp11
-rw-r--r--src/core/common/VariantWriter.hpp13
3 files changed, 40 insertions, 17 deletions
diff --git a/src/core/common/VariantConverter.cpp b/src/core/common/VariantConverter.cpp
index 2d6d286..1f5f514 100644
--- a/src/core/common/VariantConverter.cpp
+++ b/src/core/common/VariantConverter.cpp
@@ -31,18 +31,20 @@
namespace ousia {
-static std::string msgUnexpectedType(VariantType actualType,
+static std::string msgUnexpectedType(const Variant &v,
VariantType requestedType)
{
- return std::string("Cannot convert ") + Variant::getTypeName(actualType) +
- std::string(" to ") + Variant::getTypeName(requestedType);
+ return std::string("Cannot convert ") + v.getTypeName() + std::string(" ") +
+ VariantWriter::writeJsonToString(v, false) + std::string(" to ") +
+ Variant::getTypeName(requestedType);
}
static std::string msgImplicitConversion(VariantType actualType,
- VariantType requestedType)
+ VariantType requestedType)
{
- return std::string("Implicit conversion from ") + Variant::getTypeName(actualType) +
- std::string(" to ") + Variant::getTypeName(requestedType);
+ return std::string("Implicit conversion from ") +
+ Variant::getTypeName(actualType) + std::string(" to ") +
+ Variant::getTypeName(requestedType);
}
bool VariantConverter::toBool(Variant &var, Logger &logger, Mode mode)
@@ -76,7 +78,7 @@ bool VariantConverter::toBool(Variant &var, Logger &logger, Mode mode)
}
// No conversion possible, assign default value and log error
- logger.error(msgUnexpectedType(var.getType(), VariantType::BOOL));
+ logger.error(msgUnexpectedType(var, VariantType::BOOL));
var = false;
return false;
}
@@ -132,7 +134,7 @@ bool VariantConverter::toInt(Variant &var, Logger &logger, Mode mode)
}
// No conversion possible, assign default value and log error
- logger.error(msgUnexpectedType(var.getType(), VariantType::INT));
+ logger.error(msgUnexpectedType(var, VariantType::INT));
var = 0;
return false;
}
@@ -186,7 +188,7 @@ bool VariantConverter::toDouble(Variant &var, Logger &logger, Mode mode)
}
// No conversion possible, assign default value and log error
- logger.error(msgUnexpectedType(var.getType(), VariantType::DOUBLE));
+ logger.error(msgUnexpectedType(var, VariantType::DOUBLE));
var = 0.0;
return false;
}
@@ -260,7 +262,7 @@ bool VariantConverter::toString(Variant &var, Logger &logger, Mode mode)
}
// No conversion possible, assign default value and log error
- logger.error(msgUnexpectedType(var.getType(), VariantType::STRING));
+ logger.error(msgUnexpectedType(var, VariantType::STRING));
var = "";
return false;
}
@@ -296,7 +298,7 @@ bool VariantConverter::toArray(Variant &var, const RttiType &innerType,
}
// No conversion possible, assign the default value and log an error
- logger.error(msgUnexpectedType(var.getType(), VariantType::ARRAY));
+ logger.error(msgUnexpectedType(var, VariantType::ARRAY));
var.setArray(Variant::arrayType{});
return false;
}
@@ -327,7 +329,7 @@ bool VariantConverter::toMap(Variant &var, const RttiType &innerType,
}
// No conversion possible, assign the default value and log an error
- logger.error(msgUnexpectedType(var.getType(), VariantType::MAP));
+ logger.error(msgUnexpectedType(var, VariantType::MAP));
var.setMap(Variant::mapType{});
return false;
}
@@ -339,7 +341,7 @@ bool VariantConverter::toFunction(Variant &var, Logger &logger)
}
// No conversion possible, assign the default value and log an error
- logger.error(msgUnexpectedType(var.getType(), VariantType::MAP));
+ logger.error(msgUnexpectedType(var, VariantType::MAP));
var.setFunction(std::shared_ptr<Function>{new Method<void>([](
const Variant::arrayType &args, void *thisRef) { return Variant{}; })});
return false;
@@ -356,8 +358,7 @@ bool VariantConverter::convert(Variant &var, const RttiType &type,
} else if (&type == &RttiTypes::Nullptr) {
// Make sure the variant is set to null
if (!var.isNull()) {
- logger.error(
- msgUnexpectedType(var.getType(), VariantType::NULLPTR));
+ logger.error(msgUnexpectedType(var, VariantType::NULLPTR));
var.setNull();
return false;
}
@@ -381,7 +382,7 @@ bool VariantConverter::convert(Variant &var, const RttiType &type,
// If none of the above primitive types is requested, we were
// obviously asked for a managed object.
if (!var.isObject()) {
- logger.error(msgUnexpectedType(var.getType(), VariantType::OBJECT));
+ logger.error(msgUnexpectedType(var, VariantType::OBJECT));
var.setNull();
return false;
}
diff --git a/src/core/common/VariantWriter.cpp b/src/core/common/VariantWriter.cpp
index 713ec01..fc23359 100644
--- a/src/core/common/VariantWriter.cpp
+++ b/src/core/common/VariantWriter.cpp
@@ -16,6 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <sstream>
+
#include "Variant.hpp"
#include "VariantWriter.hpp"
@@ -161,5 +163,14 @@ void VariantWriter::writeJson(const Variant &var, std::ostream &stream,
{
writeJsonInternal(var, stream, pretty, 0);
}
+
+std::string VariantWriter::writeJsonToString(const Variant &var, bool pretty)
+{
+ std::stringstream ss;
+ writeJson(var, ss, pretty);
+ return ss.str();
+}
+
+
}
diff --git a/src/core/common/VariantWriter.hpp b/src/core/common/VariantWriter.hpp
index 211da34..7fe32fb 100644
--- a/src/core/common/VariantWriter.hpp
+++ b/src/core/common/VariantWriter.hpp
@@ -28,6 +28,7 @@
#ifndef _OUSIA_VARIANT_WRITER_HPP_
#define _OUSIA_VARIANT_WRITER_HPP_
+#include <string>
#include <ostream>
namespace ousia {
@@ -42,7 +43,8 @@ class Variant;
class VariantWriter {
public:
/**
- * Dumps the Variant as JSON data.
+ * Dumps the Variant as JSON data. Note that the resulting JSON data is
+ * invalid if the Variant consists of function or object references.
*
* @param var is the variant that should be serialized.
* @param stream is the stream the result should be written to.
@@ -50,6 +52,15 @@ public:
*/
static void writeJson(const Variant &var, std::ostream &stream,
bool pretty = true);
+
+ /**
+ * Dumps the Variant as JSON data to a string.
+ *
+ * @param var is the variant that should be serialized.
+ * @param pretty if true, the resulting value is properly indented.
+ */
+ static std::string writeJsonToString(const Variant &var, bool pretty = true);
+
};
}