/** * Equivalent to <pre>get(property, true)</pre> * * @return the value of the specified property * @throws UnsupportedOperationException if the value is not found in the model */ public <TYPE> TYPE get(Property<TYPE> property) { return get(property, true); }
private static void checkCacheIntegrity(AbstractModel model, JSONProperty<?> property, String transitoryKey) { if (!model.hasTransitory(transitoryKey)) { return; } JSONObjectHolder<?> holder = getJSONTransitory(model, transitoryKey); if (model.containsValue(property) || model.getDefaultValues().containsKey(property.getName())) { String jsonValue = model.get(property); if (SqlUtils.equals(holder.jsonString, jsonValue)) { return; } } model.clearTransitory(transitoryKey); }
/** * Sets the given JSON-serialized property to the given value * * @return true if the value object was successfully serialized, false otherwise */ public static <T> boolean setValueAsJSON(AbstractModel model, JSONProperty<T> property, T data, Type javaType) { try { String json = null; if (data != null) { if (MAPPER == null) { throw new NullPointerException("JSONPropertySupport needs to be initialized with a " + "JSONMapper instance using setJSONMapper()"); } json = MAPPER.toJSON(data, javaType); if (model.containsNonNullValue(property) && json.equals(model.get(property))) { return false; } } model.set(property, json); putJSONTransitory(model, transitoryKeyForProperty(property), data, json); return true; } catch (Exception e) { Logger.w(TAG, "Error serializing object to JSON string: " + data, e); // TODO: Should this throw? return false; } }
/** * Deserialize a JSON string property into the specified Java type */ public static <T> T getValueFromJSON(AbstractModel model, JSONProperty<T> property, Type javaType) { String transitoryKey = transitoryKeyForProperty(property); checkCacheIntegrity(model, property, transitoryKey); if (!model.hasTransitory(transitoryKey)) { T data = null; String json = model.get(property); // Will throw if model doesn't have property if (json != null) { try { if (MAPPER == null) { throw new NullPointerException("JSONPropertySupport needs to be initialized with a " + "JSONMapper instance using setJSONMapper()"); } data = MAPPER.fromJSON(json, javaType); } catch (Exception e) { // TODO: Should this throw or at least not cache null? Logger.w(TAG, "Error deserializing JSON string: " + json, e); model.clearValue(property); } } putJSONTransitory(model, transitoryKey, data, json); return data; } JSONObjectHolder<T> holder = getJSONTransitory(model, transitoryKey); return holder.parsedObject; }
/** * Equivalent to <pre>get(property, true)</pre> * * @return the value of the specified property * @throws UnsupportedOperationException if the value is not found in the model */ public <TYPE> TYPE get(Property<TYPE> property) { return get(property, true); }