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.<br>Anyway, the JSON file yaml.json contains:<br>{<br> "bool_test": true,<br>
"directory": ".",<br> "suffix": "_test",<br> "int_test": 4<br>}<br><br>The test program when run shows:<br><br>YAML:5:15: error: invalid number<br> "int_test": 4<br>
^~ // ^~ points to the 4<br>bool_test: true<br>save_directory: .",<br> "suffix": "_test",<br> "int_test": 4<br>}<br><br>save_suffix: _test",<br> "int_test": 4<br>
}<br><br>int_test: -627100365<br>Done<br><br>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.<br><br>The test code is:<br><br>#include <iostream><br>
#include <fstream><br>#include "llvm/Support/YAMLTraits.h"<br><br>using namespace std;<br>using llvm::yaml::Input;<br>using llvm::yaml::MappingTraits;<br><br><br>//===-------------------------<div>
---------------------------------------------===//<br>
// Test MappingTraits<br>//===----------------------------------------------------------------------===//<br><br>struct FooBar {<br> bool boolTest;<br> llvm::StringRef directory;<br> llvm::StringRef suffix;<br> int32_t intTest;<br>
};<br>typedef std::vector<FooBar> FooBarSequence;<br><br>namespace llvm {<br>namespace yaml {<br> template <><br> struct MappingTraits<FooBar> {<br> static void mapping(IO &io, FooBar& fb) {<br>
io.mapRequired("bool_test", fb.boolTest);<br> io.mapRequired("directory", fb.directory);<br> io.mapRequired("suffix", fb.suffix);<br> io.mapRequired("int_test", fb.intTest);<br>
struct FooBar {<br> bool boolTest;<br> llvm::StringRef directory;<br> llvm::StringRef suffix;<br> int32_t intTest;<br>};<br><br>namespace llvm {<br>namespace yaml {<br> template <><br> struct MappingTraits<FooBar> {<br>
static void mapping(IO &io, FooBar& fb) {<br> io.mapRequired("bool_test", fb.boolTest);<br> io.mapRequired("directory", fb.directory);<br> io.mapRequired("suffix", fb.suffix);<br>
io.mapRequired("int_test", fb.intTest);<br> }<br> };<br>}<br>}<br><br>int main()<br>{<br> ifstream jsonFile("yaml.json");<br><br> if (jsonFile.is_open())<br> {<br> // Read file into a string <br>
string jsonSource((istreambuf_iterator<char>(jsonFile)),<br> istreambuf_iterator<char>());<br><br> jsonFile.close();<br><br> FooBar doc;<br> Input yin(jsonSource.c_str());<br>
yin >> doc;<br> cout << "bool_test: " << (doc.boolTest ? "true" : "false") << endl;<br> cout << "save_directory: " << doc.directory.data() << endl;<br>
cout << "save_suffix: " << doc.suffix.data() << endl;<br> cout << "int_test: " << doc.intTest << endl;<br> cout << "Done\n";<br> }<br>
}<br><br>Am I doing anything wrong here?<br>Thanks!<br><br></div>