Generate JSON from DSL
Hi, community members!
I have read a lot of MPS docs and watched some tutorials, but I didn't find answers to few my questions:
1) How can MPS generate JSON from my DSL? I have seen some examples for XML (using jetbrains.mps.core.xml), but can't find examples for JSON.
2) How can I use my DSL as a library in, for example, 3-rd party java project? For example, I want to add DSL lib to my Java project and use DSL as an input. Then, using DLS library, get JSON as a response. Then do some actions using retrieved JSON according to business logic in runtime. Is it possible?
If you could provide some examples or links, it would help me a lot. Thanks!
Please sign in to leave a comment.
1) There's no JSON language in MPS distribution, and I'm not aware of any third-party plugin to provide one, although it doesn't look too complicated to create such a language, especially if you need mostly textgen part.
2) Could you please elaborate what do you mean by "use DSL as an input". MPS allows to create an editor for your domain-specific language, but the editor is an IDEA editor and therefore you need part of IDEA platform to run the editor.
Hi, Artem. Thanks for your reply!
>> Could you please elaborate what do you mean by "use DSL as an input".
I meant some string written using my DLS
E.g. I want to run my application like:
java -jar myapplication.jar "calculate avg speed on the segment from 50 to 200 km"
where "calculate avg speed on the segment from 50 to 200 km" -> is a string written on my DSL
and get following JSON as output:
{
"action": "calculate",
"function": "avg",
"property": "speed",
"segment" : {
"unit": "km",
"from":50,
"to": 200
}
}
Is it possible?
I'm afraid that MPS may not be able to help you much in your scenario, since your DSL is purely textual (string on a command line), which needs to be parsed first. MPS does not provide any assistance for parsing, first, and second, MPS feels a bit too heavyweight for this purpose, in my opinion.
The domain for MPS lies in syntactically rich, non-parseable or combined language solutions.
Vaclav
I must admit I have been fooled too in seeing in MPS a translation tool.
Defining the languages in MPS is fine, but the aim is to use them in the editor.
Unless I am mistaken, the point is not to take a text as input and output it's translation in other languages and formats. (and by the way I wonder what other tool would be right for this job)
Bruno
Vaclav, Bruno, thanks for your replies! MPS is a convenient tool and it would be great to have such ability in it :)
>> and by the way I wonder what other tool would be right for this job
If you are using Java, you can try javaCC.
This might be related and interesting for you.
https://mps-support.jetbrains.com/hc/en-us/community/posts/203381904-Parsing-JSON
Here is yet another JOSN language implementation ;)
https://github.com/nkoester/mps-json