Code example for JsonPrimitive

public class EncodedStringTypeAdapter implements JsonSerializer<String>{
    public static final Logger s_logger = Logger.getLogger(EncodedStringTypeAdapter.class.getName());
    public JsonElement serialize(String src, Type typeOfResponseObj, JsonSerializationContext ctx) {
        return new JsonPrimitive(encodeString(src));
    private static String encodeString(String value) {
        if (!ApiServer.encodeApiResponse) { 
            return value;
        try { 
            return new URLEncoder().encode(value).replaceAll("\\+", "%20");
        } catch (Exception e) {
            s_logger.warn("Unable to encode: " + value, e);
        return value;
Connect your IDE to all the code out there  Get Codota for Java