How do I deal with # characters in data?

80 views
Skip to first unread message

bmeh...@gmail.com

unread,
Apr 14, 2013, 6:31:33 AM4/14/13
to googl...@googlegroups.com
Hello all,

While using Gson for my project I ran into an issue with data containing '#' characters. The data I am reading and writing contains the names of IRC channels, which must always be prefixed by '#'.
Below is a sample of the data I work with.

{
  "timers": {
    "TestTimer": {
      "interval": 2,
      "remainingTime": 2,
      "repeat": true,
      "event": "testevent",
      "eventArguments": [
        "#bottest"
      ],
      "identifier": "TestTimer",
      "isRunning": false
    }
  }
}

Writing the data works just fine. Reading the data causes an JsonSyntaxException.
Digging through the code, I found the JsonReader.nextNonWhitespace() method. Comments in this method quite literally state that the # character is treated as an end-of-line comment, which would indeed cause all sorts of funny things to happen while reading my data.
Curiously, the comments go on to say that this behaviour is not part of the Json specs. If this is true, I must admit I would have expected an option to toggle this behaviour, but there appears to be none.

My question is now, what is the best way to deal with the data that I have? Am I overlooking a setting? Is there a way to escape the # characters?

Thanks in advance for any advice and/or insight you can give me.

Sincerely,
Bart

Joel Leitch

unread,
Apr 14, 2013, 5:47:12 PM4/14/13
to googl...@googlegroups.com
Hi Bart,
I checked in a unit test (https://code.google.com/p/google-gson/source/detail?r=1244) to verify the # and // comments are working as expected when inside of a String literal. This special comments parsing should only affected unquoted strings in some JSON structure.

Just curious, what version of Gson are you using?

Regards,
Joel

Brandon Mintern

unread,
Apr 15, 2013, 5:14:16 PM4/15/13
to google-gson
Agreed. This simple test works fine:

import com.google.gson.Gson;

public class Hash {
    public static void main(String[] args) {
        System.out.println(new Gson().fromJson("\"#test\"", String.class));
    }
}


Even the provided sample data works fine for me:

$ cat HashSample.java
import com.google.gson.Gson;

public class HashSample {
    Timers timers;
    static class Timers {
        Timer TestTimer;
    }
    static class Timer {
        int interval;
        int remainingTime;
        boolean repeat;
        String event;
        String[] eventArguments;
        String identifier;
        boolean isRunning;
    }
    public static void main(String[] args) {
        Gson gson = new Gson();
        HashSample test = gson.fromJson(args[0], HashSample.class);
        System.out.println(gson.toJson(test));
    }
}

$ javac -cp gson-2.2.2.jar HashSample.java
$ java -cp .:gson-2.2.2.jar HashSample '{

  "timers": {
    "TestTimer": {
      "interval": 2,
      "remainingTime": 2,
      "repeat": true,
      "event": "testevent",
      "eventArguments": [
        "#bottest"
      ],
      "identifier": "TestTimer",
      "isRunning": false
    }
  }
}'
{"timers":{"TestTimer":{"interval":2,"remainingTime":2,"repeat":true,"event":"testevent","eventArguments":["#bottest"],"identifier":"TestTimer","isRunning":false}}}


--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-gson...@googlegroups.com.
To post to this group, send email to googl...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-gson?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

bmeh...@gmail.com

unread,
Apr 17, 2013, 5:17:01 PM4/17/13
to googl...@googlegroups.com
Hi all,

thanks for the prompt replies. I managed to figure out the problem - it was my own mistake, of course.

In brief, I was loading the test data I showed as a Map<String, Object>, then getting the 'timers' map from the map, then trying to load the TestTimer object by parsing the result of toString() as Json... Which results in an unquoted string, which results in a nice little error message.
The bits and pieces were scattered across a class, so I didn't immediately figure it out.

Thanks for showing me that I really should have reduced the issue to its basic components before coming to conclusions. :)

For your enjoyment, below is the distilled version of my scenario, with both the error situation and my solution. The fix is probably not the best available, but it seems to work so far. I'm open to suggestions!

import com.google.gson.reflect.TypeToken;

public class HashTest
{
public static void main(String[] args)
{
String rawData = "{  \"timers\": {    \"TestTimer\": {      \"interval\": 2,      \"remainingTime\": 2,      \"repeat\": true,      \"event\": \"testevent\",      \"eventArguments\": [        \"#tfbottest\"      ],      \"identifier\": \"TestTimer\",      \"isRunning\": false    }  }}";
Type type = new TypeToken<Map<String, Object>>()
{
}.getType();
Gson gson = new Gson();
Map<String, Object> node = gson.fromJson(rawData, type);
Map<String, Object> timers = (Map<String, Object>) node.get("timers");
Object testTimerObject = timers.get("TestTimer");

boolean fail = true;
String data;
if (fail)
{
data = testTimerObject.toString();
}
else
{
data = gson.toJson(testTimerObject);
}
System.out.println(data);

EventTimer testTimer = gson.fromJson(data, EventTimer.class);
System.out.println(testTimer);
}
}

Thanks for the help.

Sincerely,
Bart

janwen lou

unread,
Aug 15, 2013, 2:58:55 AM8/15/13
to googl...@googlegroups.com, bmeh...@gmail.com
thanks,i just encounter the same issue,
Reply all
Reply to author
Forward
0 new messages