0

Basically, I'm trying to do this:

@Test
public void testCreateFromString() {
    BasicDBObject obj = new BasicDBObject("{'username': 'xirby'}");
    assertNotNull(obj.get("username")); 
}

@Test
public void testCreateQueryFromString() {
    BasicDBObject query = new BasicDBObject("{'$inc': {number: 1}}");
    assertNotNull(query.get("$inc")); 
}

Both test fails, with error:

Unexpected character (') at position 2.
    at org.json.simple.parser.Yylex.yylex(Unknown Source)

BasicDBObject.java: (w/c is a HashMap)

public BasicDBObject(String doc){
    try {
        JSONParser parser=new JSONParser();
        Object obj = parser.parse(doc);
        if (obj != null){
            if (obj instanceof JSONObject){ 
                putAll((Map<String,Object>) obj);
            } 
        } else {
            throw new RuntimeException("Cannot parse document: " + doc);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Its most likely that the JSON String would be like this on code to eliminate the need to \ escape character:

"{'username' : 'xirby'}"

So are there any JSON parser that could parse this?

Or

Perhaps, a library that can turn this String into a valid JSON String.

quarks
  • 33,478
  • 73
  • 290
  • 513
  • 1
    I doubt you would get a JSON parser to parse that because it isn't valid JSON… – Alyssa Ross Jun 09 '13 at 11:36
  • @RossPenman Right, perhaps a library to transform the string into a valid json – quarks Jun 09 '13 at 11:40
  • http://stackoverflow.com/questions/17009265/replace-single-quote-with-double-quote-with-regex – quarks Jun 09 '13 at 11:48
  • @RossPenman actually there is one, see the link posted by xybrek right above – fge Jun 09 '13 at 12:14
  • This is basically a duplicate question asked in an alternate form. You should consider deleting it as your other question seems to have gotten all the answers you need. – Perception Jun 09 '13 at 12:21

0 Answers0