Map list of strings to Enum with YamlReader?

214 views
Skip to first unread message

Matthew Cummings

unread,
Dec 8, 2016, 1:33:45 AM12/8/16
to yamlbeans-users
Hi,
I've looked at the unit test examples in the GitHub repo but can't figure out how to do this in my code, my YAML snippet looks like this:
stages:
  - EXTRACT
  - TAG
  - ZIP

I want to map that into so that stages becomes an ArrayList<stageEnum>

My enum class looks like:
enum stageEnum { EXTRACT, TAG, ZIP }

Is this possible?  

Any help is appreciated.
Thank you!

Nate

unread,
Dec 8, 2016, 12:19:40 PM12/8/16
to yamlbea...@googlegroups.com
    static public enum Moo {
        a, b, c
    }

    static public class Cow {
        public ArrayList<Moo> list;
    }

    public static void main (String[] args) throws Exception {
        Cow cow = new Cow();
        cow.list = new ArrayList();
        cow.list.add(Moo.a);
        cow.list.add(Moo.b);
        StringWriter writer = new StringWriter();
        YamlWriter out = new YamlWriter(writer);
        out.write(cow);
        out.close();
        System.out.println(writer.toString());
        YamlReader reader = new YamlReader(writer.toString());
        System.out.println(reader.read(Cow.class).list);
    }


--
--
You received this message because you are subscribed to the "yamlbeans-users" group:
http://groups.google.com/group/yamlbeans-users

---
You received this message because you are subscribed to the Google Groups "yamlbeans-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yamlbeans-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matthew Cummings

unread,
Dec 8, 2016, 5:12:57 PM12/8/16
to yamlbeans-users
Excellent, thanks.  BTW, I found a bug.  Your example does not work when there is an underscore in the package name/path.  In that case you get an exception like this one:

Exception in thread "main" com.esotericsoftware.yamlbeans.YamlException: Error tokenizing YAML.
at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:107)
at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:90)
at com.carbonblack.cbprove.tools.bobobob_bobobob.Application.main(Application.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: com.esotericsoftware.yamlbeans.tokenizer.Tokenizer$TokenizerException: Line 0, column 38: While scanning a tag, expected ' ' but found: '_' (95)
at com.esotericsoftware.yamlbeans.tokenizer.Tokenizer.scanTag(Tokenizer.java:703)
at com.esotericsoftware.yamlbeans.tokenizer.Tokenizer.fetchTag(Tokenizer.java:484)
at com.esotericsoftware.yamlbeans.tokenizer.Tokenizer.fetchMoreTokens(Tokenizer.java:304)
at com.esotericsoftware.yamlbeans.tokenizer.Tokenizer.peekNextToken(Tokenizer.java:120)
at com.esotericsoftware.yamlbeans.tokenizer.Tokenizer.peekNextTokenType(Tokenizer.java:125)
at com.esotericsoftware.yamlbeans.parser.Parser$4.produce(Parser.java:131)
at com.esotericsoftware.yamlbeans.parser.Parser.getNextEvent(Parser.java:80)
at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:98)
... 7 more
To unsubscribe from this group and stop receiving emails from it, send an email to yamlbeans-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages