/** */ @Override public Ref deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { if (jp.getCurrentToken() != JsonToken.VALUE_STRING) throw new IllegalStateException("Cannot yet deserialize Refs that were serialized to a full entity object (as opposed to just string key representation)"); String text = jp.getText(); return Ref.create(Key.create(text)); } }
/** * Obtain the entity value, throwing an exception if the entity was not found. * * @return the entity referenced. Never returns null. * @throws NotFoundException if the specified entity was not found */ final public T safe() throws NotFoundException { T t = this.get(); if (t == null) throw new NotFoundException(key()); else return t; }
@Override public void serialize(Ref value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { Object obj = value.getValue(); if (obj != null) { // writeObject() abandons the serialization context and breaks @JsonView //jgen.writeObject(obj); // Tatu says that this is better: provider.defaultSerializeValue(value.getValue(), jgen); } else { jgen.writeString(value.key().getString()); } } }
/** * This method exists to facilitate serialization via javabeans conventions. Unlike get(), * it will return null if isLoaded() is false. * * @return the entity referenced, or null if either the entity was not found or isLoaded() is false. */ final public T getValue() { return isLoaded() ? get() : null; }
/** * Same as key() but conforms to JavaBeans conventions in case this is being processed by a JSON * converter or expression language. */ final public Key<T> getKey() { return key(); }
@Override protected void doSerialize( JsonWriter writer, Ref<T> ref, JsonSerializationContext ctx, JsonSerializerParameters params ) { writer.beginObject(); writer.name( RefConstant.KEY ); KeyJsonSerializer.getInstance().serialize( writer, ref.getKey(), ctx, params ); writer.name( RefConstant.VALUE ); serializer.serialize( writer, ref.getValue(), ctx, params ); writer.endObject(); } }
public static <T> T unref(Ref<T> ref) { return ref == null ? null : ref.get(); }
/** Type-safe comparison for key equivalence */ public boolean equivalent(Ref<T> other) { return equals(other); }
@Override public void serialize( Ref ref, JsonGenerator jgen, SerializerProvider provider ) throws IOException { jgen.writeStartObject(); boolean includeNull = Include.NON_NULL != provider.getConfig().getDefaultPropertyInclusion().getValueInclusion(); if ( ref.key() != null || includeNull ) { jgen.writeObjectField( RefConstant.KEY, ref.key() ); } if ( ref.getValue() != null || includeNull ) { jgen.writeObjectField( RefConstant.VALUE, ref.getValue() ); } jgen.writeEndObject(); }
/** Creates a Ref from a registered pojo entity */ public static <T> Ref<T> create(T value) { Key<T> key = Key.create(value); return create(key); }
/** Renders some info about the key */ @Override public String toString() { return this.getClass().getSimpleName() + "(" + key() + ")"; } }
public static <T> Ref<T> ref(T instance) { return instance == null ? null : Ref.create(instance); }
/** Hash code is simply that of key */ @Override public int hashCode() { return key().hashCode(); }
public static <T> Ref<T> ref(Key<T> key) { return key == null ? null : Ref.create(key); }
/** * Callback that we found a Ref in the object graph. Subclasses of this context may want to do something * special with this. */ public Key saveRef(final Ref<?> value, final LoadConditions loadConditions) { return value.key().getRaw(); } }
@Override public void serialize(Ref value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeFieldName(value.key().getString()); } }
/** Type safe comparison for key equivalence */ public boolean equivalent(Key<T> other) { return key().equivalent(other); }
@Override public <E> LoadResult<E> ref(final Ref<E> ref) { return key(ref.key()); }