use of com.google.gson.JsonSyntaxException in project spring-cloud-function by spring-cloud.
the class FluxHandlerMethodArgumentResolver method resolveArgument.
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
Object handler = webRequest.getAttribute(WebRequestConstants.HANDLER, NativeWebRequest.SCOPE_REQUEST);
Class<?> type = inspector.getInputType(handler);
if (type == null) {
type = Object.class;
}
boolean message = inspector.isMessage(handler);
List<Object> body;
ContentCachingRequestWrapper nativeRequest = new ContentCachingRequestWrapper(webRequest.getNativeRequest(HttpServletRequest.class));
if (logger.isDebugEnabled()) {
logger.debug("Resolving request body into type: " + type);
}
if (isPlainText(webRequest) && CharSequence.class.isAssignableFrom(type)) {
body = Arrays.asList(StreamUtils.copyToString(nativeRequest.getInputStream(), Charset.forName("UTF-8")));
} else {
try {
body = mapper.fromJson(new InputStreamReader(nativeRequest.getInputStream()), ResolvableType.forClassWithGenerics(ArrayList.class, type).getType());
} catch (JsonSyntaxException e) {
nativeRequest.setAttribute(WebRequestConstants.INPUT_SINGLE, true);
body = Arrays.asList(mapper.fromJson(new String(nativeRequest.getContentAsByteArray()), type));
}
}
if (message) {
List<Object> messages = new ArrayList<>();
MessageHeaders headers = HeaderUtils.fromHttp(new ServletServerHttpRequest(webRequest.getNativeRequest(HttpServletRequest.class)).getHeaders());
for (Object payload : body) {
messages.add(MessageUtils.create(handler, payload, headers));
}
body = messages;
}
return new FluxRequest<Object>(body);
}
use of com.google.gson.JsonSyntaxException in project iosched by google.
the class SyncCommand method execute.
@Override
public void execute(Context context, String type, String extraData) {
LOGI(TAG, "Received FCM message: " + type);
int syncJitter;
SyncData syncData = null;
if (extraData != null) {
try {
Gson gson = new Gson();
syncData = gson.fromJson(extraData, SyncData.class);
} catch (JsonSyntaxException e) {
LOGI(TAG, "Error while decoding extraData: " + e.toString());
}
}
if (syncData != null && syncData.sync_jitter != 0) {
syncJitter = syncData.sync_jitter;
} else {
syncJitter = DEFAULT_TRIGGER_SYNC_MAX_JITTER_MILLIS;
}
scheduleSync(context, syncJitter);
}
use of com.google.gson.JsonSyntaxException in project iosched by google.
the class SyncUserCommand method execute.
@Override
public void execute(Context context, String type, String extraData) {
LOGI(TAG, "Received FCM message: " + type);
int syncJitter;
SyncData syncData = null;
if (extraData != null) {
try {
Gson gson = new Gson();
syncData = gson.fromJson(extraData, SyncData.class);
} catch (JsonSyntaxException e) {
LOGI(TAG, "Error while decoding extraData: " + e.toString());
}
}
if (syncData != null && syncData.sync_jitter != 0) {
syncJitter = syncData.sync_jitter;
} else {
syncJitter = DEFAULT_TRIGGER_SYNC_DELAY;
}
scheduleSync(context, syncJitter);
}
use of com.google.gson.JsonSyntaxException in project openems by OpenEMS.
the class ConfigChannel method setChannelDoc.
/**
* Sets values for this ConfigChannel using its annotation
*
* This method is called by reflection from {@link InjectionUtils.getThingInstance}
*
* @param parent
* @throws OpenemsException
*/
@Override
public void setChannelDoc(ChannelDoc channelDoc) throws OpenemsException {
super.setChannelDoc(channelDoc);
if (!this.isOptional.isPresent()) {
this.isOptional = Optional.of(channelDoc.isOptional());
}
if (!this.defaultValue.isPresent() && !channelDoc.getDefaultValue().isEmpty()) {
JsonElement jValue = null;
try {
jValue = (new JsonParser()).parse(channelDoc.getDefaultValue());
@SuppressWarnings("unchecked") T value = (T) JsonUtils.getAsType(type().get(), jValue);
this.defaultValue(value);
} catch (NotImplementedException | JsonSyntaxException e) {
throw new OpenemsException("Unable to set defaultValue [" + jValue + "] " + e.getMessage());
}
}
}
use of com.google.gson.JsonSyntaxException in project vertigo by KleeGroup.
the class JsonConverterWebServiceHandlerPlugin method readParameterValue.
private void readParameterValue(final Request request, final WebServiceCallContext routeContext, final WebServiceParam webServiceParam) {
try {
boolean found = false;
JsonReader jsonReaderToApply = null;
JsonConverter jsonConverterToApply = null;
for (final JsonReader jsonReader : jsonReaders.get(webServiceParam.getParamType())) {
jsonReaderToApply = jsonReader;
for (final JsonConverter jsonConverter : jsonConverters.get(jsonReader.getSupportedOutput())) {
if (jsonConverter.canHandle(webServiceParam.getType())) {
jsonConverterToApply = jsonConverter;
found = true;
break;
}
}
if (found) {
break;
}
}
// -----
Assertion.checkNotNull(jsonReaderToApply, "Can't parse param {0} of service {1} {2} no compatible JsonReader found for {3}", webServiceParam.getFullName(), routeContext.getWebServiceDefinition().getVerb(), routeContext.getWebServiceDefinition().getPath(), webServiceParam.getParamType());
Assertion.checkNotNull(jsonConverterToApply, "Can't parse param {0} of service {1} {2} no compatible JsonConverter found for {3} {4}", webServiceParam.getFullName(), routeContext.getWebServiceDefinition().getVerb(), routeContext.getWebServiceDefinition().getPath(), webServiceParam.getParamType(), webServiceParam.getType());
// -----
final Object converterSource = jsonReaderToApply.extractData(request, webServiceParam, routeContext);
if (converterSource != null) {
// On ne convertit pas les null
jsonConverterToApply.populateWebServiceCallContext(converterSource, webServiceParam, routeContext);
} else if (webServiceParam.isOptional()) {
routeContext.setParamValue(webServiceParam, null);
}
Assertion.checkNotNull(routeContext.getParamValue(webServiceParam), "RestParam not found : {0}", webServiceParam);
} catch (final JsonSyntaxException e) {
throw new JsonSyntaxException("Error parsing param " + webServiceParam.getFullName() + " on service " + routeContext.getWebServiceDefinition().getVerb() + " " + routeContext.getWebServiceDefinition().getPath(), e);
}
}
Aggregations