1

For a project we are working with a telnet server where we receive our data from. After a line is read from a telnet connection i get the following string:

Original string:

SVR GAME MOVE {PLAYER: "PLAYER", MOVE: "1", DETAILS: ""}

I'm trying to convert this to an output I can easily use without too many exploitability occuring. The desired format would be to easily get(PLAYER) or get. I tried using regex in combination with json see the code below:

String line = currentLine;
line = line.replaceAll("(SVR GAME MOVE )", ""); //remove SVR GAME MATCH
line = line.replaceAll("(\"|-|\\s)", "");//remove quotations and - and spaces (=\s)
line = line.replaceAll("(\\w+)", "\"$1\""); //add quotations to every word
JSONParser parser = new JSONParser();
try {
       JSONObject json = (JSONObject) parser.parse(line);
       //@todo bug when details is empty causes
       //@todo example string: SVR GAME MOVE {PLAYER: "b", MOVE: "1", DETAILS: ""}
       //@todo string that (line) that causes an error when parsing to json {"PLAYER":"b","MOVE":"1","DETAILS":}
       //@todo Unexpected token RIGHT BRACE(}), i think because "details" has no value
       System.out.println(json.get("PLAYER"));
       System.out.println(json.get("MOVE"));
       System.out.println(json.get("DETAILS"));
       int index = Integer.valueOf(json.get("MOVE").toString());
       for(GameView v : views){
          v.serverMove(index);//dummy data is index 1
       }
    } catch (ParseException e) {
        e.printStackTrace();
}

The problem is when the details are empty this would result in a Unexpected token RIGHT BRACE(}). Furthermore when players take exploitative name for example quotes in their names the code will crash easily.

What is the best way to convert the original String to an output where you can get the seperate settings easily(player,move,details)?

5
  • take the string inside the {} and split at , then you can again split at : Commented Apr 6, 2017 at 9:39
  • You should get the first { and the last } and consider that your json String. Parse it with your Json parsing library (because REGEX is not appropriate for nested properties) Commented Apr 6, 2017 at 9:42
  • I'm not convinced that using a JSON parser is the way to go here. Commented Apr 6, 2017 at 10:30
  • @MauricePerry You would recommened using regex? Commented Apr 6, 2017 at 10:42
  • @Jouke I believe I would use a regex, yes Commented Apr 6, 2017 at 10:44

2 Answers 2

2

This will parse the string and put the variables in a map:

    Map<String,String> vars = new HashMap<>();
    String input = "SVR GAME MOVE {PLAYER: \"PLAYER\", MOVE: \"1\", DETAILS: \"\"}";
    Pattern pattern = Pattern.compile("([A-Za-z]+): \"([^\"]*)\"");
    Matcher matcher = pattern.matcher(input);
    while (matcher.find()) {
        vars.put(matcher.group(1), matcher.group(2));
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Just a quick follow up for clarification: First group: ([A-Za-z]+): //Match any characert from A-Z or a-z, + means 1 or more, end with : So the first group matches every word that ends with : Second group: "([^"]*)" //Match the ", then [^"] = match a single characer " that is not in the set, * = between zero and more times, end with a " . So the second group matches everything between quotes and checks if it doesn't contain quotes itself?
Your a regex wizard;) Thank you very much.
0

With jackson library you could do something like:

String input = "{PLAYER: \"PLAYER\", MOVE: \"1\", DETAILS: \"\"}";

ObjectMapper mapper = new ObjectMapper();
JsonNode yourJson = mapper.readTree(input);
System.out.println(yourJson.get("PLAYER"));
System.out.println(yourJson.get("MOVE"));
System.out.println(yourJson.get("DETAILS"));

11 Comments

would it parse the string? it's not valid JSON.
Results in com.fasterxml.jackson.core.JsonParseException: Unexpected character ('P' (code 80)): was expecting double-quote to start field name The problem is that PLAYER,MOVE&DETAILS do not contain quotes(as in default JSON format). I tried fixing that with a regex as displayed in my original code. But i can't get it to work properly. Any idea?
@Jouke yes: the attribute names must be in quotes
@MauricePerry It will. If you look it with out escapes, it's a valid json.
@Jouke My input string has escapes just for demo purpose. Without them it becomes valid json.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.