preserve order of tags

38 views
Skip to first unread message

Сергей Каримов

unread,
Mar 1, 2019, 3:44:40 PM3/1/19
to yamlbeans-users
Hello All!
I'm trying to edit yaml file with this library and problem is that order of elements is not preserved. 
The file is very big and I'm trying to trace changes made via compare tool. when 2 files are out of order it's impossible to compare them.
I suppose it's due to internal code using HashMap instead of LinkedHashMap or similar:
YamlReader.readValueInternal(...) {
...
  case MAPPING_START :
    paramClass1 = HashMap.class;
    break;
...
}
Please allow us to read and write tree with the same element order.
Maybe some boolean flag in ReaderConfig which changes all HashMap to LinkedHashMap during structure initialization.

Uday Bhaskar Sarma Seetamraju

unread,
May 11, 2019, 9:39:04 PM5/11/19
to yamlbeans-users
// load file-contents into a java.util.LinkedHashMap<String, Object>
final LinkedHashMap dataObj = new com.esotericsoftware.yamlbeans.YamlReader( reader1 ).read( LinkedHashMap.class );
@SuppressWarnings("unchecked")
final LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) dataObj;
reader1.close();

The 'reader1' can be either InputStreamReader   or   FileInputStream  etc..
The above code works wonderfully robustly and consistently - in keeping the order of elements.

But..
there is a caveat - that is, a known "issue" with LinkedHashMap. (There is no easy solution)
If your YAML is like:-
   A: won  # Typo
   B: two 
   C: three
If you RESET the value of 'A' as:-
  data.put( "A", "one" );
Then.. "A" becomes the last element !
Reply all
Reply to author
Forward
0 new messages