1
votes

I have the following use case. I need to transfer an XText model instance via the network in some serialized format. For this purpose I need to serialize the model on the client side, send it as the body of some kind of POST request and deserialize it on the server side.

At the time I issue the send request I do only have access to the object structure of my model. I do not have the files I created the model from any more (it would be possible, but it would destroy the cleanliness of my architecture and makes testing very complex).

I created the following code for serializing the model based on some threads here on StackOverflow and tutorials available via other Websites to serialize an XText-Model

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put("xtextbin", new XMIResourceFactoryImpl());
Injector injector = Guice.createInjector(new ScenarioRuntimeModule());
Serializer serializer = injector.getInstance(Serializer.class);
System.out.println(serializer.serialize(scenario));

However it does not work and I get the following error: com.google.inject.CreationException: Guice creation errors:

1) Error injecting constructor, org.eclipse.emf.common.util.WrappedException: org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Ungültiges Byte 1 von 1-Byte-UTF-8-Sequenz.

The problem most likely lies with the XMIResourceFactoryImpl, which I do not know how to use properly.

Another approach might be to use:

String serializedScenario = ModelUtils.serialize(scenario);

But I do not know how to deserialize the result of this serialize call.

My question however is more basic, since the code above might be a completely wrong approach. Unfortunately I did not find very much about this in the documentation or anywhere else on the Web.

TL;DR: What is the best way to serialize an XText object model and to deserialize it again?

1

1 Answers

2
votes

The first line doesn't belong here at all, just remove it.

How did you create/obtain the object 'scenario'? If it was parsed with Xtext already you can obtain the serializer like this:

((XtextResource)scenario.eResource()).getResourceServiceProvider
                                     .get(ISerializer.class)