|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Serialize DSL model from with out Xtext Web project classes [message #1800056 is a reply to message #1800053] |
Wed, 19 December 2018 11:28 |
Mykola Makhin Messages: 50 Registered: July 2018 |
Member |
|
|
Sure.
I have implemented a custom DSL.
I have some text that I parse into AST (which as I understand is an EMF/ECore model), represented by EObject
To be very specific, I use this code to parse the text in DSL into an AST, which returns me an EObject for astRoot:
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
IResourceFactory resourceFactory = injector.getInstance(IResourceFactory.class);
Provider<XtextResourceSet> xtextResourceSetProvider = injector.getInstance(new Key<Provider<XtextResourceSet>>() {});
XtextResourceSet resourceSet = xtextResourceSetProvider.get();
Resource resource = resourceFactory.createResource(URI.createURI(UUID.randomUUID() + ".mydsl"));
resourceSet.getResources().add(resource);
resource.load(new StringInputStream(body), null);
final EObject astRoot = resource.getContents().isEmpty() ? null : resource.getContents().get(0);
I knot that EObject and it's children are actually some classes that are generated by XText for my DSL and represent particular grammar rules (and implement EObject interface).
I'd like to serialize it into JSON or XML so that I could inspect the resulting AST and see if it's what I expected it to be.
I can use some generic serialization libraries like Jackson and give them the astRoot EObject:
new ObjectMapper().writeValueAsString(astRoot)
But this results in StackOverflow, because there are circular references in the AST EObjects (I'm guessing that at least reference from parent to child and from child to parent e.g. container will definitely create a loop - so for parser AST "tree" is not 100% a tree/DAC, but more of a tree representation).
Thus I'm curious if there's any way to serialize it without writing custom code that would account for specific classes/fields generated by Xtext for my specific grammar.
[Updated on: Wed, 19 December 2018 11:30] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05599 seconds