Example usage for com.google.gson JsonSyntaxException JsonSyntaxException

List of usage examples for com.google.gson JsonSyntaxException JsonSyntaxException

Introduction

In this page you can find the example usage for com.google.gson JsonSyntaxException JsonSyntaxException.

Prototype

public JsonSyntaxException(Throwable cause) 

Source Link

Document

Creates exception with the specified cause.

Usage

From source file:org.thingsboard.server.transport.mqtt.session.GatewaySessionHandler.java

License:Apache License

public void onDeviceAttributes(MqttPublishMessage mqttMsg) throws AdaptorException {
    JsonElement json = JsonMqttAdaptor.validateJsonPayload(sessionId, mqttMsg.payload());
    int msgId = mqttMsg.variableHeader().packetId();
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        for (Map.Entry<String, JsonElement> deviceEntry : jsonObj.entrySet()) {
            String deviceName = deviceEntry.getKey();
            Futures.addCallback(checkDeviceConnected(deviceName),
                    new FutureCallback<GatewayDeviceSessionCtx>() {
                        @Override
                        public void onSuccess(@Nullable GatewayDeviceSessionCtx deviceCtx) {
                            if (!deviceEntry.getValue().isJsonObject()) {
                                throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
                            }/*w  w  w.j  av  a 2  s .  com*/
                            TransportProtos.PostAttributeMsg postAttributeMsg = JsonConverter
                                    .convertToAttributesProto(deviceEntry.getValue().getAsJsonObject());
                            transportService.process(deviceCtx.getSessionInfo(), postAttributeMsg,
                                    getPubAckCallback(channel, deviceName, msgId, postAttributeMsg));
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            log.debug("[{}] Failed to process device attributes command: {}", sessionId,
                                    deviceName, t);
                        }
                    }, context.getExecutor());
        }
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}

From source file:org.thingsboard.server.transport.mqtt.session.GatewaySessionHandler.java

License:Apache License

public void onDeviceRpcResponse(MqttPublishMessage mqttMsg) throws AdaptorException {
    JsonElement json = JsonMqttAdaptor.validateJsonPayload(sessionId, mqttMsg.payload());
    int msgId = mqttMsg.variableHeader().packetId();
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        String deviceName = jsonObj.get(DEVICE_PROPERTY).getAsString();
        Futures.addCallback(checkDeviceConnected(deviceName), new FutureCallback<GatewayDeviceSessionCtx>() {
            @Override/*www  . j  ava2  s.  c  o m*/
            public void onSuccess(@Nullable GatewayDeviceSessionCtx deviceCtx) {
                Integer requestId = jsonObj.get("id").getAsInt();
                String data = jsonObj.get("data").toString();
                TransportProtos.ToDeviceRpcResponseMsg rpcResponseMsg = TransportProtos.ToDeviceRpcResponseMsg
                        .newBuilder().setRequestId(requestId).setPayload(data).build();
                transportService.process(deviceCtx.getSessionInfo(), rpcResponseMsg,
                        getPubAckCallback(channel, deviceName, msgId, rpcResponseMsg));
            }

            @Override
            public void onFailure(Throwable t) {
                log.debug("[{}] Failed to process device teleemtry command: {}", sessionId, deviceName, t);
            }
        }, context.getExecutor());
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}

From source file:org.thingsboard.server.transport.mqtt.session.GatewaySessionHandler.java

License:Apache License

public void onDeviceAttributesRequest(MqttPublishMessage msg) throws AdaptorException {
    JsonElement json = JsonMqttAdaptor.validateJsonPayload(sessionId, msg.payload());
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        int requestId = jsonObj.get("id").getAsInt();
        String deviceName = jsonObj.get(DEVICE_PROPERTY).getAsString();
        boolean clientScope = jsonObj.get("client").getAsBoolean();
        Set<String> keys;
        if (jsonObj.has("key")) {
            keys = Collections.singleton(jsonObj.get("key").getAsString());
        } else {//from  w  ww .  ja  v a  2  s  . c o m
            JsonArray keysArray = jsonObj.get("keys").getAsJsonArray();
            keys = new HashSet<>();
            for (JsonElement keyObj : keysArray) {
                keys.add(keyObj.getAsString());
            }
        }
        TransportProtos.GetAttributeRequestMsg.Builder result = TransportProtos.GetAttributeRequestMsg
                .newBuilder();
        result.setRequestId(requestId);

        if (clientScope) {
            result.addAllClientAttributeNames(keys);
        } else {
            result.addAllSharedAttributeNames(keys);
        }
        TransportProtos.GetAttributeRequestMsg requestMsg = result.build();
        int msgId = msg.variableHeader().packetId();
        Futures.addCallback(checkDeviceConnected(deviceName), new FutureCallback<GatewayDeviceSessionCtx>() {
            @Override
            public void onSuccess(@Nullable GatewayDeviceSessionCtx deviceCtx) {
                transportService.process(deviceCtx.getSessionInfo(), requestMsg,
                        getPubAckCallback(channel, deviceName, msgId, requestMsg));
            }

            @Override
            public void onFailure(Throwable t) {
                log.debug("[{}] Failed to process device attributes request command: {}", sessionId, deviceName,
                        t);
            }
        }, context.getExecutor());
        ack(msg);
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}

From source file:prototype.pa55nyaps.gui.PA55NYAPSApp.java

License:Apache License

private boolean readDatabaseFromFile(File file, String password) {
    boolean retVal = false;
    Scanner is;/* w  w  w.  j a  va 2  s  .com*/
    StringBuilder jsonct = new StringBuilder();
    try {
        is = new Scanner(file);
        while (is.hasNextLine()) {
            jsonct.append(is.nextLine());
        }
        is.close();
        Type ctType = new TypeToken<Ciphertext>() {
        }.getType();
        Ciphertext ciphertext = gson.fromJson(jsonct.toString(), ctType);
        String json = AESCryptosystem.getInstance().decryptWithHmac(ciphertext, password);
        Type dbType = new TypeToken<PasswordDatabase>() {
        }.getType();
        passwordDatabase = gson.fromJson(json.toString(), dbType);
        if (passwordDatabase == null) {
            throw new JsonSyntaxException(
                    file.getName() + " contains a password database that cannot be deserialized.");
        }
        this.primaryStage.setTitle(APP_NAME + " - " + file.getName());
        retVal = true;
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        showAlert(AlertType.ERROR, "File open error", ex.getClass().getName(), ex.getLocalizedMessage());
    }
    return retVal;
}

From source file:prototype.pa55nyaps.gui.PA55NYAPSApp.java

License:Apache License

public void saveDatabaseToFile(PasswordDatabase database, File file, String password, boolean silent) {
    PrintStream pos;/*w  ww .  ja v  a2s.  c o m*/
    try {
        String json = gson.toJson(database);
        //System.out.println(json);
        if (json == null) {
            throw new JsonSyntaxException("The password database cannot be serialized.");
        }
        Ciphertext ciphertext = AESCryptosystem.getInstance().encryptWithHmac(json, new String(password));
        String jsonct = gson.toJson(ciphertext);
        pos = new PrintStream(file.getAbsolutePath());
        pos.print(jsonct);
        pos.flush();
        pos.close();
        this.primaryStage.setTitle(APP_NAME + " - " + file.getName());
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        if (!silent) {
            showAlert(AlertType.ERROR, "File save error", ex.getClass().getName(), ex.getLocalizedMessage());
        }
    }
}

From source file:se.billes.pdf.json.BlockTypeSelector.java

License:Open Source License

public Gson createGson() {

    GsonFireBuilder builder = new GsonFireBuilder()
            .registerTypeSelector(AbstractPhrase.class, new TypeSelector<AbstractPhrase>() {
                @Override/*w w  w .ja  v  a2s  . com*/
                public Class<? extends AbstractPhrase> getClassForElement(JsonElement readElement) {
                    JsonElement element = readElement.getAsJsonObject().get("dotted");
                    if (element != null) {
                        return DottedFillPhrase.class;
                    }

                    return Phrase.class;
                }

            }).registerTypeSelector(AbstractParagraph.class, new TypeSelector<AbstractParagraph>() {
                @Override
                public Class<? extends AbstractParagraph> getClassForElement(JsonElement readElement) {
                    JsonElement element = readElement.getAsJsonObject().get("cells");
                    if (element != null) {
                        return TableParagraph.class;
                    }

                    element = readElement.getAsJsonObject().get("padding");
                    if (element != null) {
                        return TableCell.class;
                    }

                    element = readElement.getAsJsonObject().get("fill");
                    if (element != null) {
                        return FillParagraph.class;
                    }

                    return Paragraph.class;
                }

            }).registerTypeSelector(BaseElement.class, new TypeSelector<BaseElement>() {
                @Override
                public Class<? extends BaseElement> getClassForElement(JsonElement readElement) {
                    JsonElement element = readElement.getAsJsonObject().get("type");
                    if (element == null) {
                        throw new JsonSyntaxException("You must have a type in block");
                    }
                    String kind = element.getAsString();
                    if (kind.equals("LINE")) {
                        return Line.class;
                    } else if (kind.equals("BLOCK")) {
                        return Block.class;
                    } else if (kind.equals("IMAGE")) {
                        return Image.class;
                    } else if (kind.equals("QR")) {
                        return QRCode.class;
                    } else if (kind.equals("BARCODE")) {
                        return Barcode.class;
                    } else {
                        return null; //returning null will trigger Gson's default behavior
                    }
                }
            });

    return builder.createGson();

}

From source file:ts.resources.jsonconfig.TsconfigJson.java

License:Open Source License

public static <T extends TsconfigJson> T load(Reader json, Class<T> classOfT) {
    Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
    T o = gson.fromJson(json, classOfT);
    if (o == null) {
        throw new JsonSyntaxException("JSON Syntax error");
    }/*from   w  ww .  ja v a2  s  . c  o  m*/
    return o;
}