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");
}
}
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 + "');");
}
}
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 &#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;
}
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();
}
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);
}
}
Aggregations