Closed bonnysinha closed 9 years ago
You can try to add a field eClass for each objects, otherwise emfjson does not know what kind of objects to load.
{
"eClass": "http://com.sample.jndisample#//Server"
"IP_Addr" : "SAMPLE_VALUE",
"Port_Number" : 0
}
Hi Guillaume,
Thanks for the response. Following is the modified instance.
{ "eClass": "http://com.sample.jndisample#//Server" "IP_Addr" : "SAMPLE_VALUE", "Port_Number" : 0, "Mac_Addr" : "SAMPLE_VALUE", "Database" : [ { "eClass": "http://com.sample.jndisample#//Database" "Database_Name" : "SAMPLE_VALUE", "Username" : "SAMPLE_VALUE", "Password" : "SAMPLE_VALUE", "Schema" : { "eClass": "http://com.sample.jndisample#//Schema" "Schema_Name" : "SAMPLE_VALUE" } } ] }
Following are the observations made: a. This new json threw an exception (java.net.UnknownHostException: com.sample.jndisample). Note: I do not have the URI hosted anywhere. b. If I modify the json by removing the URI information as "eClass": "#//Server" and the same for others, I get the exception (java.io.FileNotFoundException: D:\SampleEMFProject (Access is denied)) c. Lastly I tried to modify "STEP-3" as explained in my first post as: metaResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*.json", new JsonResourceFactory()); I got the error as (Cannot create a resource for '/instance.json'; a registered resource factory is needed)
Is there something else I am missing? Kindly help.
Regards, Bonny.
Did you register the EPackage before loading the file?
Yes I registered as shown below
ResourceSet metaResourceSet = new ResourceSetImpl(); metaResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(<tried as "" and as ".json">, new JsonResourceFactory()); metaResourceSet.getPackageRegistry().put(ePack.getNsURI(), ePack);
This works for me, create a maven project, add emfjson-jackson as dependencies, put the json files in src/main/resources.
package emfj;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.emfjson.jackson.resource.JsonResourceFactory;
public class Main {
public static void main(String[] args) {
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet
.getPackageRegistry()
.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
resourceSet
.getResourceFactoryRegistry()
.getExtensionToFactoryMap()
.put("json", new JsonResourceFactory());
Resource resource = resourceSet.getResource(
URI.createFileURI("src/main/resources/meta.json"), true);
EPackage ePackage = (EPackage) resource.getContents().get(0);
resourceSet.getPackageRegistry().put(ePackage.getNsURI(), ePackage);
Resource resource2 = resourceSet.getResource(
URI.createFileURI("src/main/resources/model.json"), true);
EObject server = resource2.getContents().get(0);
System.out.println(server.eGet(server.eClass().getEStructuralFeature("IP_Addr")));
System.out.println(server.eGet(server.eClass().getEStructuralFeature("Port_Number")));
List<EObject> databases = (List<EObject>) server.eGet(server.eClass().getEStructuralFeature("Database"));
EObject database = databases.get(0);
System.out.println(database.eGet(database.eClass().getEStructuralFeature("Database_Name")));
}
}
Hi Guillaume,
I changed my code to the following as I had to get the ecore object from code and not file.
And followed the rest of the code as shown above. It worked like a charm. Thanks a lot.
Cheers.
Hi,
I have an ecore model defined. Later when I try to load an instance from a json file, it fails to do so. Kindly have a look at the model definition and the instance.
Model
Instance
Kindly find the steps used to load the instance
Hence I cannot use it further.
The same works perfectly in the case of XML(using XMLResourceset and all).
I just started working with emf and am new to it. Kindly help.