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,