11
In 1.13, Minecraft language files were switched from being a simple multi-line key=value format to JSON.
Challenge
Write a program converting from the original format returning a JSON string. Input can be taken using any standard input method, output must be json from any standard output method
The original format contains lines with key=value pairs, for example
tile.dirt.name=Dirt
advMode.nearestPlayer=Use "@p" to target nearest player
build.tooHigh=Height limit for building is %s blocks
Should be converted to one large JSON object with key=value
{
"tile.dirt.name": "Dirt",
"advMode.nearestPlayer": "Use \"@p\" to target nearest player",
"build.tooHigh": "Height limit for building is %s blocks"
}
Some details
- Any valid JSON is allowed as long as it contains only the correct key/value pairs. Trailing commas are allowed because Minecraft allows them.
- The only things that must be escaped are quotes. (No newlines, backslashes, or other json-breaking things existed in the language file prior to 1.13)
- Empty lines should be ignored
- Lines contain exactly one equals
Test Cases
Input:
tile.dirt.name=Dirt
advMode.nearestPlayer=Use "@p" to target nearest player
build.tooHigh=Height limit for building is %s blocks
Output:
{
"tile.dirt.name": "Dirt",
"advMode.nearestPlayer": "Use \"@p\" to target nearest player",
"build.tooHigh": "Height limit for building is %s blocks"
}
Input:
translation.test.none=Hello, world!
translation.test.complex=Prefix, %s%2$s again %s and %1$s lastly %s and also %1$s again!
translation.test.escape=%%s %%%s %%%%s %%%%%s
translation.test.invalid=hi %
translation.test.invalid2=hi % s
translation.test.args=%s %s
translation.test.world=world
Output:
{
"translation.test.none": "Hello, world!",
"translation.test.complex": "Prefix, %s%2$s again %s and %1$s lastly %s and also %1$s again!",
"translation.test.escape": "%%s %%%s %%%%s %%%%%s",
"translation.test.invalid": "hi %",
"translation.test.invalid2": "hi % s",
"translation.test.args": "%s %s",
"translation.test.world": "world",
}
Input:
stat.mineBlock=%1$s Mined
stat.craftItem=%1$s Crafted
stat.useItem=%1$s Used
stat.breakItem=%1$s Depleted
Output:
{
"stat.mineBlock": "%1$s Mined",
"stat.craftItem": "%1$s Crafted",
"stat.useItem": "%1$s Used",
"stat.breakItem": "%1$s Depleted"
}
1How does
tile.dirt.name
become"block.minecraft.dirt"
? – Pavel – 2018-08-09T02:34:31.343@Pavel uuh... whoops. Fixed that. That was unintentional – pfg – 2018-08-09T02:46:28.900
5Is it guaranteed that each non-empty line contains exactly 1
=
? – user202729 – 2018-08-09T05:19:19.683@user202729 yes – pfg – 2018-08-09T17:58:16.890
3I'd be willing to bet that you actually need a solution to this problem and intend to use one to convert your files. :) – mbomb007 – 2018-08-09T19:24:06.927
What if the input contains
\"
? – pppery – 2018-08-09T21:31:10.683No, Minecraft automatically does that – Redwolf Programs – 2018-08-09T22:00:24.247
I'd know, I'm an Ubernerd of the highest level. – Redwolf Programs – 2018-08-09T22:00:47.707
@ppperry it doesn't contain backslashes – pfg – 2018-08-15T22:23:26.187
Do we only need to handle English json? – l4m2 – 2018-10-26T10:15:09.867
@l4m2 english only is fine – pfg – 2018-10-26T15:02:21.463