Search in sources :

Example 31 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project eventapis by kloiasoft.

the class RollbackCommandSpec method rollback.

default void rollback(CommandRecord record) {
    // Get from context
    ObjectMapper objectMapper = new ObjectMapper();
    for (Method method : this.getClass().getDeclaredMethods()) {
        if (method.getName().equals("rollback")) {
            if (method.getParameterCount() == 1 && method.getParameterTypes()[0] == CommandRecord.class)
                continue;
            try {
                Object[] args = new Object[method.getParameterCount()];
                for (Map.Entry<Integer, ?> entry : record.getParameters().entrySet()) {
                    Class<?> type = method.getParameterTypes()[entry.getKey()];
                    args[entry.getKey()] = objectMapper.treeToValue((TreeNode) entry.getValue(), type);
                }
                method.invoke(this, args);
            } catch (InvocationTargetException | IllegalAccessException | IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TreeNode(com.fasterxml.jackson.core.TreeNode) CommandRecord(com.kloia.eventapis.pojos.CommandRecord) AbstractMap(java.util.AbstractMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 32 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project bson4jackson by michel-kraemer.

the class BsonJavaScriptDeserializer method deserialize.

@Override
@SuppressWarnings("deprecation")
public JavaScript deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (jp instanceof BsonParser) {
        BsonParser bsonParser = (BsonParser) jp;
        if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT || (bsonParser.getCurrentBsonType() != BsonConstants.TYPE_JAVASCRIPT && bsonParser.getCurrentBsonType() != BsonConstants.TYPE_JAVASCRIPT_WITH_SCOPE)) {
            throw ctxt.mappingException(JavaScript.class);
        }
        return (JavaScript) bsonParser.getEmbeddedObject();
    } else {
        TreeNode tree = jp.getCodec().readTree(jp);
        String code = null;
        TreeNode codeNode = tree.get("$code");
        if (codeNode instanceof ValueNode) {
            code = ((ValueNode) codeNode).asText();
        }
        Map<String, Object> scope = null;
        TreeNode scopeNode = tree.get("$scope");
        if (scopeNode instanceof ObjectNode) {
            @SuppressWarnings("unchecked") Map<String, Object> scope2 = jp.getCodec().treeToValue(scopeNode, Map.class);
            scope = scope2;
        }
        return new JavaScript(code, scope);
    }
}
Also used : BsonParser(de.undercouch.bson4jackson.BsonParser) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TreeNode(com.fasterxml.jackson.core.TreeNode) JavaScript(de.undercouch.bson4jackson.types.JavaScript) ValueNode(com.fasterxml.jackson.databind.node.ValueNode)

Example 33 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project bson4jackson by michel-kraemer.

the class BsonRegexDeserializer method deserialize.

@Override
@SuppressWarnings("deprecation")
public Pattern deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    if (jp instanceof BsonParser) {
        BsonParser bsonParser = (BsonParser) jp;
        if (bsonParser.getCurrentToken() != JsonToken.VALUE_EMBEDDED_OBJECT || bsonParser.getCurrentBsonType() != BsonConstants.TYPE_REGEX) {
            throw ctxt.mappingException(Pattern.class);
        }
        return (Pattern) bsonParser.getEmbeddedObject();
    } else {
        TreeNode tree = jp.getCodec().readTree(jp);
        String pattern = null;
        TreeNode patternNode = tree.get("$pattern");
        if (patternNode instanceof ValueNode) {
            pattern = ((ValueNode) patternNode).asText();
        }
        int flags = 0;
        TreeNode flagsNode = tree.get("$flags");
        if (flagsNode instanceof ValueNode) {
            flags = ((ValueNode) flagsNode).asInt();
        }
        return Pattern.compile(pattern, flags);
    }
}
Also used : Pattern(java.util.regex.Pattern) BsonParser(de.undercouch.bson4jackson.BsonParser) TreeNode(com.fasterxml.jackson.core.TreeNode) ValueNode(com.fasterxml.jackson.databind.node.ValueNode)

Example 34 with TreeNode

use of com.fasterxml.jackson.core.TreeNode in project keycloak by keycloak.

the class ClaimInformationPointProviderTest method testBodyJsonClaimsInformationPoint.

@Test
public void testBodyJsonClaimsInformationPoint() throws Exception {
    Map<String, List<String>> headers = new HashMap<>();
    headers.put("Content-Type", Arrays.asList("application/json"));
    ObjectMapper mapper = JsonSerialization.mapper;
    JsonParser parser = mapper.getFactory().createParser("{\"a\": {\"b\": {\"c\": \"c-value\"}}, \"d\": [\"d-value1\", \"d-value2\"], \"e\": {\"number\": 123}}");
    TreeNode treeNode = mapper.readTree(parser);
    HttpFacade httpFacade = createHttpFacade(headers, new ByteArrayInputStream(treeNode.toString().getBytes()));
    Map<String, List<String>> claims = getClaimInformationProviderForPath("/claims-provider", "claims").resolve(httpFacade);
    assertEquals("c-value", claims.get("claim-from-json-body-object").get(0));
    assertEquals("d-value2", claims.get("claim-from-json-body-array").get(0));
    assertEquals("123", claims.get("claim-from-json-body-number").get(0));
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) TreeNode(com.fasterxml.jackson.core.TreeNode) OIDCHttpFacade(org.keycloak.adapters.OIDCHttpFacade) HttpFacade(org.keycloak.adapters.spi.HttpFacade) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Aggregations

TreeNode (com.fasterxml.jackson.core.TreeNode)34 TextNode (com.fasterxml.jackson.databind.node.TextNode)11 JsonParser (com.fasterxml.jackson.core.JsonParser)10 IOException (java.io.IOException)8 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 HashMap (java.util.HashMap)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)4 BsonParser (de.undercouch.bson4jackson.BsonParser)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)3 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 Sets (com.google.common.collect.Sets)3 Iterator (java.util.Iterator)3 Test (org.junit.Test)3 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)3 HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)2