How to set yaml-cpp node style?

I have a class vector3.

class vector3 { float x, y, z; } node["x"] = vector3.x; node["y"] = vector3.y; node["z"] = vector3.z; 

Result

 x: 0 y: 0 z: 0 

I want the result to be:

 {x: 0, y: 0, z: 0} 

If using the old API, I can use YAML::Flow to set the style:

 YAML::Emitter emitter; out << YAML::Flow << YAML::BeginMap << YAML::Key << "x" << YAML::Value << x << YAML::EndMap 

Using the new API, how can I set the style?

I asked this question on the yaml-cpp project release page:

https://code.google.com/p/yaml-cpp/issues/detail?id=186

I got the answer:

You can still use the emitter and set the flow style:

 YAML::Emitter emitter; emitter << YAML::Flow << node; 

but vector3 is part of the object. I specialize YAML :: convert <>

 template<> struct convert<vector3> { static Node encode(const vector3 & rhs) { Node node = YAML::Load("{}"); node["x"] = rhs.x; node["y"] = rhs.y; node["z"] = rhs.z; return node; } } 

so I need to return the node, but the emitter cannot convert it to node.

I need an object for this:

 GameObject: m_Layer: 0 m_Pos: {x: 0.500000, y: 0.500000, z: 0.500000} 

How can i do this?

+3
source share
1 answer

Some time ago, the node interface was expanded in yaml-cpp to include the SetStyle() function, adding the following line anywhere in encode should have the desired result

 node.SetStyle(YAML::EmitterStyle::Flow); 
+1
source

All Articles