Search in sources :

Example 46 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project ratpack by ratpack.

the class JsonParser method parse.

@Override
public <T> T parse(Context context, TypedData body, Parse<T, JsonParseOpts> parse) throws IOException {
    if (!body.getContentType().isJson()) {
        return null;
    }
    JsonParseOpts opts = parse.getOpts().orElse(DefaultJsonParseOpts.INSTANCE);
    TypeToken<T> type = parse.getType();
    ObjectMapper objectMapper = opts.getObjectMapper().orElseGet(() -> context.get(ObjectMapper.class));
    InputStream inputStream = body.getInputStream();
    if (type.equals(JSON_NODE_TYPE)) {
        return cast(objectMapper.readTree(inputStream));
    } else {
        return objectMapper.readValue(inputStream, toJavaType(type, objectMapper));
    }
}
Also used : InputStream(java.io.InputStream) JsonParseOpts(ratpack.jackson.JsonParseOpts) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 47 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project ratpack by ratpack.

the class DefaultConfigDataBuilder method newDefaultObjectMapper.

public static ObjectMapper newDefaultObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    objectMapper.registerModule(new Jdk8Module());
    objectMapper.registerModule(new GuavaModule());
    objectMapper.registerModule(new JavaTimeModule());
    JsonFactory factory = objectMapper.getFactory();
    factory.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
    factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
    return objectMapper;
}
Also used : Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule)

Example 48 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project ratpack by ratpack.

the class AbstractPropertiesConfigSource method loadConfigData.

@Override
public ObjectNode loadConfigData(ObjectMapper objectMapper, FileSystemBinding fileSystemBinding) throws Exception {
    ObjectNode rootNode = objectMapper.createObjectNode();
    Properties properties = loadProperties();
    Stream<Pair<String, String>> pairs = properties.stringPropertyNames().stream().map(key -> Pair.of(key, properties.getProperty(key)));
    if (prefix.isPresent()) {
        pairs = pairs.filter(p -> p.left.startsWith(prefix.get())).map(((Function<Pair<String, String>, Pair<String, String>>) p -> p.mapLeft(s -> s.substring(prefix.get().length()))).toFunction());
    }
    pairs.forEach(p -> populate(rootNode, p.left, p.right));
    return rootNode;
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Function(ratpack.func.Function) Properties(java.util.Properties) Stream(java.util.stream.Stream) FileSystemBinding(ratpack.file.FileSystemBinding) ConfigSource(ratpack.config.ConfigSource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Optional(java.util.Optional) Pair(ratpack.func.Pair) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) Function(ratpack.func.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Properties(java.util.Properties) Pair(ratpack.func.Pair)

Example 49 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project spring-security by spring-projects.

the class CookieDeserializer method deserialize.

@Override
public Cookie deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode jsonNode = mapper.readTree(jp);
    Cookie cookie = new Cookie(readJsonNode(jsonNode, "name").asText(), readJsonNode(jsonNode, "value").asText());
    cookie.setComment(readJsonNode(jsonNode, "comment").asText());
    cookie.setDomain(readJsonNode(jsonNode, "domain").asText());
    cookie.setMaxAge(readJsonNode(jsonNode, "maxAge").asInt(-1));
    cookie.setSecure(readJsonNode(jsonNode, "secure").asBoolean());
    cookie.setVersion(readJsonNode(jsonNode, "version").asInt());
    cookie.setPath(readJsonNode(jsonNode, "path").asText());
    cookie.setHttpOnly(readJsonNode(jsonNode, "httpOnly").asBoolean());
    return cookie;
}
Also used : Cookie(javax.servlet.http.Cookie) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 50 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project spring-security by spring-projects.

the class PreAuthenticatedAuthenticationTokenDeserializer method deserialize.

/**
	 * This method construct {@link PreAuthenticatedAuthenticationToken} object from serialized json.
	 * @param jp the JsonParser
	 * @param ctxt the DeserializationContext
	 * @return the user
	 * @throws IOException if a exception during IO occurs
	 * @throws JsonProcessingException if an error during JSON processing occurs
	 */
@Override
public PreAuthenticatedAuthenticationToken deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    PreAuthenticatedAuthenticationToken token = null;
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode jsonNode = mapper.readTree(jp);
    Boolean authenticated = readJsonNode(jsonNode, "authenticated").asBoolean();
    JsonNode principalNode = readJsonNode(jsonNode, "principal");
    Object principal = null;
    if (principalNode.isObject()) {
        principal = mapper.readValue(principalNode.toString(), new TypeReference<User>() {
        });
    } else {
        principal = principalNode.asText();
    }
    Object credentials = readJsonNode(jsonNode, "credentials").asText();
    List<GrantedAuthority> authorities = mapper.readValue(readJsonNode(jsonNode, "authorities").toString(), new TypeReference<List<GrantedAuthority>>() {
    });
    if (authenticated) {
        token = new PreAuthenticatedAuthenticationToken(principal, credentials, authorities);
    } else {
        token = new PreAuthenticatedAuthenticationToken(principal, credentials);
    }
    token.setDetails(readJsonNode(jsonNode, "details"));
    return token;
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) JsonNode(com.fasterxml.jackson.databind.JsonNode) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1706 Test (org.junit.Test)641 JsonNode (com.fasterxml.jackson.databind.JsonNode)270 IOException (java.io.IOException)238 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)183 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)127 Map (java.util.Map)124 HashMap (java.util.HashMap)119 ArrayList (java.util.ArrayList)81 File (java.io.File)72 InputStream (java.io.InputStream)65 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)55 JCodeModel (com.sun.codemodel.JCodeModel)52 List (java.util.List)49 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)47 JPackage (com.sun.codemodel.JPackage)44 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)41 JsonException (jmri.server.json.JsonException)41 JType (com.sun.codemodel.JType)38 Test (org.testng.annotations.Test)37