Vasantha Raj
Vasantha Raj

Reputation: 647

unable to convert yaml string to map with key "NO" in java

While I am trying to convert yaml string to Map I am getting key change.
YAML File:-

---
HK: 
  isp: 
    Airtel: AirtelChennal
  www.enemalta.com: 
    default: defaultEma
    user1: chennal1
  studiodefault: hkDefaultchennal
  country: 
    DK: denmarkChennal
    NO: chennal2

Java code:-

Yaml yaml= new Yaml();
Map<String,Object> map= (Map<String, Object>) yaml.load(yamlString);

output :-

{HK={isp={Airtel=AirtelChennal}, www.enemalta.com={default=defaultEma, user1=chennal1}, studiodefault=hkDefaultchennal, country={DK=denmarkChennal, false=chennal2}}}

in above output false is replace with "NO", but I need "NO" as it is.

Expected output:-

{HK={isp={Airtel=AirtelChennal}, www.enemalta.com={default=defaultEma, user1=chennal1}, studiodefault=hkDefaultchennal, country={DK=denmarkChennal, NO=chennal2}}}

Upvotes: 10

Views: 1194

Answers (2)

Anthon
Anthon

Reputation: 76632

You should update your parser to one that conforms to the latest standard (1.2 from 2009!), it seems to be conforming to the 1.1 YAML standard. And interpret Yes and No as booleans. In revision 1.2 booleans should be true or false.

You can get the same effect by making the NO scalar an explicit strings by quoting them with single or double quotes.

Upvotes: 0

JP Moresmau
JP Moresmau

Reputation: 7403

According to this article: http://makandracards.com/makandra/24809-yaml-keys-like-yes-or-no-evaluate-to-true-and-false

In order to use the strings ‘yes’ and ‘no’ as keys, you need to wrap them with quotes

Upvotes: 4

Related Questions