mrosenbladt / yaml-cpp

Automatically exported from code.google.com/p/yaml-cpp
MIT License
0 stars 0 forks source link

Support for nested named maps in emitters #204

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When I'm trying to emit nested maps, I'm getting some issues

E.g. here's some simple code:

 emitter << YAML::BeginMap;
    emitter << YAML::Key << "Name1" << YAML::Value << "Val1";        
    emitter << YAML::BeginMap;
        emitter << YAML::Key << "Name2" << YAML::Value << "Val2";
        emitter << YAML::Key << "Name3" << YAML::Value << "Val3";
    emitter << YAML::EndMap;
    emitter << YAML::Key << "Name5" << YAML::Value << "Val5";        
emitter << YAML::EndMap;

when I serialize to a file, I'm getting this:

Name1: Val1
? Name2: Val2
  Name3: Val3
: Name5
Val5

(raw data: http://pastebin.com/HGN3ZkaS)

Here's two issues:

 1. Values added after the nested map are incorrectly define
 2. Add a syntax providing names of maps being added rather than having anonymous maps:

Name1: Val1
Maps:
  Name2: Val2
  Name3: Val3
Name5: Val5

I'm on yaml-cpp-0.5.1.tar.gz (msvc 2008).

Original issue reported on code.google.com by DenisKra...@gmail.com on 17 May 2013 at 1:34

GoogleCodeExporter commented 9 years ago
In your code:

 emitter << YAML::BeginMap;
    emitter << YAML::Key << "Name1" << YAML::Value << "Val1";        
    emitter << YAML::BeginMap;   // <--- this line
        emitter << YAML::Key << "Name2" << YAML::Value << "Val2";
        emitter << YAML::Key << "Name3" << YAML::Value << "Val3";
    emitter << YAML::EndMap;
    emitter << YAML::Key << "Name5" << YAML::Value << "Val5";        
emitter << YAML::EndMap;

The indicated line is in the "key" position of a map (the first entry in a 
key/value pair). yaml-cpp doesn't actually need the YAML::Key / YAML::Value 
entries (although this example makes me think that maybe it should produce an 
error if you use them incorrectly).

In any case, this throws the key/value pairs off by one. The second key in your 
map is the nested map, and its associated value is "Name5" (as printed).  The 
third key is "Val5", and there's no associated value. Presumably, you want:

 emitter << YAML::BeginMap;
    emitter << YAML::Key << "Name1" << YAML::Value << "Val1";        
    emitter << YAML::Key << "Maps";
    emitter << YAML::Value << YAML::BeginMap;
        emitter << YAML::Key << "Name2" << YAML::Value << "Val2";
        emitter << YAML::Key << "Name3" << YAML::Value << "Val3";
    emitter << YAML::EndMap;
    emitter << YAML::Key << "Name5" << YAML::Value << "Val5";        
emitter << YAML::EndMap;

This should produce the output you list at the bottom.

Original comment by jbe...@gmail.com on 1 Jun 2013 at 8:14