/** {@inheritDoc} */ @Override public String toStore(Feature bean) { if (bean == null) return null; return bean.toJson(); }
/** {@inheritDoc} */ @Override public String toString() { return toJson(); }
@Override public JsonElement serialize(Feature feature, Type srcType, JsonSerializationContext context) { return gson.fromJson(feature.toJson(), JsonElement.class); }
/** * Convert feature array to json. * * @param features * target features * @return json string */ public static String featureArrayToJson(Feature[] features) { StringBuilder sb = new StringBuilder(); sb.append("["); if (features != null) { boolean first = true; for (Feature feature : features) { sb.append(first ? "" : ","); sb.append(feature.toJson()); first = false; } } sb.append("]"); return sb.toString(); }
/** {@inheritDoc} */ @Override public JsonDocument toStore(Feature feature) { if (feature == null) return null; JsonObject jsonObject; try { jsonObject = TRANSCODER.stringToJsonObject(feature.toJson()); jsonObject.put("_class", Feature.class.getCanonicalName()); } catch (Exception e) { throw new FeatureAccessException("Cannot parse the feature", e); } return JsonDocument.create(feature.getUid(), jsonObject); }
Feature f = getFf4j().getFeatureStore().read(uid); res.setContentType(CONTENT_TYPE_JSON); res.getWriter().println(f.toJson());
/** {@inheritDoc} */ @Override public void putFeature(Feature fp) { Util.assertNotNull(fp); Jedis jedis = null; try { jedis = getJedis(); jedis.set(KEY_FEATURE + fp.getUid(), fp.toJson()); jedis.expire(KEY_FEATURE + fp.getUid(), getTimeToLive()); } finally { if (jedis != null) { jedis.close(); } } }
/** {@inheritDoc} */ @Override public void update(Feature fp) { Util.assertNotNull("Feature" , fp); if (!exist(fp.getUid())) { throw new FeatureNotFoundException(fp.getUid()); } Jedis jedis = null; try { jedis = getJedis(); jedis.set(KEY_FEATURE + fp.getUid(), fp.toJson()); jedis.persist(KEY_FEATURE + fp.getUid()); } finally { if (jedis != null) { jedis.close(); } } }
/** {@inheritDoc} */ @Override public void create(Feature fp) { Util.assertNotNull("Feature", fp); if (exist(fp.getUid())) { throw new FeatureAlreadyExistException(fp.getUid()); } Jedis jedis = null; try { String id = fp.getUid(); jedis = getJedis(); // Store the feature in the mapping bucket. jedis.sadd(KEY_FEATURE_MAP, id); jedis.set(KEY_FEATURE + id, fp.toJson()); jedis.persist(KEY_FEATURE + id); } finally { if (jedis != null) { jedis.close(); } } }