In Groovy it could be:
import groovy.json.JsonOutput
def str = """name : Cherry.API.DEV
version2 : Test
uuid : 0492389a-43243-ewr234342-343d
xx : {@{fdsfsdfsf}
lastSession : 0.0
activeOK : True
learn : ok
"""
def map = [:]
str.splitEachLine(~':', { List<String> list ->
map.put(list[0].trim(), list[1].trim())
})
def json = JsonOutput.toJson(map)
println json
assert json == '{"name":"Cherry.API.DEV","version2":"Test","uuid":"0492389a-43243-ewr234342-343d","xx":"{@{fdsfsdfsf}","lastSession":"0.0","activeOK":"True","learn":"ok"}'
It prints:
{"name":"Cherry.API.DEV","version2":"Test","uuid":"0492389a-43243-ewr234342-343d","xx":"{@{fdsfsdfsf}","lastSession":"0.0","activeOK":"True","learn":"ok"}
The solution in Java with jackson:
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/test.txt")));
String line;
Map<String, String> map = new LinkedHashMap<>();
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(":");
if (tokens != null && tokens.length > 1) {
String key = tokens[0];
String value = tokens[1];
if (key != null && value != null) {
map.put(key.trim(), value.trim());
}
}
}
String json = new ObjectMapper().writeValueAsString(map);
System.out.println(json);