[LLVMdev] YAML IO problems

Robert Ankeney rrankene at gmail.com
Sun Mar 10 21:42:39 PDT 2013


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 <iostream>
#include <fstream>
#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<FooBar> FooBarSequence;

namespace llvm {
namespace yaml {
  template <>
  struct MappingTraits<FooBar> {
    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<FooBar> {
    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<char>(jsonFile)),
                       istreambuf_iterator<char>());

    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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130310/7b5cd4ee/attachment.html>


More information about the llvm-dev mailing list