Example usage for com.google.gson JsonParseException JsonParseException

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

Introduction

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

Prototype

public JsonParseException(Throwable cause) 

Source Link

Document

Creates exception with the specified cause.

Usage

From source file:org.openhab.io.neeo.internal.serialization.ItemSubTypeSerializer.java

License:Open Source License

@Override
public ItemSubType deserialize(@Nullable JsonElement elm, @Nullable Type type,
        @Nullable JsonDeserializationContext jsonContext) throws JsonParseException {
    Objects.requireNonNull(elm, "elm cannot be null");
    Objects.requireNonNull(type, "type cannot be null");
    Objects.requireNonNull(jsonContext, "jsonContext cannot be null");

    if (elm.isJsonNull()) {
        throw new JsonParseException("ItemSubType could not be parsed from null");
    }//  www .  j  av  a 2  s.  c  o  m

    return ItemSubType.parse(elm.getAsString());
}

From source file:org.openhab.io.neeo.internal.serialization.ListUiActionSerializer.java

License:Open Source License

@Override
public ListUiAction deserialize(@Nullable JsonElement elm, @Nullable Type type,
        @Nullable JsonDeserializationContext jsonContext) throws JsonParseException {
    Objects.requireNonNull(elm, "elm cannot be null");
    Objects.requireNonNull(type, "type cannot be null");
    Objects.requireNonNull(jsonContext, "jsonContext cannot be null");

    if (elm.isJsonNull()) {
        throw new JsonParseException("ListUiAction could not be parsed from null");
    }/*  ww w  . java 2s  .co  m*/

    return ListUiAction.parse(elm.getAsString());
}

From source file:org.openhab.io.neeo.internal.serialization.NeeoCapabilityTypeSerializer.java

License:Open Source License

@Override
public NeeoCapabilityType deserialize(@Nullable JsonElement elm, @Nullable Type type,
        @Nullable JsonDeserializationContext jsonContext) throws JsonParseException {
    Objects.requireNonNull(elm, "elm cannot be null");
    Objects.requireNonNull(type, "type cannot be null");
    Objects.requireNonNull(jsonContext, "jsonContext cannot be null");

    if (elm.isJsonNull()) {
        throw new JsonParseException("NeeoCapabilityType could not be parsed from null");
    }/* w  w  w  .jav a2s  . co m*/

    return NeeoCapabilityType.parse(elm.getAsString());
}

From source file:org.openhab.io.neeo.internal.serialization.NeeoDeviceChannelKindSerializer.java

License:Open Source License

@Override
public NeeoDeviceChannelKind deserialize(@Nullable JsonElement elm, @Nullable Type type,
        @Nullable JsonDeserializationContext jsonContext) throws JsonParseException {
    Objects.requireNonNull(elm, "elm cannot be null");
    Objects.requireNonNull(type, "type cannot be null");
    Objects.requireNonNull(jsonContext, "jsonContext cannot be null");

    if (elm.isJsonNull()) {
        throw new JsonParseException("NeeoDeviceChannelKind could not be parsed from null");
    }/*from w ww.j  a  v a  2 s .  c om*/

    return NeeoDeviceChannelKind.parse(elm.getAsString());
}

From source file:org.openhab.io.neeo.internal.serialization.NeeoDeviceChannelSerializer.java

License:Open Source License

@Override
public NeeoDeviceChannel deserialize(@Nullable JsonElement elm, @Nullable Type type,
        @Nullable JsonDeserializationContext context) throws JsonParseException {
    Objects.requireNonNull(elm, "elm cannot be null");
    Objects.requireNonNull(type, "type cannot be null");
    Objects.requireNonNull(context, "context cannot be null");

    if (!(elm instanceof JsonObject)) {
        throw new JsonParseException("Element not an instance of JsonObject: " + elm);
    }// w ww .j ava  2s. c o  m

    final JsonObject jo = (JsonObject) elm;
    final String itemName = NeeoUtil.getString(jo, "itemName");

    if (itemName == null || StringUtils.isEmpty(itemName)) {
        throw new JsonParseException("Element requires an itemName attribute: " + elm);
    }

    final ItemSubType itemSubType = jo.has("subType")
            ? context.deserialize(jo.get("subType"), ItemSubType.class)
            : ItemSubType.NONE;

    final String label = NeeoUtil.getString(jo, "label");
    final String value = NeeoUtil.getString(jo, "value");
    final Integer channelNbr = NeeoUtil.getInt(jo, "channelNbr");

    if (channelNbr == null) {
        throw new JsonParseException("Channel Number is not a valid integer");
    }
    final NeeoCapabilityType capType = context.deserialize(jo.get("type"), NeeoCapabilityType.class);

    final NeeoDeviceChannelRange range = jo.has("range")
            ? context.deserialize(jo.get("range"), NeeoDeviceChannelRange.class)
            : null;

    final NeeoDeviceChannelKind kind = jo.has("kind")
            ? context.deserialize(jo.get("kind"), NeeoDeviceChannelKind.class)
            : NeeoDeviceChannelKind.ITEM;

    try {
        if (capType == NeeoCapabilityType.TEXTLABEL) {
            final boolean labelVisible = jo.has("labelVisible") ? jo.get("labelVisible").getAsBoolean() : true;

            return new NeeoDeviceChannelText(kind, itemName, channelNbr, capType, itemSubType,
                    label == null || StringUtils.isEmpty(label) ? NeeoUtil.NOTAVAILABLE : label, value, range,
                    labelVisible);
        } else if (capType == NeeoCapabilityType.DIRECTORY) {
            final NeeoDeviceChannelDirectoryListItem[] listItems = jo.has("listItems")
                    ? context.deserialize(jo.get("listItems"), NeeoDeviceChannelDirectoryListItem[].class)
                    : new NeeoDeviceChannelDirectoryListItem[0];

            return new NeeoDeviceChannelDirectory(kind, itemName, channelNbr, capType, itemSubType,
                    label == null || StringUtils.isEmpty(label) ? NeeoUtil.NOTAVAILABLE : label, value, range,
                    listItems);
        } else {
            return new NeeoDeviceChannel(kind, itemName, channelNbr, capType, itemSubType,
                    label == null || StringUtils.isEmpty(label) ? NeeoUtil.NOTAVAILABLE : label, value, range);
        }
    } catch (NullPointerException | IllegalArgumentException e) {
        throw new JsonParseException(e);
    }
}

From source file:org.openhab.io.neeo.internal.serialization.NeeoDeviceSerializer.java

License:Open Source License

@Override
public NeeoDevice deserialize(@Nullable JsonElement elm, @Nullable Type type,
        @Nullable JsonDeserializationContext jsonContext) throws JsonParseException {
    Objects.requireNonNull(elm, "elm cannot be null");
    Objects.requireNonNull(type, "type cannot be null");
    Objects.requireNonNull(jsonContext, "jsonContext cannot be null");

    if (!(elm instanceof JsonObject)) {
        throw new JsonParseException("Element not an instance of JsonObject: " + elm);
    }/*from w w  w  .  j  a  v a 2  s. c o  m*/

    final JsonObject jo = (JsonObject) elm;
    final NeeoThingUID uid = jsonContext.deserialize(jo.get("uid"), NeeoThingUID.class);
    final NeeoDeviceType devType = jsonContext.deserialize(jo.get("type"), NeeoDeviceType.class);
    final String manufacturer = NeeoUtil.getString(jo, "manufacturer");
    final String name = NeeoUtil.getString(jo, "name");
    final NeeoDeviceChannel[] channels = jsonContext.deserialize(jo.get("channels"), NeeoDeviceChannel[].class);
    final NeeoDeviceTiming timing = jo.has("timing")
            ? jsonContext.deserialize(jo.get("timing"), NeeoDeviceTiming.class)
            : null;

    final String[] deviceCapabilities = jo.has("deviceCapabilities")
            ? jsonContext.deserialize(jo.get("deviceCapabilities"), String[].class)
            : null;

    final String specificName = jo.has("specificName") ? jo.get("specificName").getAsString() : null;

    final String iconName = jo.has("iconName") ? jo.get("iconName").getAsString() : null;
    final int driverVersion = jo.has("driverVersion") ? jo.get("driverVersion").getAsInt() : 0;

    try {
        return new NeeoDevice(uid, driverVersion, devType,
                manufacturer == null || StringUtils.isEmpty(manufacturer) ? NeeoUtil.NOTAVAILABLE
                        : manufacturer,
                name, Arrays.asList(channels), timing,
                deviceCapabilities == null ? null : Arrays.asList(deviceCapabilities), specificName, iconName);
    } catch (NullPointerException | IllegalArgumentException e) {
        throw new JsonParseException(e);
    }
}

From source file:org.openhab.io.neeo.internal.serialization.NeeoDeviceTypeSerializer.java

License:Open Source License

@Override
public NeeoDeviceType deserialize(@Nullable JsonElement elm, @Nullable Type type,
        @Nullable JsonDeserializationContext jsonContext) throws JsonParseException {
    Objects.requireNonNull(elm, "elm cannot be null");
    Objects.requireNonNull(type, "type cannot be null");
    Objects.requireNonNull(jsonContext, "jsonContext cannot be null");

    if (elm.isJsonNull()) {
        throw new JsonParseException("NeeoDeviceType could not be parsed from null");
    }//  w  ww.j  a v  a 2 s.c o m

    return NeeoDeviceType.parse(elm.getAsString());
}

From source file:org.openhab.io.neeo.internal.serialization.NeeoThingUIDSerializer.java

License:Open Source License

@Override
public NeeoThingUID deserialize(@Nullable JsonElement elm, @Nullable Type type,
        @Nullable JsonDeserializationContext jsonContext) throws JsonParseException {
    Objects.requireNonNull(elm, "elm cannot be null");
    Objects.requireNonNull(type, "type cannot be null");
    Objects.requireNonNull(jsonContext, "jsonContext cannot be null");

    if (elm.isJsonNull()) {
        throw new JsonParseException("Not a valid ChannelUID: (null)");
    }//from   w w w  . j av  a 2  s.  c  o m

    try {
        return new NeeoThingUID(elm.getAsString());
    } catch (IllegalArgumentException e) {
        throw new JsonParseException("Not a valid thingUID: " + elm.getAsString(), e);
    }
}

From source file:org.openlmis.core.network.adapter.RnrFormAdapter.java

License:Open Source License

@Override
public JsonElement serialize(RnRForm rnRForm, Type typeOfSrc, JsonSerializationContext context) {
    try {/*  w w w .  j  a v a 2s.c  om*/
        return buildRnrFormJson(rnRForm);
    } catch (LMISException e) {
        throw new JsonParseException("can not find Signature by rnrForm");
    } catch (NullPointerException e) {
        throw new JsonParseException("No Program associated !");
    }
}

From source file:org.openlmis.core.network.adapter.RnrFormAdapter.java

License:Open Source License

@Override
public RnRForm deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    RnRForm rnRForm = gson.fromJson(json.toString(), RnRForm.class);
    RnRForm.fillFormId(rnRForm);/* w  ww .  ja  v  a 2 s .c o m*/

    try {
        Program program = programRepository
                .queryByCode(json.getAsJsonObject().get("programCode").getAsString());
        rnRForm.setProgram(program);
    } catch (LMISException e) {
        e.reportToFabric();
        throw new JsonParseException("can not find Program by programCode");
    }
    rnRForm.setStatus(RnRForm.STATUS.AUTHORIZED);
    rnRForm.setSynced(true);

    return rnRForm;
}