/*******************************************************************************
* Copyright (c) 2011 The University of York.
* 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:
* Louis Rose - initial API and implementation
******************************************************************************/
package simulator.persistence;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import simulator.util.EmfUtil;
public class EObjectSerializer implements Serializable {
// Generated by Eclipse
private static final long serialVersionUID = 5261606297382792696L;
public String serialize(EObject object) throws SerializationException {
try {
final ByteArrayOutputStream destination = new ByteArrayOutputStream();
EmfUtil.serialise(object, destination);
return new String(destination.toByteArray(), "UTF-8");
} catch (IOException e) {
throw new SerializationException("Error encountered whilst serializing " + object, e);
}
}
public EObject deserialize(EPackage metamodel, String serialised) throws SerializationException {
try {
final ByteArrayInputStream source = new ByteArrayInputStream(serialised.getBytes("UTF-8"));
return EmfUtil.deserialise(metamodel, source);
} catch (IOException e) {
throw new SerializationException("Error encountered whilst deserializing: " + serialised, e);
}
}
}