fix vectToString such that it outputs valid JSON (#7749)

Co-authored-by: ptosco <paolo.tosco@novartis.com>
This commit is contained in:
Paolo Tosco
2024-08-26 16:27:03 +02:00
committed by GitHub
parent 0f4f6fd488
commit ce4a28870f
2 changed files with 12 additions and 9 deletions

View File

@@ -32,7 +32,7 @@
#ifndef RDKIT_RDVALUE_H
#define RDKIT_RDVALUE_H
//#define UNSAFE_RDVALUE
// #define UNSAFE_RDVALUE
#ifdef UNSAFE_RDVALUE
#include "RDValue-doublemagic.h"
#else
@@ -179,7 +179,10 @@ std::string vectToString(RDValue val) {
sstr.imbue(std::locale("C"));
sstr << std::setprecision(17);
sstr << "[";
std::copy(tv.begin(), tv.end(), std::ostream_iterator<T>(sstr, ","));
if (!tv.empty()) {
std::copy(tv.begin(), tv.end() - 1, std::ostream_iterator<T>(sstr, ","));
sstr << tv.back();
}
sstr << "]";
return sstr.str();
}

View File

@@ -424,7 +424,7 @@ void testVectToString() {
d.setVal("foo", v);
std::string sv;
d.getVal("foo", sv);
TEST_ASSERT(sv == "[1,0,]");
TEST_ASSERT(sv == "[1,0]");
}
{
Dict d;
@@ -434,9 +434,9 @@ void testVectToString() {
d.setVal("foo", v);
std::string sv;
d.getVal("foo", sv);
TEST_ASSERT(sv == "[1,0,]");
TEST_ASSERT(sv == "[1,0]");
sv = d.getVal<std::string>("foo");
TEST_ASSERT(sv == "[1,0,]");
TEST_ASSERT(sv == "[1,0]");
}
{
Dict d;
@@ -446,9 +446,9 @@ void testVectToString() {
d.setVal("foo", v);
std::string sv;
d.getVal("foo", sv);
TEST_ASSERT(sv == "[1.2,0,]");
TEST_ASSERT(sv == "[1.2,0]");
sv = d.getVal<std::string>("foo");
TEST_ASSERT(sv == "[1.2,0,]");
TEST_ASSERT(sv == "[1.2,0]");
}
{
Dict d;
@@ -458,9 +458,9 @@ void testVectToString() {
d.setVal("foo", v);
std::string sv;
d.getVal("foo", sv);
TEST_ASSERT(sv == "[10001,0,]");
TEST_ASSERT(sv == "[10001,0]");
sv = d.getVal<std::string>("foo");
TEST_ASSERT(sv == "[10001,0,]");
TEST_ASSERT(sv == "[10001,0]");
}
BOOST_LOG(rdErrorLog) << "\tdone" << std::endl;