Search in sources :

Example 76 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project Rosetta by HubSpot.

the class StoredAsJsonDeserializer method deserialize.

@Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JavaType javaType = ctxt.getTypeFactory().constructType(type);
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
        return deserialize(mapper, jp.getText(), javaType);
    } else if (jp.getCurrentToken() == JsonToken.VALUE_EMBEDDED_OBJECT) {
        String json = new String(jp.getBinaryValue(Base64Variants.getDefaultVariant()), StandardCharsets.UTF_8);
        return deserialize(mapper, json, javaType);
    } else if (jp.getCurrentToken() == JsonToken.START_OBJECT || jp.getCurrentToken() == JsonToken.START_ARRAY) {
        return mapper.readValue(jp, javaType);
    } else {
        throw ctxt.mappingException("Expected JSON String");
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 77 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project dhis2-core by dhis2.

the class V2_33_5__Update_job_parameters_with_system_setting_values method migrate.

@Override
public void migrate(final Context context) throws Exception {
    // 1. Fetch data from SystemSettings if present
    int trackerPageSize = 0;
    int eventPageSize = 0;
    int dataValuesPageSize = 0;
    String sql = "SELECT value FROM systemsetting WHERE name = '" + TRACKER_PROGRAM_SYNC_PAGE_SIZE + "';";
    try (Statement stmt = context.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(sql)) {
        if (rs.next()) {
            trackerPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
        }
    }
    sql = "SELECT value FROM systemsetting WHERE name = '" + EVENT_PROGRAM_SYNC_PAGE_SIZE + "';";
    try (Statement stmt = context.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(sql)) {
        if (rs.next()) {
            eventPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
        }
    }
    sql = "SELECT value FROM systemsetting WHERE name = '" + DATA_VALUES_SYNC_PAGE_SIZE + "';";
    try (Statement stmt = context.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(sql)) {
        if (rs.next()) {
            dataValuesPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
        }
    }
    log.info("Following values were found in SystemSettings: trackerPageSize: " + trackerPageSize + ", eventPageSize: " + eventPageSize + ", dataValuePageSize: " + dataValuesPageSize);
    // 2. Update jobParameters with data from SystemSettings
    if (trackerPageSize > 0 || eventPageSize > 0 || dataValuesPageSize > 0) {
        Map<Integer, JobParameters> updatedJobParameters = new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();
        mapper.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().allowIfBaseType(JobParameters.class).build());
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        JavaType resultingJavaType = mapper.getTypeFactory().constructType(JobParameters.class);
        ObjectReader reader = mapper.readerFor(resultingJavaType);
        ObjectWriter writer = mapper.writerFor(resultingJavaType);
        if (trackerPageSize > 0) {
            sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.TRACKER_PROGRAMS_DATA_SYNC.name() + "';";
            try (Statement stmt = context.getConnection().createStatement();
                ResultSet rs = stmt.executeQuery(sql)) {
                while (rs.next()) {
                    TrackerProgramsDataSynchronizationJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
                    jobparams.setPageSize(trackerPageSize);
                    updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
                }
            }
        }
        if (eventPageSize > 0) {
            sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.EVENT_PROGRAMS_DATA_SYNC.name() + "';";
            try (Statement stmt = context.getConnection().createStatement();
                ResultSet rs = stmt.executeQuery(sql)) {
                while (rs.next()) {
                    EventProgramsDataSynchronizationJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
                    jobparams.setPageSize(eventPageSize);
                    updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
                }
            }
        }
        sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.META_DATA_SYNC.name() + "';";
        try (Statement stmt = context.getConnection().createStatement();
            ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                MetadataSyncJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
                if (trackerPageSize > 0) {
                    jobparams.setTrackerProgramPageSize(trackerPageSize);
                }
                if (eventPageSize > 0) {
                    jobparams.setEventProgramPageSize(eventPageSize);
                }
                if (dataValuesPageSize > 0) {
                    jobparams.setDataValuesPageSize(dataValuesPageSize);
                }
                updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
            }
        }
        for (Map.Entry<Integer, JobParameters> jobParams : updatedJobParameters.entrySet()) {
            try (PreparedStatement ps = context.getConnection().prepareStatement("UPDATE jobconfiguration SET jsonbjobparameters = ? where  jobconfigurationid = ?;")) {
                PGobject pg = new PGobject();
                pg.setType("jsonb");
                pg.setValue(writer.writeValueAsString(jobParams.getValue()));
                ps.setObject(1, pg);
                ps.setInt(2, jobParams.getKey());
                ps.execute();
            }
        }
    }
    // 3. Remove entries from systemsetting table
    try (Statement stmt = context.getConnection().createStatement()) {
        stmt.executeUpdate("DELETE FROM systemsetting WHERE name IN ('" + TRACKER_PROGRAM_SYNC_PAGE_SIZE + "', '" + EVENT_PROGRAM_SYNC_PAGE_SIZE + "', '" + DATA_VALUES_SYNC_PAGE_SIZE + "');");
    }
}
Also used : HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) PreparedStatement(java.sql.PreparedStatement) EventProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.EventProgramsDataSynchronizationJobParameters) PGobject(org.postgresql.util.PGobject) JavaType(com.fasterxml.jackson.databind.JavaType) TrackerProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.TrackerProgramsDataSynchronizationJobParameters) ResultSet(java.sql.ResultSet) MetadataSyncJobParameters(org.hisp.dhis.scheduling.parameters.MetadataSyncJobParameters) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JobParameters(org.hisp.dhis.scheduling.JobParameters) EventProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.EventProgramsDataSynchronizationJobParameters) MetadataSyncJobParameters(org.hisp.dhis.scheduling.parameters.MetadataSyncJobParameters) TrackerProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.TrackerProgramsDataSynchronizationJobParameters) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 78 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project hive by apache.

the class UDFJson method evaluate.

/**
 * Extract json object from a json string based on json path specified, and
 * return json string of the extracted json object. It will return null if the
 * input json string is invalid.
 *
 * A limited version of JSONPath supported: $ : Root object . : Child operator
 * [] : Subscript operator for array * : Wildcard for []
 *
 * Syntax not supported that's worth noticing: '' : Zero length string as key
 * .. : Recursive descent &amp;#064; : Current object/element () : Script
 * expression ?() : Filter (script) expression. [,] : Union operator
 * [start:end:step] : array slice operator
 *
 * @param jsonString
 *          the json string.
 * @param pathString
 *          the json path expression.
 * @return json string or null when an error happens.
 */
public Text evaluate(String jsonString, String pathString) {
    if (jsonString == null || jsonString.isEmpty() || pathString == null || pathString.isEmpty() || pathString.charAt(0) != '$') {
        return null;
    }
    int pathExprStart = 1;
    boolean unknownType = pathString.equals("$");
    boolean isRootArray = false;
    if (pathString.length() > 1) {
        if (pathString.charAt(1) == '[') {
            pathExprStart = 0;
            isRootArray = true;
        } else if (pathString.charAt(1) == '.') {
            isRootArray = pathString.length() > 2 && pathString.charAt(2) == '[';
        } else {
            return null;
        }
    }
    // Cache pathExpr
    String[] pathExpr = pathExprCache.get(pathString);
    if (pathExpr == null) {
        pathExpr = pathString.split("\\.", -1);
        pathExprCache.put(pathString, pathExpr);
    }
    // Cache extractObject
    Object extractObject = extractObjectCache.get(jsonString);
    if (extractObject == null) {
        if (unknownType) {
            try {
                extractObject = objectMapper.readValue(jsonString, LIST_TYPE);
            } catch (Exception e) {
            // Ignore exception
            }
            if (extractObject == null) {
                try {
                    extractObject = objectMapper.readValue(jsonString, MAP_TYPE);
                } catch (Exception e) {
                    return null;
                }
            }
        } else {
            JavaType javaType = isRootArray ? LIST_TYPE : MAP_TYPE;
            try {
                extractObject = objectMapper.readValue(jsonString, javaType);
            } catch (Exception e) {
                return null;
            }
        }
        extractObjectCache.put(jsonString, extractObject);
    }
    for (int i = pathExprStart; i < pathExpr.length; i++) {
        if (extractObject == null) {
            return null;
        }
        extractObject = extract(extractObject, pathExpr[i], i == pathExprStart && isRootArray);
    }
    Text result = new Text();
    if (extractObject instanceof Map || extractObject instanceof List) {
        try {
            result.set(objectMapper.writeValueAsString(extractObject));
        } catch (Exception e) {
            return null;
        }
    } else if (extractObject != null) {
        result.set(extractObject.toString());
    } else {
        return null;
    }
    return result;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) Text(org.apache.hadoop.io.Text) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 79 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project swagger-core by swagger-api.

the class TypeNameResolver method nameForGenericType.

protected String nameForGenericType(JavaType type, Set<Options> options) {
    final StringBuilder generic = new StringBuilder(nameForClass(type, options));
    final int count = type.containedTypeCount();
    for (int i = 0; i < count; ++i) {
        final JavaType arg = type.containedType(i);
        final String argName = PrimitiveType.fromType(arg) != null ? nameForClass(arg, options) : nameForType(arg, options);
        generic.append(WordUtils.capitalize(argName));
    }
    return generic.toString();
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType)

Example 80 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project graylog2-server by Graylog2.

the class Generator method schemaForType.

private Map<String, Object> schemaForType(Type valueType) {
    final SchemaFactoryWrapper schemaFactoryWrapper = new SchemaFactoryWrapper() {

        @Override
        public JsonAnyFormatVisitor expectAnyFormat(JavaType convertedType) {
            /*final ObjectSchema s = schemaProvider.objectSchema();
                s.putProperty("anyType", schemaProvider.stringSchema());
                this.schema = s;
                return visitorFactory.anyFormatVisitor(new AnySchema());*/
            return super.expectAnyFormat(convertedType);
        }
    };
    final JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(mapper, schemaFactoryWrapper);
    try {
        final JsonSchema schema = schemaGenerator.generateSchema(mapper.getTypeFactory().constructType(valueType));
        final Map<String, Object> schemaMap = mapper.readValue(mapper.writeValueAsBytes(schema), Map.class);
        if (schemaMap.containsKey("additional_properties") && !schemaMap.containsKey("properties")) {
            schemaMap.put("properties", Collections.emptyMap());
        }
        if (schemaMap.equals(Collections.singletonMap("type", "any"))) {
            return ImmutableMap.of("type", "object", "properties", Collections.emptyMap());
        }
        return schemaMap;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema) SchemaFactoryWrapper(com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper) IOException(java.io.IOException) JsonSchemaGenerator(com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator)

Aggregations

JavaType (com.fasterxml.jackson.databind.JavaType)322 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)36 Test (org.junit.Test)29 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)25 IOException (java.io.IOException)25 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)22 ArrayList (java.util.ArrayList)21 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)17 Property (io.swagger.models.properties.Property)16 List (java.util.List)16 Map (java.util.Map)14 ModelImpl (io.swagger.models.ModelImpl)13 StringProperty (io.swagger.models.properties.StringProperty)13 Annotation (java.lang.annotation.Annotation)12 Method (java.lang.reflect.Method)11 Type (java.lang.reflect.Type)11 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)10 BodyParameter (io.swagger.models.parameters.BodyParameter)10 MapProperty (io.swagger.models.properties.MapProperty)10 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)9