Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#isEnabled()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#isEnabled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExportUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void exportUsersToStream(KeycloakSession session, RealmModel realm, List<UserModel> usersToExport, ObjectMapper mapper, OutputStream os, ExportOptions options) throws IOException {
    JsonFactory factory = mapper.getFactory();
    JsonGenerator generator = factory.createGenerator(os, JsonEncoding.UTF8);
    try {
        if (mapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            generator.useDefaultPrettyPrinter();
        }
        generator.writeStartObject();
        generator.writeStringField("realm", realm.getName());
        // generator.writeStringField("strategy", strategy.toString());
        generator.writeFieldName("users");
        generator.writeStartArray();

        for (UserModel user : usersToExport) {
            UserRepresentation userRep = ExportUtils.exportUser(session, realm, user, options, true);
            generator.writeObject(userRep);
        }

        generator.writeEndArray();
        generator.writeEndObject();
    } finally {
        generator.close();
    }
}
 
Example 2
Source File: ExportUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void exportFederatedUsersToStream(KeycloakSession session, RealmModel realm, List<String> usersToExport, ObjectMapper mapper, OutputStream os, ExportOptions options) throws IOException {
    JsonFactory factory = mapper.getFactory();
    JsonGenerator generator = factory.createGenerator(os, JsonEncoding.UTF8);
    try {
        if (mapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            generator.useDefaultPrettyPrinter();
        }
        generator.writeStartObject();
        generator.writeStringField("realm", realm.getName());
        // generator.writeStringField("strategy", strategy.toString());
        generator.writeFieldName("federatedUsers");
        generator.writeStartArray();

        for (String userId : usersToExport) {
            UserRepresentation userRep = ExportUtils.exportFederatedUser(session, realm, userId, options);
            generator.writeObject(userRep);
        }

        generator.writeEndArray();
        generator.writeEndObject();
    } finally {
        generator.close();
    }
}
 
Example 3
Source File: SdkPojoDeserializer.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
private SdkPojo readPojo(final SdkPojo pojo, JsonParser p, DeserializationContext ctxt) throws IOException {
    if (!p.isExpectedStartObjectToken()) {
        throw new JsonMappingException(p, "Expected to be in START_OBJECT got " + p.currentToken());
    }

    Map<String, SdkField<?>> fieldMap = getFields(pojo);
    JsonToken next = p.nextToken();
    ObjectMapper codec = (ObjectMapper) p.getCodec();
    while (next != JsonToken.END_OBJECT) {
        /*
         * if (next != JsonToken.FIELD_NAME) { throw new JsonMappingException(p,
         * "Expecting to get FIELD_NAME token, got " + next); }
         */
        String fieldName = p.getCurrentName();
        SdkField<?> sdkField = fieldMap.get(fieldName);
        if (sdkField == null) {
            if (codec.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
                throw new JsonMappingException(p, "Unknown property encountered " + fieldName);
            }
            // we need to skip this
            next = p.nextToken();
            if (next == JsonToken.START_ARRAY || next == JsonToken.START_OBJECT) {
                p.skipChildren();
            }
            // all others, just proceed to next token
            next = p.nextToken();
            continue;
        }
        // progress to next thing to read
        p.nextToken();
        // Okay we need to parse the field and set it
        Object value = readObject(sdkField, p, ctxt);
        sdkField.set(pojo, value);
        next = p.nextToken();
    }
    return pojo instanceof SdkBuilder ? (SdkPojo) ((SdkBuilder) pojo).build() : pojo;
}
 
Example 4
Source File: HalParser.java    From edison-hal with Apache License 2.0 3 votes vote down vote up
/**
 * Create a new HalParser for a JSON document.
 * <p>
 *     The specified {@code objectMapper} is used to parse documents. If not already configured
 *     appropriately, the {@code objectMapper} is copied and configured to
 *     {@link com.fasterxml.jackson.databind.DeserializationFeature#ACCEPT_SINGLE_VALUE_AS_ARRAY accept single values as arrays}.
 * </p>
 * @param json the application/hal+json document to be parsed.
 * @param objectMapper the ObjectMapper instance used to parse documents.
 * @return HalParser instance.
 *
 * @since 2.0.0
 */
public static HalParser parse(final String json, final ObjectMapper objectMapper) {
    if (objectMapper.isEnabled(ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
        return new HalParser(json, objectMapper);
    } else {
        return new HalParser(json, objectMapper
                .copy()
                .configure(ACCEPT_SINGLE_VALUE_AS_ARRAY, true));
    }
}
 
Example 5
Source File: Traverson.java    From edison-hal with Apache License 2.0 3 votes vote down vote up
/**
 * <p>
 *     Create a Traverson that is using the given {@link Function} to resolve a Link and return a HAL+JSON document.
 * </p>
 * <p>
 *     The function will be called by the Traverson, whenever a Link must be followed. The Traverson will
 *     take care of URI templates (templated links), so the implementation of the function can rely on the
 *     Link parameter to be not {@link Link#isTemplated() templated}.
 * </p>
 * <p>
 *     Typical implementations of the Function will rely on some HTTP client. Especially in this case,
 *     the function should take care of the link's {@link Link#getType() type} and {@link Link#getProfile()},
 *     so the proper HTTP Accept header is used.
 * </p>
 *
 * @param linkResolver A function that gets a Link of a resource and returns a HAL+JSON document.
 * @param objectMapper The ObjectMapper instance used to parse documents.
 * @return Traverson
 *
 * @since 2.0.0
 */
public static Traverson traverson(final LinkResolver linkResolver,
                                  final ObjectMapper objectMapper) {
    if (objectMapper.isEnabled(ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
        return new Traverson(linkResolver, objectMapper);
    } else {
        return new Traverson(linkResolver, objectMapper
                .copy()
                .configure(ACCEPT_SINGLE_VALUE_AS_ARRAY, true));
    }
}