Skip to content

Commit

Permalink
Register a StringMapper for JsonNode
Browse files Browse the repository at this point in the history
  • Loading branch information
cowwoc committed Aug 30, 2024
1 parent 85e3488 commit 6f43a36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.github.cowwoc.requirements10.jackson.validator.JsonNodeValidator;
import com.github.cowwoc.requirements10.java.ValidationFailure;
import com.github.cowwoc.requirements10.java.internal.Configuration;
import com.github.cowwoc.requirements10.java.internal.ConfigurationUpdater;
import com.github.cowwoc.requirements10.java.internal.MutableStringMappers;
import com.github.cowwoc.requirements10.java.internal.scope.ApplicationScope;
import com.github.cowwoc.requirements10.java.internal.validator.AbstractValidators;

Expand All @@ -29,6 +31,11 @@ public class JacksonValidatorsImpl extends AbstractValidators<JacksonValidators>
public JacksonValidatorsImpl(ApplicationScope scope, Configuration configuration)
{
super(scope, configuration);
try (ConfigurationUpdater config = updateConfiguration())
{
MutableStringMappers stringMappers = config.stringMappers();
stringMappers.putIfAbsent(JsonNode.class, (value, seen) -> ((JsonNode) value).toPrettyString());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public MutableStringMappers put(Class<?> type, StringMapper mapper)
return this;
}

/**
* Returns the function that maps an object of the given type to a String.
* @param type a type
* @param mapper a function that returns the String representation of the type's instances
* @return this
*/
public MutableStringMappers putIfAbsent(Class<?> type, StringMapper mapper)
{
typeToMapper.putIfAbsent(Optional.ofNullable(type), mapper);
return this;
}

/**
* Removes a mapper for a type.
*
Expand Down

0 comments on commit 6f43a36

Please sign in to comment.