I’m trying to parse a simple JSON file, and am having problems with using YAMLIO. FWIW, I see the unit test YAMLIOTest.cpp is disabled with a #if 0.
Anyway, the JSON file yaml.json contains:
{
“bool_test”: true,
“directory”: “.”,
“suffix”: “_test”,
“int_test”: 4
}
The test program when run shows:
YAML:5:15: error: invalid number
“int_test”: 4
^~ // ^~ points to the 4
bool_test: true
save_directory: .",
“suffix”: “_test”,
“int_test”: 4
}
save_suffix: _test",
“int_test”: 4
}
int_test: -627100365
Done
Note the strings are both incorrect - it can’t seem to identify the proper end of each string. And it ends up lost looking at the integer.
The test code is:
#include
#include
#include “llvm/Support/YAMLTraits.h”
using namespace std;
using llvm::yaml::Input;
using llvm::yaml::MappingTraits;
//===-------------------------
---------------------------------------------===//
// Test MappingTraits
//===----------------------------------------------------------------------===//
struct FooBar {
bool boolTest;
llvm::StringRef directory;
llvm::StringRef suffix;
int32_t intTest;
};
typedef std::vector FooBarSequence;
namespace llvm {
namespace yaml {
template <>
struct MappingTraits {
static void mapping(IO &io, FooBar& fb) {
io.mapRequired(“bool_test”, fb.boolTest);
io.mapRequired(“directory”, fb.directory);
io.mapRequired(“suffix”, fb.suffix);
io.mapRequired(“int_test”, fb.intTest);
struct FooBar {
bool boolTest;
llvm::StringRef directory;
llvm::StringRef suffix;
int32_t intTest;
};
namespace llvm {
namespace yaml {
template <>
struct MappingTraits {
static void mapping(IO &io, FooBar& fb) {
io.mapRequired(“bool_test”, fb.boolTest);
io.mapRequired(“directory”, fb.directory);
io.mapRequired(“suffix”, fb.suffix);
io.mapRequired(“int_test”, fb.intTest);
}
};
}
}
int main()
{
ifstream jsonFile(“yaml.json”);
if (jsonFile.is_open())
{
// Read file into a string
string jsonSource((istreambuf_iterator(jsonFile)),
istreambuf_iterator());
jsonFile.close();
FooBar doc;
Input yin(jsonSource.c_str());
yin >> doc;
cout << "bool_test: " << (doc.boolTest ? “true” : “false”) << endl;
cout << "save_directory: " << doc.directory.data() << endl;
cout << "save_suffix: " << doc.suffix.data() << endl;
cout << "int_test: " << doc.intTest << endl;
cout << “Done\n”;
}
}
Am I doing anything wrong here?
Thanks!