ldh0826 / snakeyaml

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

Arrays of primitives are not fully supported #135

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I tried to use the library to dump arbitrary object graphs for logging 
purposes, but since there are primitive arrays in them, I cannot get it to work.

What steps will reproduce the problem?
1. Call dump() for a bean that has long[] fields.

What is the expected output? What do you see instead?
org.yaml.snakeyaml.error.YAMLException: Arrays of primitives are not fully 
supported.

What version of SnakeYAML are you using? On what Java version?
1.9 on JDK 1.6.0_24

Please provide any additional information below. (Often a failing test is
the best way to describe the problem.)

org.yaml.snakeyaml.error.YAMLException: Arrays of primitives are not fully 
supported.
    at org.yaml.snakeyaml.representer.BaseRepresenter.representData(BaseRepresenter.java:102)
    at org.yaml.snakeyaml.representer.Representer.representJavaBeanProperty(Representer.java:126)
    at org.yaml.snakeyaml.representer.Representer.representJavaBean(Representer.java:84)
    at org.yaml.snakeyaml.representer.Representer$RepresentJavaBean.representData(Representer.java:50)
    at org.yaml.snakeyaml.representer.BaseRepresenter.representData(BaseRepresenter.java:110)
    at org.yaml.snakeyaml.representer.BaseRepresenter.representSequence(BaseRepresenter.java:139)
    at org.yaml.snakeyaml.representer.SafeRepresenter$RepresentArray.representData(SafeRepresenter.java:194)
    at org.yaml.snakeyaml.representer.BaseRepresenter.representData(BaseRepresenter.java:96)
    at org.yaml.snakeyaml.representer.BaseRepresenter.represent(BaseRepresenter.java:66)
    at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:270)
    at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:261)
    at org.yaml.snakeyaml.Yaml.dump(Yaml.java:248)

Original issue reported on code.google.com by bors...@gmail.com on 27 Oct 2011 at 2:46

GoogleCodeExporter commented 8 years ago
Yes, this is not (yet?) implemented. Feel free to try to add the support.

1. Remove in the BaseRepresenter the check for arrays of primitives (line 
100-103)
2. Run the tests to see what fails
3. Adapt SeqTagTest (and other tests) to work as you expect
4. Apply the new functionality in SnakeYAML to make it work

Some info:
a) the check in the BaseRepresenter is the last check before getting the 
Representer for JavaBeans. There should be another way to detect the array and 
the type of members (Strings should be dumped differently then integers)

b) SafeRepresenter shall get yet another implementation of interface Represent 
in the  'representers' instance variable. See the examples: RepresentByteArray, 
RepresentArray

c) check that the coverage is complete (mvn site)

Original comment by py4fun@gmail.com on 28 Oct 2011 at 7:55

GoogleCodeExporter commented 8 years ago
Does anyone already made this issue? It have not fixed  yet in main repository 
and I can't find in other repositories. 

Original comment by ann.r...@gmail.com on 18 Mar 2013 at 2:52

GoogleCodeExporter commented 8 years ago
I don't think so. Be the One ;)

Original comment by alexande...@gmail.com on 19 Mar 2013 at 6:30

GoogleCodeExporter commented 8 years ago
I think I've fixed this issue (patch attached).

I have added a new test case, org.yaml.snakeyaml.array.PrimitiveArrayTest, 
which tests several use-cases for primitive arrays: loading, representing, 
truncating values (e.g. float->int) and promoting values (e.g. int->double).

With some modifications to SafeConstructor, BaseRepresenter, SafeRepresenter, 
and Representer, the test passes. A new RepresentPrimitiveArray has been 
introduced, and SafeConstructor.createArray() and 
SafeConstructor.constructArrayStep2() have been patched to handle arrays of all 
primitive types.

Some test cases checked for the old "Arrays of primitives not fully supported" 
error message and were removed.

Code coverage looks good. There are a few lines which are not covered, such as 
checks for unexpected primitive types (which cannot be tested unless a new 
primitive type is introduced). These follow a pattern used elsewhere, such as 
in Constructor.wrapIfPrimitive().

Comments/criticism welcome. I'd like to apply this patch to main soon.

Original comment by JordanAn...@gmail.com on 11 Apr 2013 at 5:48

Attachments:

GoogleCodeExporter commented 8 years ago
Thank you Jordan !
I think you forgot to include the test in the patch. (You can also create a 
remote repository.)

Original comment by py4fun@gmail.com on 11 Apr 2013 at 6:37

GoogleCodeExporter commented 8 years ago
I'm pretty sure the test is included in the patch, under 
org.yaml.snakeyaml.array.PrimitiveArrayTest . Did you want it under 
issues.issue135 instead?

I also realized that multi-dimensional arrays were not supported by that last 
patch. I've added some new cases to the test for long[][], long[][][], and 
array-of-objects-which-have-arrays, and have added those changes as 1465.patch 
(attached).

Original comment by JordanAn...@gmail.com on 11 Apr 2013 at 7:33

Attachments:

GoogleCodeExporter commented 8 years ago
Weird. I see PrimitiveArrayTest in the patch, but it was not created in the 
working directory.
I think the 'array' package is better. This is sure no bug.

Original comment by py4fun@gmail.com on 11 Apr 2013 at 9:36

GoogleCodeExporter commented 8 years ago
Are there any objections to me committing that change? I'd like to commit soon, 
before I forget about this.

Original comment by JordanAn...@gmail.com on 15 Apr 2013 at 7:24

GoogleCodeExporter commented 8 years ago
No objection from me ;)

Original comment by alexande...@gmail.com on 16 Apr 2013 at 9:52

GoogleCodeExporter commented 8 years ago
Feel free to commit.

Original comment by py4fun@gmail.com on 16 Apr 2013 at 11:45

GoogleCodeExporter commented 8 years ago

Original comment by JordanAn...@gmail.com on 16 Apr 2013 at 6:38