emfjson / emfjson-jackson

JSON Binding for Eclipse Modeling Framework
https://emfjson.github.io
Other
80 stars 23 forks source link

Enhancement: Specify the json property name not only by using @JsonProperty but also when we have ExtendedMetaData #95

Closed kchobantonov closed 6 years ago

kchobantonov commented 7 years ago

Currently if someone wants to change the XML output for a particular document one of the ways is to specify

xmlSaveOptions.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE)

where xmlSaveOptions is the hashmap passed to the resource save method and to have the following annotation for the specified EStructuralFeature

`<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" unique="false" lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">

</eStructuralFeatures>`

Here the name details would be used only since the kind does not make sense in JSON - this way if we have such metadata and we have specified the OPTION_EXTENDED_META_DATA (and possibly not having the JsonProperty annotation) then the json property won't be name but will be myCustomName - This will help when we have framework that needs to support both JSON and XML format and have only one configuration how the names should be mapped.

ghillairet commented 7 years ago

That's something that could be consider. Would there be any other XML metadata that could be use in JSON as well?

kchobantonov commented 7 years ago

There are one other option to influence what is the name of the EStructuralFeature besides passing the XMLResource.OPTION_EXTENDED_META_DATA with Boolean.TRUE and having the extended metadata on particular EAttribute/EReference - you can pass XMLResource.OPTION_XML_MAP to the save with object value from XMLResource.XMLMap type - for example here is a sample code that will change the getResource_Content EReference feature of some custom RestPackage EPackage

                    XMLResource.XMLInfo info = new XMLInfoImpl();

info.setXMLRepresentation(XMLResource.XMLInfo.ELEMENT); info.setName("content"); XMLResource.XMLMap xmlMap = new XMLMapImpl(); xmlMap.add(RestPackage.eINSTANCE.getResource_Content(), info);

xmlOptions.put(XMLResource.OPTION_XML_MAP, xmlMap);

where xmlOptions is a hashmap that is passed to the save method - here the EReference feature RestPackage.eINSTANCE.getResource_Content() is going to be saved as XML element and its name is going to be content - (provided by info.setName("content"))

On Sat, Dec 10, 2016 at 12:29 PM, Guillaume Hillairet < notifications@github.com> wrote:

That's something that could be consider. Would there be any other XML metadata that could be use in JSON as well?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/emfjson/emfjson-jackson/issues/95#issuecomment-266224554, or mute the thread https://github.com/notifications/unsubscribe-auth/AMLbP6YhZEmg0roiPKdQCDFKFNnaWRlQks5rGuF8gaJpZM4LDJS0 .

kchobantonov commented 7 years ago

P.S.

Other XML metadata information could be used if we want to generate for example json schema - for example if we have EAttribute with custom EData type - ECustomString - for example and we have annotation on the ECustomString EDataType with source http:///org/eclipse/emf/ecore/util/ExtendedMetaData and then in the details we have a key named "pattern" then the value will be a regex pattern that this custom string needs to pass this regex in order to be valid

This information is not used during the deserialization so there is no need to alter the json library - this information is used by the EMF validator framework - but if for example you are planning to have maybe separate project that depend on the json project to provide json schema generation for EMF then this could be useful.

Also note that about the OPTION_EXTENDED_META_DATA (when I provide Boolean.TRUE in previous examples) - in general the other valid value besides Boolean is object of type org.eclipse.emf.ecore.util.ExtendedMetaData

On Mon, Dec 12, 2016 at 10:29 AM, Krasimir Chobantonov < kchobantonov@gmail.com> wrote:

There are one other option to influence what is the name of the EStructuralFeature besides passing the XMLResource.OPTION_EXTENDED_META_DATA with Boolean.TRUE and having the extended metadata on particular EAttribute/EReference - you can pass XMLResource.OPTION_XML_MAP to the save with object value from XMLResource.XMLMap type - for example here is a sample code that will change the getResource_Content EReference feature of some custom RestPackage EPackage

                    XMLResource.XMLInfo info = new XMLInfoImpl();

info.setXMLRepresentation(XMLResource.XMLInfo.ELEMENT); info.setName("content"); XMLResource.XMLMap xmlMap = new XMLMapImpl(); xmlMap.add(RestPackage.eINSTANCE.getResource_Content(), info);

xmlOptions.put(XMLResource.OPTION_XML_MAP, xmlMap);

where xmlOptions is a hashmap that is passed to the save method - here the EReference feature RestPackage.eINSTANCE.getResource_Content() is going to be saved as XML element and its name is going to be content - (provided by info.setName("content"))

On Sat, Dec 10, 2016 at 12:29 PM, Guillaume Hillairet < notifications@github.com> wrote:

That's something that could be consider. Would there be any other XML metadata that could be use in JSON as well?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/emfjson/emfjson-jackson/issues/95#issuecomment-266224554, or mute the thread https://github.com/notifications/unsubscribe-auth/AMLbP6YhZEmg0roiPKdQCDFKFNnaWRlQks5rGuF8gaJpZM4LDJS0 .

ghillairet commented 6 years ago

Get property name from extended metadata annotation done in https://github.com/emfjson/emfjson-jackson/commit/ad97c2331bc2684bde9183289a2d680a13487732