Search in sources :

Example 51 with JsonSyntaxException

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);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) JsonSyntaxException(com.google.gson.JsonSyntaxException) MessageHeaders(org.springframework.messaging.MessageHeaders)

Example 52 with JsonSyntaxException

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);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson)

Example 53 with JsonSyntaxException

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);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson)

Example 54 with JsonSyntaxException

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());
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) NotImplementedException(io.openems.common.exceptions.NotImplementedException) OpenemsException(io.openems.common.exceptions.OpenemsException) JsonParser(com.google.gson.JsonParser)

Example 55 with JsonSyntaxException

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);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) PrimitiveJsonConverter(io.vertigo.vega.plugins.webservice.handler.converter.PrimitiveJsonConverter) VFileJsonConverter(io.vertigo.vega.plugins.webservice.handler.converter.VFileJsonConverter) JsonConverter(io.vertigo.vega.plugins.webservice.handler.converter.JsonConverter) DtListJsonConverter(io.vertigo.vega.plugins.webservice.handler.converter.DtListJsonConverter) DtObjectJsonConverter(io.vertigo.vega.plugins.webservice.handler.converter.DtObjectJsonConverter) ImplicitJsonConverter(io.vertigo.vega.plugins.webservice.handler.converter.ImplicitJsonConverter) DefaultJsonConverter(io.vertigo.vega.plugins.webservice.handler.converter.DefaultJsonConverter) DtListDeltaJsonConverter(io.vertigo.vega.plugins.webservice.handler.converter.DtListDeltaJsonConverter) PathJsonReader(io.vertigo.vega.plugins.webservice.handler.reader.PathJsonReader) QueryJsonReader(io.vertigo.vega.plugins.webservice.handler.reader.QueryJsonReader) RequestJsonReader(io.vertigo.vega.plugins.webservice.handler.reader.RequestJsonReader) HeaderJsonReader(io.vertigo.vega.plugins.webservice.handler.reader.HeaderJsonReader) BodyJsonReader(io.vertigo.vega.plugins.webservice.handler.reader.BodyJsonReader) JsonReader(io.vertigo.vega.plugins.webservice.handler.reader.JsonReader) InnerBodyJsonReader(io.vertigo.vega.plugins.webservice.handler.reader.InnerBodyJsonReader)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)379 Gson (com.google.gson.Gson)169 IOException (java.io.IOException)83 HashMap (java.util.HashMap)68 JsonElement (com.google.gson.JsonElement)62 JsonObject (com.google.gson.JsonObject)58 ArrayList (java.util.ArrayList)46 JsonParser (com.google.gson.JsonParser)42 GsonBuilder (com.google.gson.GsonBuilder)38 Cursor (android.database.Cursor)33 InputStreamReader (java.io.InputStreamReader)30 Map (java.util.Map)30 BadRequestException (co.cask.cdap.common.BadRequestException)28 JsonArray (com.google.gson.JsonArray)28 Path (javax.ws.rs.Path)28 Reader (java.io.Reader)26 Type (java.lang.reflect.Type)23 JsonIOException (com.google.gson.JsonIOException)21 FileReader (java.io.FileReader)21 File (java.io.File)19