I have a static inner class that contains a single element, but
instead of having the string wrapped in quotes, I want the function
literal to be left alone.
static class tooltip
{
public String formatter = "function(){return '<b>'+ this.point.name
+'</b>: '+ this.y +' %';}";
public tooltip(){}
}
From reading the docs, it seems like I should be able to write my own
serializer for this. Here's my serialier class, but the two problems
I'm running into are 1) I cannot subclass JsonPrimitive, which seems
like my best route, and 2) if I try to subclass JsonElement, I run
into issues where the underlying gson code is expecting either a
subclass of JsonPrimitive, JsonObject, etc.
class tooltipSerializer implements JsonSerializer<tooltip>
{
@Override
public JsonElement serialize(
com.mypath.tooltip tooltip,
Type type, JsonSerializationContext context) {
//return new JsonPrimitive("foo"); // this didn't leave the string
alone
return new MyJsonElement("foo"); // fails with
java.lang.IllegalStateException: This is not a JSON Primitive.
}
}
MyJsonElement class:
package com.google.gson;
import java.io.IOException;
public class MyJsonElement extends JsonElement {
private String value;
public MyJsonElement(final String input)
{
this.value = input;
}
@Override
protected void toString(Appendable sb, Escaper escaper) throws
IOException {
sb.append(value);
}
}
Hopefully I'm missing something obvious here, any help is much
appreciated.