marcusaram / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
1 stars 0 forks source link

Not enough examples for Loading #22

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What is the reverse of the Dumper + Representer feature?

When you tag a class using,
        Representer representer = new Representer();
        representer.addClassTag(Definition.class, "define");

What do we use for Loading the same? The parser complains of unrecognized
"!<define>" characters.

Nice work on SnakeYaml, but some better docs on Loading would be appreciated.

Thanks!
Ashwin.

Original issue reported on code.google.com by ashwin.j...@gmail.com on 18 Sep 2009 at 10:12

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
One more:

http://code.google.com/p/snakeyaml/wiki/Documentation#Shortcuts

Original comment by aso...@gmail.com on 22 Sep 2009 at 5:41

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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