Closed GoogleCodeExporter closed 9 years ago
I did not quite catch what you want to achieve. Can you please attach a
complete
example ?
If you only use YAML in Java then you probably never need to define local tags
(!
define). Global tags with class name will do the job.
You can find some explanations here:
http://code.google.com/p/snakeyaml/wiki/
Documentation#Constructors,_representers,_resolvers
Original comment by py4fun@gmail.com
on 19 Sep 2009 at 8:09
What I was trying to do was add a user-friendly alias to the Class names that
get
printed. Is "representer.addClassTag(xx)" not the right way to do it?
Original comment by ashwin.j...@gmail.com
on 20 Sep 2009 at 7:14
Yes, representer.addClassTag(xx) is one of the possible ways.
As far as I understand the problem is not during dumping but during parsing.
Do you mean that if you set something for representer it should symmetrically
work
for constructor ?
Original comment by py4fun@gmail.com
on 20 Sep 2009 at 10:27
Yes, you don't think so? I was under the impression that Representer would work
both
ways. It would be really useful for self-descriptive documents.
Original comment by ashwin.j...@gmail.com
on 21 Sep 2009 at 5:32
No I do not. YAML -> Java and Java -> YAML transformations are very asymmetric.
The
SnakeYAML interface clearly separates them.
What is a self-descriptive document ?
Original comment by aso...@gmail.com
on 21 Sep 2009 at 10:28
By self-descriptive, I meant that .yaml file would double as a
serialization/de-ser
format and also as an easy to read, poor man's document. Isn't that the appeal
of
YAML over XML and JSON?
That way, a simple format with aliases like - !<alias>, would serve well for
load and
parse.
Just my 2¢.
Original comment by ashwin.j...@gmail.com
on 22 Sep 2009 at 5:26
Tags are fully implemented for both dumping and parsing.
Is this what you are looking for:
http://code.google.com/p/snakeyaml/source/browse/src/test/java/org/yaml/snakeyam
l/Example2_24Test.java
For this YAML:
%TAG ! tag:clarkevans.com,2002:
--- !shape
# Use the ! handle for presenting
# tag:clarkevans.com,2002:circle
- !circle
center: &ORIGIN {x: 73, y: 129}
radius: 7
- !line
start: *ORIGIN
finish: { x: 89, y: 102 }
- !label
start: *ORIGIN
color: 0xFFEEBB
text: Pretty vector drawing.
Original comment by aso...@gmail.com
on 22 Sep 2009 at 5:38
One more:
http://code.google.com/p/snakeyaml/wiki/Documentation#Shortcuts
Original comment by aso...@gmail.com
on 22 Sep 2009 at 5:41
Please provide an example of YAML which you try to parse. Otherwise it is
difficult to
help.
Original comment by py4fun@gmail.com
on 25 Sep 2009 at 3:22
I figured it out. The docs talk about representer.addTypeDescription(xx) in the
http://code.google.com/p/snakeyaml/wiki/Documentation#Shortcuts section and I
was
looking for that method:
Representer representer = new Representer();
representer.addTypeDescription(new TypeDescription(Car.class, "!car"));
representer.addTypeDescription(new TypeDescription(Wheel.class,
"tag:yaml.org,2002:map"));
But in the latest version it is really:
Representer representer = new Representer();
representer.addClassTag(Car.class, "car");
But the reverse/loading API looks a little different:
Constructor ctor = new Constructor();
ctor.addTypeDescription(new TypeDescription(Car.class, "car"));
Note the missing "!" in the alias field and the Representer and Constructor APIs
don't look symmetric. That's what confused me.
Now, the Write and Read works with custom tags/shortcuts for the actual Class
names.
Cool!
Original comment by ashwin.j...@gmail.com
on 1 Oct 2009 at 8:56
Thank you for the comment. I will have a look why "!" is missing.
The API for Representer and Constructor are different by design.
Original comment by aso...@gmail.com
on 1 Oct 2009 at 10:20
As a developer I may not be the best person to write the tutorial. After you
have
finished your exercise can you may be contribute an addition to the wiki page
with
some explanations?
Original comment by py4fun@gmail.com
on 6 Oct 2009 at 9:39
Thank you for pointing out a mistake in the documentation, it is fixed.
(representer.addTypeDescription() -> representer.addClassTag()):
http://code.google.com/p/snakeyaml/wiki/Documentation#Shortcuts
Please note that "!" is required. Without it the tag becomes "!<car>" instead
of
"!car".
Original comment by py4fun@gmail.com
on 12 Oct 2009 at 11:41
Original issue reported on code.google.com by
ashwin.j...@gmail.com
on 18 Sep 2009 at 10:12