Example usage for com.google.gson JsonElement isJsonNull

List of usage examples for com.google.gson JsonElement isJsonNull

Introduction

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

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

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");
    }//from w  ww  .  ja  v a  2  s  .  c  om

    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 ww  .j a va2s . com*/

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

From source file:org.openhab.io.transport.modbus.json.WriteRequestJsonUtilities.java

License:Open Source License

private static ModbusWriteRequestBlueprint constructBluerint(int unitId, @Nullable JsonElement functionCodeElem,
        @Nullable JsonElement addressElem, @Nullable JsonElement maxTriesElem, @Nullable JsonArray valuesElem) {
    int functionCodeNumeric;
    if (functionCodeElem == null || functionCodeElem.isJsonNull()) {
        throw new IllegalStateException(String.format("Value for '%s' is invalid", JSON_FUNCTION_CODE));
    }/*from   w  w  w .  j a  v a  2  s  .  c  om*/
    try {
        functionCodeNumeric = functionCodeElem.getAsInt();
    } catch (ClassCastException | IllegalStateException e) {
        throw new IllegalStateException(String.format("Value for '%s' is invalid", JSON_FUNCTION_CODE), e);
    }
    ModbusWriteFunctionCode functionCode = ModbusWriteFunctionCode.fromFunctionCode(functionCodeNumeric);
    int address;
    if (addressElem == null || addressElem.isJsonNull()) {
        throw new IllegalStateException(String.format("Value for '%s' is invalid", JSON_ADDRESS));
    }
    try {
        address = addressElem.getAsInt();
    } catch (ClassCastException | IllegalStateException e) {
        throw new IllegalStateException(String.format("Value for '%s' is invalid", JSON_ADDRESS), e);
    }
    int maxTries;
    if (maxTriesElem == null || maxTriesElem.isJsonNull()) {
        // Go with default
        maxTries = DEFAULT_MAX_TRIES;
    } else {
        try {
            maxTries = maxTriesElem.getAsInt();
        } catch (ClassCastException | IllegalStateException e) {
            throw new IllegalStateException(String.format("Value for '%s' is invalid", JSON_MAX_TRIES), e);
        }
    }

    if (valuesElem == null || valuesElem.isJsonNull()) {
        throw new IllegalArgumentException(String.format("Expecting non-null value, got: %s", valuesElem));
    }

    AtomicBoolean writeSingle = new AtomicBoolean(false);
    switch (functionCode) {
    case WRITE_COIL:
        writeSingle.set(true);
        if (valuesElem.size() != 1) {
            throw new IllegalArgumentException(String
                    .format("Expecting single value with functionCode=%s, got: %d", functionCode, valuesElem));
        }
        // fall-through to WRITE_MULTIPLE_COILS
    case WRITE_MULTIPLE_COILS:
        if (valuesElem.size() == 0) {
            throw new IllegalArgumentException("Must provide at least one coil");
        }
        BasicBitArray bits = new BasicBitArray(valuesElem.size());
        for (int i = 0; i < valuesElem.size(); i++) {
            bits.setBit(i, valuesElem.get(i).getAsInt() != 0);
        }
        return new BasicModbusWriteCoilRequestBlueprint(unitId, address, bits, !writeSingle.get(), maxTries);
    case WRITE_SINGLE_REGISTER:
        writeSingle.set(true);
        if (valuesElem.size() != 1) {
            throw new IllegalArgumentException(String
                    .format("Expecting single value with functionCode=%s, got: %d", functionCode, valuesElem));
        }
        // fall-through to WRITE_MULTIPLE_REGISTERS
    case WRITE_MULTIPLE_REGISTERS: {
        ModbusRegister[] registers = new ModbusRegister[valuesElem.size()];
        if (registers.length == 0) {
            throw new IllegalArgumentException("Must provide at least one register");
        }
        for (int i = 0; i < valuesElem.size(); i++) {
            registers[i] = new BasicModbusRegister(valuesElem.get(i).getAsInt());
        }
        return new BasicModbusWriteRegisterRequestBlueprint(unitId, address,
                new BasicModbusRegisterArray(registers), !writeSingle.get(), maxTries);
    }
    default:
        throw new IllegalArgumentException("Unknown function code");
    }
}

From source file:org.openmrs.mobile.utilities.ObservationDeserializer.java

License:Open Source License

@Override
public Observation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonObject = json.getAsJsonObject();

    Observation observation = new Observation();
    observation.setUuid(jsonObject.get(UUID_KEY).getAsString());
    if (jsonObject.get(DISPLAY_KEY) != JsonNull.INSTANCE && jsonObject.get(DISPLAY_KEY) != null) {
        observation.setDisplay(jsonObject.get(DISPLAY_KEY).getAsString());
    }//w  w  w .  j  a va  2  s .c  o  m

    if (jsonObject.get(COMMENT_KEY) != JsonNull.INSTANCE && jsonObject.get(COMMENT_KEY) != null) {
        observation.setComment(jsonObject.get(COMMENT_KEY).getAsString());
    }

    if (jsonObject.get(DATE_KEY) != JsonNull.INSTANCE && jsonObject.get(DATE_KEY) != null) {
        observation.setObsDatetime(jsonObject.get(DATE_KEY).getAsString());
    }

    JsonElement encounterJson = jsonObject.get("encounter");
    if (null != encounterJson && !encounterJson.isJsonNull()) {
        Encounter encounter = new Encounter();
        encounter.setUuid(encounterJson.getAsJsonObject().get(UUID_KEY).getAsString());

        Visit visit = new Visit();
        JsonElement visitElement = encounterJson.getAsJsonObject().get("visit");
        if (null != visitElement && !visitElement.isJsonNull()) {
            visit.setUuid(visitElement.getAsJsonObject().get(UUID_KEY).getAsString());
        }

        encounter.setVisit(visit);
        observation.setEncounter(encounter);
    }

    JsonElement conceptJson = jsonObject.get("concept");

    if (conceptJson != null
            && "Visit Diagnoses".equals(conceptJson.getAsJsonObject().get(DISPLAY_KEY).getAsString())) {
        JsonArray diagnosisDetailJSONArray = jsonObject.get("groupMembers").getAsJsonArray();
        for (int i = 0; i < diagnosisDetailJSONArray.size(); i++) {

            JsonObject diagnosisDetails = diagnosisDetailJSONArray.get(i).getAsJsonObject();
            String diagnosisDetail = diagnosisDetails.get("concept").getAsJsonObject().get(DISPLAY_KEY)
                    .getAsString();

            if ("Diagnosis order".equals(diagnosisDetail)) {
                observation.setDiagnosisOrder(diagnosisDetails.getAsJsonObject().get(VALUE_KEY)
                        .getAsJsonObject().get(DISPLAY_KEY).getAsString());
            } else if ("Diagnosis certainty".equals(diagnosisDetail)) {
                observation.setDiagnosisCertanity(diagnosisDetails.getAsJsonObject().get(VALUE_KEY)
                        .getAsJsonObject().get(DISPLAY_KEY).getAsString());
            } else {
                try {
                    observation.setDiagnosisList(diagnosisDetails.getAsJsonObject().get(VALUE_KEY)
                            .getAsJsonObject().get(DISPLAY_KEY).getAsString());
                    observation.setValueCodedName(diagnosisDetails.getAsJsonObject().get(VALUE_KEY)
                            .getAsJsonObject().get(UUID_KEY).getAsString());
                } catch (IllegalStateException e) {
                    observation
                            .setDiagnosisList(diagnosisDetails.getAsJsonObject().get(VALUE_KEY).getAsString());
                }
            }
        }
    } else if (conceptJson != null
            && "Text of encounter note".equals(conceptJson.getAsJsonObject().get(DISPLAY_KEY).getAsString())) {
        JsonElement encounterNote = jsonObject.getAsJsonObject().get(VALUE_KEY);
        observation.setDiagnosisNote(!encounterNote.isJsonNull() ? encounterNote.getAsString() : "");
    }
    if (conceptJson != null) {
        Concept concept = new Concept();
        concept.setUuid(conceptJson.getAsJsonObject().get(UUID_KEY).getAsString());
        observation.setConcept(concept);
    }

    JsonElement personJson = jsonObject.get("person");
    if (personJson != null) {
        Person person = new Person();
        person.setUuid(personJson.getAsJsonObject().get(UUID_KEY).getAsString());
        person.setDisplay(personJson.getAsJsonObject().get(DISPLAY_KEY).getAsString());
        observation.setPerson(person);
    }

    JsonElement creatorJson = jsonObject.get("creator");
    if (creatorJson != null) {
        User user = new User();
        user.setUuid(creatorJson.getAsJsonObject().get(UUID_KEY).getAsString());
        user.setDisplay(creatorJson.getAsJsonObject().get(DISPLAY_KEY).getAsString());

        JsonElement creatorPersonJson = creatorJson.getAsJsonObject().get("person");
        if (creatorPersonJson != null) {
            Person person = new Person();
            person.setUuid(creatorPersonJson.getAsJsonObject().get(UUID_KEY).getAsString());
            person.setDisplay(creatorPersonJson.getAsJsonObject().get(DISPLAY_KEY).getAsString());
            user.setPerson(person);
        }

        observation.setCreator(user);
    }

    JsonElement dateCreatedJson = jsonObject.get("dateCreated");
    if (dateCreatedJson != null) {
        observation.setDateCreated(DateUtils.convertTimeString(dateCreatedJson.getAsString()).toDate());
    }

    JsonElement voidedJson = jsonObject.get("voided");
    if (voidedJson != null) {
        observation.setVoided(Boolean.getBoolean(voidedJson.getAsString()));
    }

    return observation;
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public Record getRecord(ValueType valueType) throws ClassCastException {

    Record record = null;//from w  ww.j  av  a  2s. c  o m
    JsonElement jse = jsonObject.get(Const.RECORD);

    if (!jse.isJsonNull()) {
        record = getRecord(gson.fromJson(jse, RestRecord.class), valueType);
    }
    return record;
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public Value getValue(ValueType valueType) throws ClassCastException {

    Value value = null;//  ww w  . ja  v a2s . com
    JsonElement jse = jsonObject.get(Const.RECORD);

    if (!jse.isJsonNull()) {
        Record record = getRecord(valueType);
        if (record != null) {
            value = record.getValue();
        }
    }
    return value;
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public DeviceState getDeviceState() {

    DeviceState ret = null;/*from  w ww. j  a v a 2  s  . co m*/
    JsonElement jse = jsonObject.get(Const.STATE);

    if (!jse.isJsonNull()) {
        ret = gson.fromJson(jse, DeviceState.class);
    }
    return ret;
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public void setChannelConfig(ChannelConfig channelConfig, String id) throws JsonSyntaxException,
        IdCollisionException, RestConfigIsNotCorrectException, MissingJsonObjectException {

    JsonElement jse = jsonObject.get(Const.CONFIGS);

    if (!jse.isJsonNull()) {
        RestChannelConfigMapper.setChannelConfig(channelConfig, gson.fromJson(jse, RestChannelConfig.class),
                id);//from  w  w w.  jav  a  2s.c o  m
    } else {
        throw new MissingJsonObjectException();
    }
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public void setDeviceConfig(DeviceConfig deviceConfig, String id) throws JsonSyntaxException,
        IdCollisionException, RestConfigIsNotCorrectException, MissingJsonObjectException {

    JsonElement jse = jsonObject.get(Const.CONFIGS);

    if (!jse.isJsonNull()) {
        RestDeviceConfigMapper.setDeviceConfig(deviceConfig, gson.fromJson(jse, RestDeviceConfig.class), id);
    } else {//from  w w  w  .j  a  v  a 2s.c  om
        throw new MissingJsonObjectException();
    }
}

From source file:org.openmuc.framework.lib.json.FromJson.java

License:Open Source License

public void setDriverConfig(DriverConfig driverConfig, String id) throws JsonSyntaxException,
        IdCollisionException, RestConfigIsNotCorrectException, MissingJsonObjectException {

    JsonElement jse = jsonObject.get(Const.CONFIGS);

    if (!jse.isJsonNull()) {
        RestDriverConfigMapper.setDriverConfig(driverConfig, gson.fromJson(jse, RestDriverConfig.class), id);
    } else {//from w w  w  .java  2s .c om
        throw new MissingJsonObjectException();
    }
}