Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/DesignDocs/406697"
(→API) |
|||
Line 6: | Line 6: | ||
*Related Enhancement Request - [https://bugs.eclipse.org/bugs/show_bug.cgi?id=406697 bug 406697]<br> | *Related Enhancement Request - [https://bugs.eclipse.org/bugs/show_bug.cgi?id=406697 bug 406697]<br> | ||
*Required for JSON schema generation - [http://wiki.eclipse.org/EclipseLink/Development/404452#Phase_1:_Additional_Mapping_Support JSON Schema Generation]<br> | *Required for JSON schema generation - [http://wiki.eclipse.org/EclipseLink/Development/404452#Phase_1:_Additional_Mapping_Support JSON Schema Generation]<br> | ||
+ | |||
+ | == Example == | ||
+ | <source lang="java"> | ||
+ | public class Root | ||
+ | public List<Thing> things; | ||
+ | |||
+ | public class Thing | ||
+ | public String name | ||
+ | public int id; | ||
+ | |||
+ | Root r = new Root(); | ||
+ | Thing thing1 = new Thing("someName",1); | ||
+ | Thing thing2 = new Thing("anotherName",2); | ||
+ | Thing thing3 = new Thing("someOtherName",3); | ||
+ | r.things.add(thing1); | ||
+ | r.things.add(thing2); | ||
+ | r.things.add(thing2); | ||
+ | |||
+ | Allow an annotation/external bindings option so that the following output can be produced | ||
+ | <root> | ||
+ | <someName> | ||
+ | <id>1</id> | ||
+ | </someName> | ||
+ | <anotherName> | ||
+ | <id>2</id> | ||
+ | </anotherName/> | ||
+ | <someOtherName> | ||
+ | <id>3</id> | ||
+ | </someOtherName> | ||
+ | <root> | ||
+ | |||
+ | {"root":{ | ||
+ | "someName":{"id":1} | ||
+ | "anotherName":{"id":2} | ||
+ | "someOtherName":{"id":3} | ||
+ | |||
+ | }} | ||
+ | </source> | ||
== API == | == API == |
Revision as of 10:05, 7 May 2013
Contents
Document History
- April 26, 2013 - Initial Draft
Requirements
- Related Enhancement Request - bug 406697
- Required for JSON schema generation - JSON Schema Generation
Example
public class Root public List<Thing> things; public class Thing public String name public int id; Root r = new Root(); Thing thing1 = new Thing("someName",1); Thing thing2 = new Thing("anotherName",2); Thing thing3 = new Thing("someOtherName",3); r.things.add(thing1); r.things.add(thing2); r.things.add(thing2); Allow an annotation/external bindings option so that the following output can be produced <root> <someName> <id>1</id> </someName> <anotherName> <id>2</id> </anotherName/> <someOtherName> <id>3</id> </someOtherName> <root> {"root":{ "someName":{"id":1} "anotherName":{"id":2} "someOtherName":{"id":3} }}
API
The following annotation will be added
org.eclipse.persistence.oxm.annotations.XmlVariableNode
@Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface XmlVariableNode { String attributeName(); Class type() default DEFAULT.class; String getMethod() default "##default"; String setMethod() default "##default"; static final class DEFAULT {} }
Example
@XmlRootElement public class Root { public String name; @XmlVariableNode(attributeName="thingName") public List<Thing> things; } public class Thing { @XmlTransient public String thingName; public String thingValue; }
Root r = new Root(); r.name = "theRootName"; r.things = new ArrayList<Thing>(); Thing thing1 = new Thing(); thing1.thingName = "thinga"; thing1.thingValue = "thingavalue"; Thing thing2 = new Thing(); thing2.thingName = "thingb"; thing2.thingValue = "thingbvalue"; Thing thing3 = new Thing(); thing3.thingName = "thingc"; thing3.thingValue = "thingcvalue"; r.things.add(thing1); r.things.add(thing2); r.things.add(thing3);
marshaller.marshal(r, System.out) XML <root> <name>theRootName</name> <thinga> <thingValue>thingavalue</thingValue> </thinga> <thingb> <thingValue>thingbvalue</thingValue> </thingb> <thingc> <thingValue>thingcvalue</thingValue> </thingc> </root>
marshaller.marshal(r, System.out) JSON {"root":{ "name":"theRootName" "thinga":{ "thingValue":"thingavalue"}, "thingb":{ "thingValue":"thingbvalue"}, "thingc":{ "thingValue":"thingcvalue"} }}
Design
A new mapping will be created XMLVariableXPathMapping and it will extend the AnyCollectionMapping
This mapping will assume that the key names are unique so will never write ['s in JSON
This annotation only makes sense when the referenced type is a complex thing. An exception will be thrown if this annotation is used on a simple object or List of simple things.
Limitations
Since this makes use of the any logic during unmarshal and MOXy only handles one Any mapping on a class if a class makes use of the XmlVariableNode annotation then that class can not have XmlAnyElement annotations or any other variables that would cause AnyObject or AnyCollection mappings to be created.
Note/Issues
- inheritance
- List<Object>
- single case ie:not on a collection XMLVariableXPathObjectMapping XMLVariableXPathCollectionMapping
- external bindings
- don`t write out that property itself as nested if it is the variable field - currently need to mark that property as transient.
- startcollection and endcollection [ ]'s in JSON
- could specify get/set method instead of attribute
- namespaces