Gson togheter with Google Chart

588 views
Skip to first unread message

Bassa Safa

unread,
Mar 13, 2012, 2:28:03 PM3/13/12
to google-gson
Hi,

To begin with, Im new to Gson and I normally dont develop front end
technologies... so If my question is stupid... Im sorry =)

I'm trying to use Gson to initialize Google Chart with Json-data.
However there is a problem with the date-type. Google Chart requires
that Json-data should be in the following form:

... {c:[{v: 'Bob'}, {v: new Date(2007, 5, 1)}]},...


Notice how they requires one to create new Javascript Dates AND that
its not embraced incitation. This is what I would like Gson to do
whenever stumbling across a java.util.Date. With the default Gson, a
java.util.Date is serialized to a String representation:

.... {"c":[{"v":"jan 2, 2003"},{"v":"36.51"}]},....

And I tried to definie my own Serializer according to the Gson User
Guide:
...
calender.setTime(date);
return new JsonPrimitive("new Date("+ calender.get(Calendar.YEAR) + ",
" + (calender.get(Calendar.MONTH) + 1) + ", " +
calender.get(Calendar.DAY_OF_MONTH) + ")");

But this also led to a string representation. Is what I want possible
to do in Gson? Is it possible to Serialize a java.util.Date to a non-
string representation of 'new Date(2007, 5, 1)'?

best regards
bassa

Pawel

unread,
Mar 13, 2012, 4:18:27 PM3/13/12
to google-gson

Hi.

I believe we've seen somebody trying something similar a while back.
The problem here, is that what you want to get is not a valid JSON at
all. It's valid JavaScript, but not valid JSON. In general, if it's
not valid JSON, Gson shouldn't be used.

I think there may be a very hackish way to get what you want, though.

1) Create your own class, extending java.lang.Number (let's say
FakeNumber). Doesn't matter what you return from the xxValue()
methods, you can as well return just 0. The toString() method should
return the string you intended to get in the output
2) In your custom serializer, say:
return new JsonPrimitive(new FakeNumber("new Date...."));
3) This should work.

Gson manipulates JSON types only. When string primitives are
serialized, they are always wrapped in double quotes. Objects and
arrays are serialized per element, boolean primitives are converted to
Boolean values, and nulls are just nulls. But the way Gson uses
Number, is that it just cares that the whatever native type extends
Number, and at serialization time calls object.toString() on the value
of the native type.

I checked JsonPrimitive(Number), that calls JsonPrimitive.setValue()
which is satisfied with (primitive instanceof Number). This eventually
ends up in Streams.write(JsonElement, boolean, JsonWriter), which
calls primitive.isNumber(), and if that's true, calls
JsonWriter.value(Number). JsonPrimitive.isNumber() returns true if
underlying object is instance of Number, and JsonWriter.value(Number)
just calls Number.toString() on the underlying object.

Hope this helps.


On Mar 13, 11:28 am, Bassa Safa <bassam.sa...@gmail.com> wrote:
> Hi,

Bassa Safa

unread,
Mar 14, 2012, 6:32:32 AM3/14/12
to google-gson
Yaaay for Pawel! =)

Problem solved....Works perfectly... teardropping beautiful! =) Added
full code below for users with similar problem trying to google up a
solution =)
Thank you for your time and help!

best regards
bassa =)

public class MyFakeDateGsonSerializer implements
JsonSerializer<FakeDate> {

@Override
public JsonElement serialize(FakeDate src, Type typeOfSrc,
JsonSerializationContext context) {
return new JsonPrimitive(src);
}
}

public class FakeDate extends Number {

private static final long serialVersionUID = 1L;

private Date date;

public FakeDate(Date date) {
this.date = date;
}

@Override
public String toString() {
Calendar calender = Calendar.getInstance();
calender.setTime(date);
//av okänd anledning börjar januari på 0.
return new String("new Date("+ calender.get(Calendar.YEAR) + ", " +
(calender.get(Calendar.MONTH) + 1) + ", " +
calender.get(Calendar.DAY_OF_MONTH) + ")");
}

@Override
public double doubleValue() {
return 0;
}

@Override
public float floatValue() {
return 0;
}

@Override
public int intValue() {
return 0;
}

@Override
public long longValue() {
return 0;
Reply all
Reply to author
Forward
0 new messages