Java tutorial
/******************************************************************************* * Copyright (c) 2010-2014, Gabor Szarnyas, Istvan Rath and Daniel Varro * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Gabor Szarnyas - initial API and implementation *******************************************************************************/ package hu.bme.mit.incqueryd.engine.util; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.commons.io.FileUtils; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.emfjson.EMFJs; import org.emfjson.jackson.resource.JsonResourceFactory; public class EObjectSerializer { public static void serializeToFile(final EObject eObject, final String modelFile) throws IOException { final ResourceSet resourceSet = new ResourceSetImpl(); Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("json", new JsonResourceFactory()); final Resource resource = resourceSet.createResource(URI.createURI(modelFile)); final Map<String, Object> options = new HashMap<String, Object>(); options.put(EMFJs.OPTION_INDENT_OUTPUT, true); options.put(EMFJs.OPTION_SERIALIZE_TYPE, true); resource.getContents().add(eObject); resource.save(options); } public static String serializeToString(final EObject eObject) throws IOException { Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("json", new JsonResourceFactory()); final File tempFile = createTempFile(); serializeToFile(eObject, tempFile.getAbsolutePath()); final String modelString = FileUtils.readFileToString(tempFile); return modelString; } public static File createTempFile() throws IOException { final File tempFile = File.createTempFile("temp-", ".json"); tempFile.deleteOnExit(); return tempFile; } }