Code example for Gson

Methods: toJson

    protected void saveData(T data, Object cacheKey) throws IOException,
        CacheSavingException { 
        String resultJson;
        // transform the content in json to store it in the cache 
        resultJson = gson.toJson(data);
        // finally store the json in the cache 
        if (!StringUtils.isEmpty(resultJson)) {
            FileUtils.writeStringToFile(getCacheFile(cacheKey), resultJson,
        } else { 
            throw new CacheSavingException(
                "Data was null and could not be serialized in json");