org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException Java Examples

The following examples show how to use org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException. 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: RestClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #2
Source File: DatadogHttpClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeMeterWithoutHost() throws JsonProcessingException {

	DMeter m = new DMeter(new Meter() {
		@Override
		public void markEvent() {}

		@Override
		public void markEvent(long n) {}

		@Override
		public double getRate() {
			return 1;
		}

		@Override
		public long getCount() {
			return 0;
		}
	}, "testMeter", null, tags, () -> MOCKED_SYSTEM_MILLIS);

	assertEquals(
		"{\"metric\":\"testMeter\",\"type\":\"gauge\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1.0]]}",
		DatadogHttpClient.serialize(m));
}
 
Example #3
Source File: DatadogHttpClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeMeter() throws JsonProcessingException {

	DMeter m = new DMeter(new Meter() {
		@Override
		public void markEvent() {}

		@Override
		public void markEvent(long n) {}

		@Override
		public double getRate() {
			return 1;
		}

		@Override
		public long getCount() {
			return 0;
		}
	}, "testMeter", "localhost", tags, () -> MOCKED_SYSTEM_MILLIS);

	assertEquals(
		"{\"metric\":\"testMeter\",\"type\":\"gauge\",\"host\":\"localhost\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1.0]]}",
		DatadogHttpClient.serialize(m));
}
 
Example #4
Source File: Pipeline.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public String toJson() {
	ObjectMapper mapper = new ObjectMapper();

	List<Map<String, String>> stageJsons = new ArrayList<>();
	for (PipelineStage s : getStages()) {
		Map<String, String> stageMap = new HashMap<>();
		stageMap.put("stageClassName", s.getClass().getTypeName());
		stageMap.put("stageJson", s.toJson());
		stageJsons.add(stageMap);
	}

	try {
		return mapper.writeValueAsString(stageJsons);
	} catch (JsonProcessingException e) {
		throw new RuntimeException("Failed to serialize pipeline", e);
	}
}
 
Example #5
Source File: JobDetailsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can marshal and unmarshal JobDetails instances.
 */
@Test
public void testJobDetailsMarshalling() throws JsonProcessingException {
	final JobDetails expected = new JobDetails(
		new JobID(),
		"foobar",
		1L,
		10L,
		9L,
		JobStatus.RUNNING,
		8L,
		new int[]{1, 3, 3, 7, 4, 2, 7, 3, 3},
		42);

	final ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();

	final JsonNode marshalled = objectMapper.valueToTree(expected);

	final JobDetails unmarshalled = objectMapper.treeToValue(marshalled, JobDetails.class);

	assertEquals(expected, unmarshalled);
}
 
Example #6
Source File: RestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #7
Source File: RestAPIDocGenerator.java    From flink with Apache License 2.0 6 votes vote down vote up
private static String createMessageHtmlEntry(Class<?> messageClass, @Nullable Class<?> nestedAsyncOperationResultClass, Class<?> emptyMessageClass) {
	JsonSchema schema = generateSchema(messageClass);

	if (nestedAsyncOperationResultClass != null) {
		JsonSchema innerSchema = generateSchema(nestedAsyncOperationResultClass);
		schema.asObjectSchema().getProperties().put(AsynchronousOperationResult.FIELD_NAME_OPERATION, innerSchema);
	}

	String json;
	if (messageClass == emptyMessageClass) {
		json = "{}";
	} else {
		try {
			json = mapper.writerWithDefaultPrettyPrinter()
				.writeValueAsString(schema);
		} catch (JsonProcessingException e) {
			LOG.error("Failed to write message schema for class {}.", messageClass.getCanonicalName(), e);
			throw new RuntimeException("Failed to write message schema for class " + messageClass.getCanonicalName() + ".", e);
		}
	}

	return json;
}
 
Example #8
Source File: Pipeline.java    From Alink with Apache License 2.0 6 votes vote down vote up
@Override
public String toJson() {
	ObjectMapper mapper = new ObjectMapper();

	List<Map<String, String>> stageJsons = new ArrayList<>();
	for (PipelineStage s : getStages()) {
		Map<String, String> stageMap = new HashMap<>();
		stageMap.put("stageClassName", s.getClass().getTypeName());
		stageMap.put("stageJson", s.toJson());
		stageJsons.add(stageMap);
	}

	try {
		return mapper.writeValueAsString(stageJsons);
	} catch (JsonProcessingException e) {
		throw new RuntimeException("Failed to toString pipeline", e);
	}
}
 
Example #9
Source File: JsonPathUdfTest.java    From sylph with Apache License 2.0 6 votes vote down vote up
@Before
public void init()
        throws JsonProcessingException
{
    String json = MAPPER.writeValueAsString(ImmutableMap.of("user_id", "uid_001",
            "ip", "127.0.0.1",
            "store", 12.0,
            "key1", ImmutableMap.of("key2", 123)
    ));

    StreamExecutionEnvironment execEnv = StreamExecutionEnvironment.createLocalEnvironment();
    execEnv.setParallelism(2);
    tableEnv = StreamTableEnvironment.create(execEnv);
    tableEnv.registerFunction("get_json_object", new UDFJson());
    table = tableEnv.sqlQuery("select '" + json + "' as message");
}
 
Example #10
Source File: DatadogHttpClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeMeterWithoutHost() throws JsonProcessingException {

	DMeter m = new DMeter(new Meter() {
		@Override
		public void markEvent() {}

		@Override
		public void markEvent(long n) {}

		@Override
		public double getRate() {
			return 1;
		}

		@Override
		public long getCount() {
			return 0;
		}
	}, "testMeter", null, tags);

	assertEquals(
		"{\"metric\":\"testMeter\",\"type\":\"gauge\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1.0]]}",
		DatadogHttpClient.serialize(m));
}
 
Example #11
Source File: DatadogHttpClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeMeter() throws JsonProcessingException {

	DMeter m = new DMeter(new Meter() {
		@Override
		public void markEvent() {}

		@Override
		public void markEvent(long n) {}

		@Override
		public double getRate() {
			return 1;
		}

		@Override
		public long getCount() {
			return 0;
		}
	}, "testMeter", "localhost", tags);

	assertEquals(
		"{\"metric\":\"testMeter\",\"type\":\"gauge\",\"host\":\"localhost\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1.0]]}",
		DatadogHttpClient.serialize(m));
}
 
Example #12
Source File: Pipeline.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public String toJson() {
	ObjectMapper mapper = new ObjectMapper();

	List<Map<String, String>> stageJsons = new ArrayList<>();
	for (PipelineStage s : getStages()) {
		Map<String, String> stageMap = new HashMap<>();
		stageMap.put("stageClassName", s.getClass().getTypeName());
		stageMap.put("stageJson", s.toJson());
		stageJsons.add(stageMap);
	}

	try {
		return mapper.writeValueAsString(stageJsons);
	} catch (JsonProcessingException e) {
		throw new RuntimeException("Failed to serialize pipeline", e);
	}
}
 
Example #13
Source File: JobDetailsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can marshal and unmarshal JobDetails instances.
 */
@Test
public void testJobDetailsMarshalling() throws JsonProcessingException {
	final JobDetails expected = new JobDetails(
		new JobID(),
		"foobar",
		1L,
		10L,
		9L,
		JobStatus.RUNNING,
		8L,
		new int[]{1, 3, 3, 7, 4, 2, 7, 3, 3},
		42);

	final ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();

	final JsonNode marshalled = objectMapper.valueToTree(expected);

	final JobDetails unmarshalled = objectMapper.treeToValue(marshalled, JobDetails.class);

	assertEquals(expected, unmarshalled);
}
 
Example #14
Source File: RestClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
	CompletableFuture<P> responseFuture = new CompletableFuture<>();
	final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
	try {
		P response = objectMapper.readValue(jsonParser, responseType);
		responseFuture.complete(response);
	} catch (IOException originalException) {
		// the received response did not matched the expected response type

		// lets see if it is an ErrorResponse instead
		try {
			ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
			responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
		} catch (JsonProcessingException jpe2) {
			// if this fails it is either the expected type or response type was wrong, most likely caused
			// by a client/search MessageHeaders mismatch
			LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
			responseFuture.completeExceptionally(
				new RestClientException(
					"Response was neither of the expected type(" + responseType + ") nor an error.",
					originalException,
					rawResponse.getHttpResponseStatus()));
		}
	}
	return responseFuture;
}
 
Example #15
Source File: JobDetailsTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that we can marshal and unmarshal JobDetails instances.
 */
@Test
public void testJobDetailsMarshalling() throws JsonProcessingException {
	final JobDetails expected = new JobDetails(
		new JobID(),
		"foobar",
		1L,
		10L,
		9L,
		JobStatus.RUNNING,
		8L,
		new int[]{1, 3, 3, 7, 4, 2, 7, 3, 3},
		42);

	final ObjectMapper objectMapper = RestMapperUtils.getStrictObjectMapper();

	final JsonNode marshalled = objectMapper.valueToTree(expected);

	final JobDetails unmarshalled = objectMapper.treeToValue(marshalled, JobDetails.class);

	assertEquals(expected, unmarshalled);
}
 
Example #16
Source File: DatadogHttpClientTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeMeter() throws JsonProcessingException {

	DMeter m = new DMeter(new Meter() {
		@Override
		public void markEvent() {}

		@Override
		public void markEvent(long n) {}

		@Override
		public double getRate() {
			return 1;
		}

		@Override
		public long getCount() {
			return 0;
		}
	}, "testMeter", "localhost", tags);

	assertEquals(
		"{\"metric\":\"testMeter\",\"type\":\"gauge\",\"host\":\"localhost\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1.0]]}",
		DatadogHttpClient.serialize(m));
}
 
Example #17
Source File: DatadogHttpClientTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeMeterWithoutHost() throws JsonProcessingException {

	DMeter m = new DMeter(new Meter() {
		@Override
		public void markEvent() {}

		@Override
		public void markEvent(long n) {}

		@Override
		public double getRate() {
			return 1;
		}

		@Override
		public long getCount() {
			return 0;
		}
	}, "testMeter", null, tags);

	assertEquals(
		"{\"metric\":\"testMeter\",\"type\":\"gauge\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1.0]]}",
		DatadogHttpClient.serialize(m));
}
 
Example #18
Source File: RestAPIDocGenerator.java    From flink with Apache License 2.0 5 votes vote down vote up
private static JsonSchema generateSchema(Class<?> messageClass) {
	try {
		return schemaGen.generateSchema(messageClass);
	} catch (JsonProcessingException e) {
		LOG.error("Failed to generate message schema for class {}.", messageClass, e);
		throw new RuntimeException("Failed to generate message schema for class " + messageClass.getCanonicalName() + ".", e);
	}
}
 
Example #19
Source File: JsonConverter.java    From Alink with Apache License 2.0 5 votes vote down vote up
/**
 * serialize an object to pretty json.
 *
 * @param src the object serialized as json
 * @return the json string
 */
public static String toPrettyJson(Object src) {
	try {
		return JSON_INSTANCE.writerWithDefaultPrettyPrinter().writeValueAsString(src);
	} catch (JsonProcessingException e) {
		throw new IllegalJsonFormatException("Serialize object to json fail.", e);
	}
}
 
Example #20
Source File: ControlMessage.java    From flink-siddhi with Apache License 2.0 5 votes vote down vote up
public ControlMessage(ControlEvent event) {
    this.type = event.getClass().getName();
    ObjectMapper mapper = new ObjectMapper();
    try {
        this.payload = mapper.writeValueAsString(event);
    } catch (JsonProcessingException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example #21
Source File: JsonSerializationSchema.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(T t) {
    try {
        return mapper.writeValueAsBytes(t);
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Error while serializing the record to Json", e);
    }
}
 
Example #22
Source File: JsonRowDeserializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private DeserializationRuntimeConverter createFallbackConverter(Class<?> valueType) {
	return (mapper, jsonNode) -> {
		// for types that were specified without JSON schema
		// e.g. POJOs
		try {
			return mapper.treeToValue(jsonNode, valueType);
		} catch (JsonProcessingException e) {
			throw new JsonParseException(format("Could not convert node: %s", jsonNode), e);
		}
	};
}
 
Example #23
Source File: FlinkMetricContainer.java    From flink with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static ArrayList getNameSpaceArray(MetricKey metricKey) {
	MetricName metricName = metricKey.metricName();
	try {
		return new ObjectMapper().readValue(metricName.getNamespace(), ArrayList.class);
	} catch (JsonProcessingException e) {
		throw new RuntimeException(
			String.format("Parse namespace[%s] error. ", metricName.getNamespace()), e);
	}
}
 
Example #24
Source File: DatadogHttpClientTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeGauge() throws JsonProcessingException {

	DGauge g = new DGauge(new Gauge<Number>() {
		@Override
		public Number getValue() {
			return 1;
		}
	}, "testCounter", "localhost", tags);

	assertEquals(
		"{\"metric\":\"testCounter\",\"type\":\"gauge\",\"host\":\"localhost\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1]]}",
		DatadogHttpClient.serialize(g));
}
 
Example #25
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example #26
Source File: RestAPIStabilityTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static <X> X jsonToObject(final JsonNode jsonContainer, final Class<X> containerClass) {
	try {
		return OBJECT_MAPPER.treeToValue(jsonContainer, containerClass);
	} catch (JsonProcessingException e) {
		throw new RuntimeException(e);
	}
}
 
Example #27
Source File: RestClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void readRawResponse(FullHttpResponse msg) {
	ByteBuf content = msg.content();

	JsonNode rawResponse;
	try (InputStream in = new ByteBufInputStream(content)) {
		rawResponse = objectMapper.readTree(in);
		LOG.debug("Received response {}.", rawResponse);
	} catch (JsonProcessingException je) {
		LOG.error("Response was not valid JSON.", je);
		// let's see if it was a plain-text message instead
		content.readerIndex(0);
		try (ByteBufInputStream in = new ByteBufInputStream(content)) {
			byte[] data = new byte[in.available()];
			in.readFully(data);
			String message = new String(data);
			LOG.error("Unexpected plain-text response: {}", message);
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, but plain-text: " + message, je, msg.getStatus()));
		} catch (IOException e) {
			jsonFuture.completeExceptionally(new RestClientException("Response was not valid JSON, nor plain-text.", je, msg.getStatus()));
		}
		return;
	} catch (IOException ioe) {
		LOG.error("Response could not be read.", ioe);
		jsonFuture.completeExceptionally(new RestClientException("Response could not be read.", ioe, msg.getStatus()));
		return;
	}
	jsonFuture.complete(new JsonResponse(rawResponse, msg.getStatus()));
}
 
Example #28
Source File: Params.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a json containing all parameters in this Params. The json should be human-readable if
 * possible.
 *
 * @return a json containing all parameters in this Params
 */
public String toJson() {
	assertMapperInited();
	try {
		return mapper.writeValueAsString(params);
	} catch (JsonProcessingException e) {
		throw new RuntimeException("Failed to serialize params to json", e);
	}
}
 
Example #29
Source File: Params.java    From flink with Apache License 2.0 5 votes vote down vote up
private String valueToJson(Object value) {
	assertMapperInited();
	try {
		if (value == null) {
			return null;
		}
		return mapper.writeValueAsString(value);
	} catch (JsonProcessingException e) {
		throw new RuntimeException("Failed to serialize to json:" + value, e);
	}
}
 
Example #30
Source File: DatadogHttpClientTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeGauge() throws JsonProcessingException {

	DGauge g = new DGauge(new Gauge<Number>() {
		@Override
		public Number getValue() {
			return 1;
		}
	}, "testCounter", "localhost", tags, () -> MOCKED_SYSTEM_MILLIS);

	assertEquals(
		"{\"metric\":\"testCounter\",\"type\":\"gauge\",\"host\":\"localhost\",\"tags\":[\"tag1\",\"tag2\"],\"points\":[[123,1]]}",
		DatadogHttpClient.serialize(g));
}