Skip to content

Commit

Permalink
Support customizable filter value serialization in links #403
Browse files Browse the repository at this point in the history
switched from simple toString to TypeParser that allows to register custom string mappers.
  • Loading branch information
remmeier committed Oct 28, 2018
1 parent 5585c4f commit d55c8b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.fasterxml.jackson.databind.node.ValueNode;

import java.io.IOException;

Expand Down Expand Up @@ -34,9 +34,12 @@ public T parse(String input) {
public String toString(T input) {
JsonNode jsonNode = mapper.valueToTree(input);

if (jsonNode instanceof ValueNode) {
if (jsonNode instanceof TextNode) {
return jsonNode.textValue();
}
if (jsonNode instanceof NumericNode) {
return jsonNode.asText();
}

// fallback to String for complex type
return input.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.crnk.core.engine.information.resource.ResourceInformation;
import io.crnk.core.engine.internal.utils.ClassUtils;
import io.crnk.core.engine.internal.utils.PreconditionUtil;
import io.crnk.core.engine.internal.utils.StringUtils;
import io.crnk.core.engine.parser.ParserException;
import io.crnk.core.engine.parser.TypeParser;
Expand Down Expand Up @@ -223,11 +224,14 @@ protected String addResourceType(QueryParameterType type, String key, ResourceIn
return type.toString().toLowerCase() + "[" + resourceType + "]" + (key != null ? key : "");
}

protected static String serializeValue(Object value) {
protected String serializeValue(Object value) {
if (value == null) {
return NULL_VALUE_STRING;
}
return value.toString();
TypeParser typeParser = context.getTypeParser();
String strValue = typeParser.toString(value);
PreconditionUtil.verify(strValue != null, "should not map to null: %s", value);
return strValue;
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions crnk-documentation/src/docs/releases/latest/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ <h4>Historic Activiti repositories</h4>
return ActivitiModule.create(processEngine, config);
</pre>

<h4>Bidirectional Object to String mapping with TypeParser</h4>

The TypeParser class of Crnk has been extended to not only parse Strings, but also map objects back to Strings.
Most notably, the DefaultQuerySpecUrlMapper makes use of this functionality to represent filter values
as Strings in urls. Thereby, the extensibility of the TypeParser allows to customize that mapping.



<h4>Simplified customizations of Criteria-based crnk-jpa repositories</h4>

Expand Down

0 comments on commit d55c8b5

Please sign in to comment.