javax.json.bind.JsonbException Java Examples

The following examples show how to use javax.json.bind.JsonbException. 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: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void toJson(Object obj, Type type, OutputStream stream) throws JsonbException {
	if (type == null) throw new JsonbException("type can't be null");
	if (stream == null) throw new JsonbException("stream can't be null");
	JsonWriter jw = localWriter.get();
	try {
		jw.reset(stream);
		if (!dslJson.serialize(jw, type, obj)) {
			throw new JsonbException("Unable to serialize provided " + type);
		}
		jw.flush();
	} catch (SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	} finally {
		jw.reset(null);
	}
}
 
Example #2
Source File: CustomiseDateFormatTest.java    From Java-EE-8-Sampler with MIT License 5 votes vote down vote up
@Test
public void givenDateInJsonDocument_shouldThrowException() {
    String json = "{\"published\":\"10/10/2018\",\"author\":\"Alex Theedom\",\"title\":\"Fun with JSON-B\",\"id\":\"ABC-123-XYZ\"}";
    assertThatThrownBy(() ->
            JsonbBuilder.create().fromJson(json, Book.class)
    ).isInstanceOf(JsonbException.class)
            .hasMessageContaining("Error parsing date from value: 10/10/2018");
}
 
Example #3
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void toJson(Object obj, OutputStream stream) throws JsonbException {
	if (stream == null) throw new JsonbException("stream can't be null");
	try {
		dslJson.serialize(obj, stream);
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example #4
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void toJson(Object obj, Type type, Writer writer) throws JsonbException {
	if (type == null) throw new JsonbException("type can't be null");
	if (writer == null) throw new JsonbException("writer can't be null");
	try {
		JsonWriter jw = localWriter.get();
		jw.reset();
		if (!dslJson.serialize(jw, type, obj)) {
			throw new JsonbException("Unable to serialize provided " + type);
		}
		writer.write(new String(jw.getByteBuffer(), 0, jw.size(), "UTF-8"));
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example #5
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void toJson(Object obj, Writer writer) throws JsonbException {
	if (writer == null) throw new JsonbException("writer can't be null");
	try {
		JsonWriter jw = localWriter.get();
		jw.reset();
		dslJson.serialize(jw, obj);
		//TODO: not ideal... but lets use it instead of throwing an exception
		writer.write(new String(jw.getByteBuffer(), 0, jw.size(), "UTF-8"));
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example #6
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toJson(Object obj, Type type) throws JsonbException {
	if (type == null) throw new JsonbException("type can't be null");
	try {
		JsonWriter writer = localWriter.get();
		writer.reset();
		if (!dslJson.serialize(writer, type, obj)) {
			throw new JsonbException("Unable to serialize provided " + type);
		}
		return new String(writer.getByteBuffer(), 0, writer.size(), "UTF-8");
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example #7
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toJson(Object obj) throws JsonbException {
	try {
		JsonWriter writer = localWriter.get();
		writer.reset();
		dslJson.serialize(writer, obj);
		return new String(writer.getByteBuffer(), 0, writer.size(), "UTF-8");
	} catch (IOException | SerializationException ex) {
		throw new JsonbException(ex.getMessage(), ex.getCause());
	}
}
 
Example #8
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public <T> T fromJson(InputStream stream, Type type) throws JsonbException {
	if (stream == null) throw new JsonbException("stream can't be null");
	if (type == null) throw new JsonbException("type can't be null");
	try {
		return (T)dslJson.deserialize(type, stream);
	} catch (IOException e) {
		throw new JsonbException(e.getMessage(), e.getCause());
	}
}
 
Example #9
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public <T> T fromJson(String input, Type type) throws JsonbException {
	if (input == null) throw new JsonbException("input can't be null");
	if (type == null) throw new JsonbException("type can't be null");
	try {
		byte[] bytes = input.getBytes("UTF-8");
		return (T)dslJson.deserialize(type, bytes, bytes.length);
	} catch (IOException e) {
		throw new JsonbException(e.getMessage(), e.getCause());
	}
}
 
Example #10
Source File: ArgumentHelper.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
/**
 * This is used once we have a valid jsonString, either from above or from complex default value from graphql-java
 *
 * @param jsonString the object represented as a json String
 * @param field the field as created while scanning
 * @return the correct object
 */
private Object correctComplexObjectFromJsonString(String jsonString, Field field) throws AbstractDataFetcherException {
    Class ownerClass = classloadingService.loadClass(field.getReference().getClassName());
    try {
        Jsonb jsonb = JsonBCreator.getJsonB(field.getReference().getClassName());
        return jsonb.fromJson(jsonString, ownerClass);
    } catch (JsonbException jbe) {
        throw new TransformException(jbe, field, jsonString);
    }
}
 
Example #11
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public void toJson(final Object object, final Type runtimeType, final OutputStream stream) throws JsonbException {
    jsonb.toJson(object, runtimeType, stream);
}
 
Example #12
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public <T> T fromJson(InputStream stream, Class<T> as) throws JsonbException {
	return (T)fromJson(stream, (Type)as);
}
 
Example #13
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public <T> T fromJson(Reader reader, Type type) throws JsonbException {
	//TODO: maybe just use naive reader to stream conversion!?
	throw new JsonbException("DSL-JSON does not support Reader API. Use InputStream API instead");
}
 
Example #14
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public <T> T fromJson(Reader reader, Class<T> as) throws JsonbException {
	return (T)fromJson(reader, (Type)as);
}
 
Example #15
Source File: DslJsonbProvider.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public <T> T fromJson(String input, Class<T> as) throws JsonbException {
	return (T)fromJson(input, (Type)as);
}
 
Example #16
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public void toJson(final Object object, final OutputStream stream) throws JsonbException {
    jsonb.toJson(object, stream);
}
 
Example #17
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public void toJson(final Object object, final Type runtimeType, final Writer writer) throws JsonbException {
    jsonb.toJson(object, runtimeType, writer);
}
 
Example #18
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public void toJson(final Object object, final Writer writer) throws JsonbException {
    jsonb.toJson(object, writer);
}
 
Example #19
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public String toJson(final Object object, final Type runtimeType) throws JsonbException {
    return jsonb.toJson(object, runtimeType);
}
 
Example #20
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public String toJson(final Object object) throws JsonbException {
    return jsonb.toJson(object);
}
 
Example #21
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T fromJson(final InputStream stream, final Type runtimeType) throws JsonbException {
    return jsonb.fromJson(stream, runtimeType);
}
 
Example #22
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T fromJson(final InputStream stream, final Class<T> type) throws JsonbException {
    return jsonb.fromJson(stream, type);
}
 
Example #23
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T fromJson(final Reader reader, final Type runtimeType) throws JsonbException {
    return jsonb.fromJson(reader, runtimeType);
}
 
Example #24
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T fromJson(final Reader reader, final Class<T> type) throws JsonbException {
    return jsonb.fromJson(reader, type);
}
 
Example #25
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T fromJson(final String str, final Type runtimeType) throws JsonbException {
    return jsonb.fromJson(str, runtimeType);
}
 
Example #26
Source File: GenericOrPojoJsonb.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T fromJson(final String str, final Class<T> type) throws JsonbException {
    return jsonb.fromJson(str, type);
}
 
Example #27
Source File: JsonDataConverter.java    From TweetwallFX with MIT License 3 votes vote down vote up
/**
 * Converts the {@code jsonString} parameter into a POJO of the type
 * {@code typeClass}.
 *
 * @param <T> the type to convert into
 *
 * @param jsonString the String to convert from
 *
 * @param typeClass the class of the type to convert into
 *
 * @return the converted object
 */
public static <T> T convertFromString(final String jsonString, final Class<T> typeClass) {
    try {
        return JSONB.fromJson(jsonString, typeClass);
    } catch (final JsonbException je) {
        LOG.error("Failed to convert to " + typeClass + ": " + jsonString, je);
        throw je;
    }
}