user575883
user575883

Reputation: 91

How to emit a string without quotes using yaml-cpp?

I know that by default yamp-cpp emits strings without quotes but, if there are brackets in the string it always emit it with double quotes.

YAML::Emitter out;
// ...
const std::string myStr = "[0, 0, 1]"
out << myStr;

In the above example I get in the file: "[0, 0, 1]" when I want [0, 0, 1]

Do you know how to solve that?

Thanks!

Upvotes: 1

Views: 939

Answers (2)

formiaczek
formiaczek

Reputation: 425

You could try this:

YAML::Emitter out;
// ...
const std::string myStr = "[0, 0, 1]"
out << YAML::Load(myStr);

or this:

out.WriteStreamable(myStr);

Upvotes: 0

Jesse Beder
Jesse Beder

Reputation: 34054

The reason yaml-cpp is quoting your string is that if it didn't, you'd be emitting a sequence, not a scalar. If you want to emit the sequence [0, 0, 1], then you can do so:

out << YAML::BeginSeq << 0 << 0 << 1 << YAML::EndSeq;

But you simply can't emit the text [0, 0, 1] as a plain scalar, since it's not!

Upvotes: 2

Related Questions