Search code examples
jsonbufferedreaderjson-simple

Why doesn't the JSON-Simple parser expect a colon, in Java?


I'm parsing a JSON block from twitter using JSON-Simple. I've run into an excepton:

Exception in thread "main" Unexpected token COLON(:) at position 27
...stack trace...

I've seen something similar on SO due to some specialness in the way JavaScript parses JSON (I think that JSONJ-P needs to recieve it padded) but this isn't the case here as I'm not in JS!

Here is my JSON (sanitized):

{"in_reply_to_status_id_str":null,"id_str":"23475129874512857","text":"RT @SanitizedPersonName: SomeTextIveSanitized @SanatizedTwitterHandle SomeMoreText http:\/\/t.co\/sanitized SomeText (MoreText) \/ Text", "restOfTheJson":"Is not Special"}

Two things strike me.

  1. It prints out the line that it just read with buffered reader, and that doesn't have a { at the start of the block it does at the end. This seems like a serious issue.
  2. The character I read at position 27 is not a colon but r

So either buffered reader is eating my { or the parser is not parsing correctly. How do I tell which is it and fix the issue?

n.b. I do have my print and parse line inside a while(buff.read()!=-1) (to check for line end) and an if statement that checks for a 'regex' (if(!line.contains(DELETE_REGEX)) (to check for invalid jsons) could this be the cause? Here is the code:

buff = new BufferedReader(new FileReader(JSON_FILE_NAME+".json"));
while(buff.read() != -1){
    line = buff.readLine();
    if(!line.contains(DELETE_REGEX)){
        System.out.println(line);
        myJSON=(JSONObject) parser.parse(line);
        myJSONSample.add(myJSON);
    }
}
return myJSONSample;

I've missed out some inconsequential stuff.


Solution

  • I think I found it. buff.read() was eating the first char of each line making it impossible to parse it as a json. I'd revert to while((line=buff.readLine())!=null) to check for EOR, but that doesn't work.