emfjson / emfjson-jackson

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

XMI:UID to JSON? #46

Closed jmarreroc closed 9 years ago

jmarreroc commented 9 years ago

Hi ghillairet,

First of all, thanks for your great work. I'm trying to persist in json format some models that i have in xmi format, so i basically put the contents of the XMI resource in your Json resource. And this is what is happening:

from my XMI fragment:

<views xmi:id="_e7ueQZdiEeS7gJfuiwqSVw"
      name="Business Function"
      composedFrom="_9svCcJdkEeSMX_UyBNZKBw //@models.1">
    <governedBy
        href="archimate.lib.ead#_5E8HAJdbEeSCOqTm_c7kWw"/>
</views>

this is the generated Json fragment:

"views" : [ {
    "name" : "Business Function",
    "governedBy" : {
      "$ref" : "platform:/plugin/mydomain.ead/examples/ead/archimate.lib.ead#_5E8HAJdbEeSCOqTm_c7kWw",
      "eClass" : "http://mydomain.com/1.0/ead#//ArchitectureViewpoint"
    },
    "composedFrom" : [ {
      "$ref" : "//@models.1",
      "eClass" : "http://mydomain.com/1.0/ead#//ArchitectureModel"
    } ]
  } ]

So, i can't access to the xmi:id="_e7ueQZdiEeS7gJfuiwqSVw" property in the json format... and when i try to get the EObject with the urifragment jsonResource.getEObject("_e7ueQZdiEeS7gJfuiwqSVw") i get null Object. Obviously when i try it with the xmiResource i can get the EObject correctly. I suspect that it happens because the property xmi:id is a particular functionallity of the xmi format wich EMF uses (in benefit) for locate and persist models. So, is there anyway to include the xmi:id property in my generated json? Am i missing something in the instantiation of the json Resource?

Thank you very much for your time and your work!

Kind regards.

ghillairet commented 9 years ago

You can serialize the UUIDs with a JsonResource like you do with a XMIResource by overriding the method useUUIDs like this

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new JsonResourceFactory() {
    @Override
    public Resource createResource(URI uri) {
        JsonResource resource = new JsonResource(uri) {
             protected boolean useUUIDs() {
                  return true;
             };
         };
         return resource;
     }
});

This will generate an _id field in the JSON output with the UUID value.

jmarreroc commented 9 years ago

Hi again ghillairet,

I tried what you said and now works perfect! I have seen that there was several changes in the project... Before all these changes i could parse a single EObject using the EObjectMapper (org.eclipselabs.emfjson.map.EObjectMapper) with this sentence EObjectMapper().to(eobject, eobject.eResource(), output_options). Is there anyway to get now the jackson ObjectNode from a single EObject?

Thank you and kind regards!

ghillairet commented 9 years ago

You would do it with the Jackson API

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new EMFModule(options));

JsonNode node = mapper.valueToTree(resourceOrObject);
jmarreroc commented 9 years ago

Oh! I see! Thank you again!