Example usage for com.google.gson JsonElement getAsDouble

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

Introduction

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

Prototype

public double getAsDouble() 

Source Link

Document

convenience method to get this element as a primitive double value.

Usage

From source file:brooklyn.event.feed.http.JsonFunctions.java

License:Apache License

@SuppressWarnings("unchecked")
public static <T> Function<JsonElement, T> cast(final Class<T> expected) {
    return new Function<JsonElement, T>() {
        @Override/*from   ww w . j  av  a  2  s  .  c o m*/
        public T apply(JsonElement input) {
            if (input == null) {
                return (T) null;
            } else if (input.isJsonNull()) {
                return (T) null;
            } else if (expected == boolean.class || expected == Boolean.class) {
                return (T) (Boolean) input.getAsBoolean();
            } else if (expected == char.class || expected == Character.class) {
                return (T) (Character) input.getAsCharacter();
            } else if (expected == byte.class || expected == Byte.class) {
                return (T) (Byte) input.getAsByte();
            } else if (expected == short.class || expected == Short.class) {
                return (T) (Short) input.getAsShort();
            } else if (expected == int.class || expected == Integer.class) {
                return (T) (Integer) input.getAsInt();
            } else if (expected == long.class || expected == Long.class) {
                return (T) (Long) input.getAsLong();
            } else if (expected == float.class || expected == Float.class) {
                return (T) (Float) input.getAsFloat();
            } else if (expected == double.class || expected == Double.class) {
                return (T) (Double) input.getAsDouble();
            } else if (expected == BigDecimal.class) {
                return (T) input.getAsBigDecimal();
            } else if (expected == BigInteger.class) {
                return (T) input.getAsBigInteger();
            } else if (Number.class.isAssignableFrom(expected)) {
                // TODO Will result in a class-cast if it's an unexpected sub-type of Number not handled above
                return (T) input.getAsNumber();
            } else if (expected == String.class) {
                return (T) input.getAsString();
            } else if (expected.isArray()) {
                JsonArray array = input.getAsJsonArray();
                Class<?> componentType = expected.getComponentType();
                if (JsonElement.class.isAssignableFrom(componentType)) {
                    JsonElement[] result = new JsonElement[array.size()];
                    for (int i = 0; i < array.size(); i++) {
                        result[i] = array.get(i);
                    }
                    return (T) result;
                } else {
                    Object[] result = (Object[]) Array.newInstance(componentType, array.size());
                    for (int i = 0; i < array.size(); i++) {
                        result[i] = cast(componentType).apply(array.get(i));
                    }
                    return (T) result;
                }
            } else {
                throw new IllegalArgumentException("Cannot cast json element to type " + expected);
            }
        }
    };
}

From source file:ca.ualberta.cs.unter.util.GeoPointConverter.java

License:Apache License

@Override
public GeoPoint deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    final JsonArray array = json.getAsJsonArray();
    final JsonElement lonElement = array.get(0);
    final JsonElement latElement = array.get(1);
    final Double lon = lonElement.getAsDouble();
    final Double lat = latElement.getAsDouble();

    return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
}

From source file:ch.cern.db.flume.sink.kite.parser.JSONtoAvroParser.java

License:GNU General Public License

private Object getElementAsType(Schema schema, JsonElement element) {
    if (element == null || element.isJsonNull())
        return null;

    switch (schema.getType()) {
    case BOOLEAN:
        return element.getAsBoolean();
    case DOUBLE:/*from  w  w w. j  a  v a2  s .  c  o m*/
        return element.getAsDouble();
    case FLOAT:
        return element.getAsFloat();
    case INT:
        return element.getAsInt();
    case LONG:
        return element.getAsLong();
    case NULL:
        return null;
    case UNION:
        return getElementAsType(schema.getTypes().get(0), element);
    //      case FIXED:
    //      case ARRAY:
    //      case BYTES:
    //      case ENUM:
    //      case MAP:
    //      case RECORD:
    //      case STRING:
    default:
        return element.getAsString();
    }
}

From source file:com.adr.datasql.orm.KindResultsJson.java

License:Apache License

@Override
public Double getDouble(String columnName) throws DataLinkException {
    JsonElement element = json.get(columnName);
    return element == null || element.isJsonNull() ? null : element.getAsDouble();
}

From source file:com.appdynamics.extensions.couchbase.CouchBaseWrapper.java

License:Apache License

/**
 * Populates an empty map with values retrieved from the entry set of a Json
 * Object//from  w  ww.  j  a v  a2s  .c o m
 * 
 * @param iterator
 *            An entry set iterator for the json object
 * @param metrics
 *            Initially empty map that is populated based on the values
 *            retrieved from entry set
 * @param prefix
 *            Optional prefix for the metric name to distinguish duplicate
 *            metric names
 */
private void populateMetricsMapHelper(Iterator<Entry<String, JsonElement>> iterator,
        Map<String, Double> metrics, String prefix) {
    while (iterator.hasNext()) {
        Entry<String, JsonElement> entry = iterator.next();
        String metricName = (String) entry.getKey();
        JsonElement value = (JsonElement) entry.getValue();
        if (value instanceof JsonPrimitive && NumberUtils.isNumber(value.getAsString())) {
            Double val = value.getAsDouble();
            metrics.put(prefix + metricName, val);
        }
    }
}

From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java

License:Apache License

@Override
public void doubleProperty(PropertyName name, JsonObject context) throws IOException {
    JsonElement prop = property(context, name);
    if (prop == null) {
        return;//w ww  . jav a2  s . com
    }
    builder.add(name, prop.getAsDouble());
}

From source file:com.bios.controller.services.UpdateBidService.java

/**
* Handles the HTTP <code>GET</code> method which updates a bid. Firstly, the token
* is checked for its validity. If the token is valid, the method then goes into the
* respective DAOs to look for the bid. If the bid is valid and can be deleted, the
* bid is then deleted from the DAO and the database. Subsequentyly, a json object
* with the status "success" will be created. If any errors occur such as an
* invalid course or missing token is detected, a json object with the status "error"
* will be created with a json array of the erros that caused the failure to update.
* @param request servlet request//from   w ww .j av  a 2s  . co  m
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ConnectionManager manager = new ConnectionManager();
    String requestObject = request.getParameter("r");
    String token = request.getParameter("token");
    ArrayList<String> errorList = new ArrayList<String>();

    JsonObject reponseObject = new JsonObject();
    JsonArray allErrors = new JsonArray();

    if (requestObject == null) {
        errorList.add("missing r");
    } else if (requestObject.length() == 0) {
        errorList.add("blank r");
    }

    if (token == null) {
        errorList.add("missing token");
    } else if (token.length() == 0) {
        errorList.add("blank token");
    } else {
        try {
            String result = JWTUtility.verify(token, "abcdefgh12345678");
            if (!result.equals("admin")) {
                errorList.add("invalid token");
            }
        } catch (JWTException e) { // do we need this?
            // This means not an admin, or token expired
            errorList.add("invalid username/token");
        }
    }

    JsonElement jelement = new JsonParser().parse(requestObject);
    JsonObject json = jelement.getAsJsonObject();
    Bid existingBid = null;

    JsonElement amt = json.get("amount");
    JsonElement crse = json.get("course");
    JsonElement sec = json.get("section");
    JsonElement user = json.get("userid");

    if (amt == null) {
        errorList.add("missing amount");
    } else if (amt.getAsString().length() == 0) {
        errorList.add("blank amount");
    }

    if (crse == null) {
        errorList.add("missing course");
    } else if (crse.getAsString().length() == 0) {
        errorList.add("blank course");
    }

    if (sec == null) {
        errorList.add("missing section");
    } else if (sec.getAsString().length() == 0) {
        errorList.add("blank section");
    }

    if (user == null) {
        errorList.add("missing userid");
    } else if (user.getAsString().length() == 0) {
        errorList.add("blank userid");
    }

    if (errorList.size() > 0) {
        errorList.sort(new ErrorMsgComparator());
        for (String s : errorList) {
            allErrors.add(new JsonPrimitive(s));
        }
        reponseObject.addProperty("status", "error");
        reponseObject.add("message", allErrors);
        response.setContentType("application/json");
        response.getWriter().write(reponseObject.toString());
        return;
    }

    // There may be type errors for the double here
    double amount = amt.getAsDouble();
    String courseID = crse.getAsString();
    String sectionID = sec.getAsString();
    String userID = user.getAsString();

    Course course = CourseDAO.getInstance().findCourse(courseID);
    Section section = SectionDAO.getInstance().findSection(courseID, sectionID);
    Student student = StudentDAO.retrieve(userID);

    int round = manager.getBiddingRound();
    BootstrapValidator bv = new BootstrapValidator();
    if (bv.parseBidAmount(amt.getAsString()) != null) {
        allErrors.add(new JsonPrimitive("invalid amount"));
    }

    if (course == null) {
        allErrors.add(new JsonPrimitive("invalid course"));
    }

    // only check if course code is valid
    if (course != null && section == null) {
        allErrors.add(new JsonPrimitive("invalid section"));
    }

    if (student == null) {
        allErrors.add(new JsonPrimitive("invalid userid"));
    }

    if (allErrors.size() > 0) {
        reponseObject.addProperty("status", "error");
        reponseObject.add("message", allErrors);
        response.setContentType("application/json");
        response.getWriter().write(reponseObject.toString());
        return;
    }

    if (manager.getBiddingRoundStatus().equals("started")) {
        if (round == 2) {
            double minBidAmt = MinBidDAO.getInstance().getValue(courseID + "-" + sectionID);
            System.out.println(courseID + "-" + sectionID);
            System.out.println("UPDATE amount: " + amount);
            System.out.println("UPDATE min bid: " + minBidAmt);
            if (amount < minBidAmt) {
                errorList.add("bid too low");
            }
        } else if (round == 1) {
            if (amount < 10) {
                errorList.add("bid too low");
            }
        }

        existingBid = BidDAO.getInstance().getStudentBidWithCourseID(userID, courseID);
        //no existing bid
        if (existingBid == null) {
            if (bv.parseEDollarEnough(userID, courseID, sectionID, amt.getAsString()) != null) {
                errorList.add("insufficient e$");
            }
        } else if (existingBid.getStatus().equals("pending")) {
            if (bv.parseEDollarEnoughExistingBid(userID, courseID, sectionID, amt.getAsString()) != null) {
                // Think too much alr, this line is not needed
                //                    errorList.add("insufficient e$");
            }
        } else if (existingBid.getStatus().equals("success")) {
            errorList.add("course enrolled");
        }

        if (bv.parseClassTimeTableClash(userID, courseID, sectionID) != null) {
            errorList.add("class timetable clash");
        }

        if (bv.parseExamTimeTableClash(userID, courseID, sectionID) != null) {
            errorList.add("exam timetable clash");
        }

        if (bv.parseIncompletePrerequisite(userID, courseID) != null) {
            errorList.add("incomplete prerequisites");
        }

        if (bv.parseAlreadyComplete(userID, courseID) != null) {
            errorList.add("course completed");
        }

        if (bv.parseSectionLimit(userID, courseID, sectionID) != null) {
            errorList.add("section limit reached");
        }

        if (bv.parseNotOwnSchoolCourse(userID, courseID) != null) {
            errorList.add("not own school course");
        }

        ArrayList<SectionStudent> sectionStudentList = SectionStudentDAO.getInstance()
                .getSectionStudentListWithID(courseID, sectionID);
        int vacancy = section.getSize() - sectionStudentList.size();
        if (vacancy <= 0) {
            errorList.add("no vacancy");
        }
    } else {
        errorList.add("round ended");
    }

    if (errorList.size() > 0) {
        Collections.sort(errorList);
        for (String s : errorList) {
            allErrors.add(new JsonPrimitive(s));
        }
        reponseObject.addProperty("status", "error");
        reponseObject.add("message", allErrors);
        response.setContentType("application/json");
        response.getWriter().write(reponseObject.toString());
        return;
    } else { //success state
        Bid newBid = new Bid(userID, amount, courseID, sectionID, "pending");
        if (existingBid != null) { //there is an existing bid
            MinBidDAO.getInstance().getMinBidList().remove(courseID + "-" + sectionID);
            MinBidDAO.getInstance().refresh();
            BidPlacementServlet.updateCurrentBid(userID, courseID, sectionID, amount, student);
        } else {
            student.seteDollar(student.geteDollar() - amount);
            manager.setEDollar(userID, student.geteDollar());
            BidDAO.getInstance().addBid(newBid);
            manager.addBid(newBid);
            //dk if this is needed
            if (round == 1) {
                RoundOneBidDAO.getInstance().addBid(newBid);
            } else if (round == 2) {
                RoundTwoBidDAO.getInstance().addBid(newBid);
            }
        }
        MinBidDAO.getInstance().refresh();
        reponseObject.addProperty("status", "success");
    }

    response.setContentType("application/json");
    response.getWriter().write(reponseObject.toString());
    return;
}

From source file:com.ccc.crest.core.cache.crest.tournament.Score.java

License:Open Source License

@Override
public Score deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (RedKey.equals(key))
            redTeam = value.getAsDouble();
        else if (BlueKey.equals(key))
            blueTeam = value.getAsDouble();
        else/*ww w  . j  a  va  2 s  .c om*/
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.cloud.bridge.util.JsonAccessor.java

License:Apache License

public double getAsDouble(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsDouble();
}

From source file:com.fooock.shodan.model.banner.BannerDeserializer.java

License:Open Source License

@Override
public List<Banner> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final List<Banner> banners = new ArrayList<>();
    if (json.isJsonNull()) {
        return banners;
    }/*from  w ww  .  j a v  a 2s .  c o  m*/
    JsonArray jsonArray = json.getAsJsonArray();
    if (jsonArray.isJsonNull()) {
        return banners;
    }
    for (JsonElement element : jsonArray) {
        JsonObject jsonObject = element.getAsJsonObject();
        JsonElement port = jsonObject.get("port");
        JsonElement ip = jsonObject.get("ip");
        JsonElement asn = jsonObject.get("asn");
        JsonElement data = jsonObject.get("data");
        JsonElement ipStr = jsonObject.get("ip_str");
        JsonElement ipv6 = jsonObject.get("ipv6");
        JsonElement timestamp = jsonObject.get("timestamp");
        JsonElement hostnames = jsonObject.get("hostnames");
        JsonElement domains = jsonObject.get("domains");
        JsonElement location = jsonObject.get("location");
        JsonElement options = jsonObject.get("opts");
        JsonElement metadata = jsonObject.get("_shodan");
        JsonElement ssl = jsonObject.get("ssl");

        JsonElement uptime = jsonObject.get("uptime");
        JsonElement link = jsonObject.get("link");
        JsonElement title = jsonObject.get("title");
        JsonElement html = jsonObject.get("html");
        JsonElement product = jsonObject.get("product");
        JsonElement version = jsonObject.get("version");
        JsonElement isp = jsonObject.get("isp");
        JsonElement os = jsonObject.get("os");
        JsonElement transport = jsonObject.get("transport");
        JsonElement devicetype = jsonObject.get("devicetype");
        JsonElement info = jsonObject.get("info");
        JsonElement cpe = jsonObject.get("cpe");

        final Banner banner = new Banner();
        if (port == null || port.isJsonNull()) {
            banner.setPort(0);
        } else {
            banner.setPort(port.getAsInt());
        }

        if (ip == null || ip.isJsonNull()) {
            banner.setIp(0);
        } else {
            banner.setIp(ip.getAsLong());
        }

        if (asn == null || asn.isJsonNull()) {
            banner.setAsn("unknown");
        } else {
            banner.setAsn(asn.getAsString());
        }

        if (data == null || data.isJsonNull()) {
            banner.setData("unknown");
        } else {
            banner.setData(data.getAsString());
        }

        if (ipStr == null || ipStr.isJsonNull()) {
            banner.setIpStr("unknown");
        } else {
            banner.setIpStr(ipStr.getAsString());
        }

        if (ipv6 == null || ipv6.isJsonNull()) {
            banner.setIpv6("unknown");
        } else {
            banner.setIpv6(ipv6.getAsString());
        }

        if (timestamp == null || timestamp.isJsonNull()) {
            banner.setTimestamp("unknown");
        } else {
            banner.setTimestamp(timestamp.getAsString());
        }

        if (hostnames == null || hostnames.isJsonNull()) {
            banner.setHostnames(new String[0]);
        } else {
            JsonArray hostnamesAsJsonArray = hostnames.getAsJsonArray();
            String[] hostnameArray = new String[hostnamesAsJsonArray.size()];
            for (int i = 0; i < hostnamesAsJsonArray.size(); i++) {
                hostnameArray[i] = hostnamesAsJsonArray.get(i).getAsString();
            }
            banner.setHostnames(hostnameArray);
        }

        if (domains == null || domains.isJsonNull()) {
            banner.setDomains(new String[0]);
        } else {
            JsonArray domainsAsJsonArray = domains.getAsJsonArray();
            String[] domainsArray = new String[domainsAsJsonArray.size()];
            for (int i = 0; i < domainsAsJsonArray.size(); i++) {
                domainsArray[i] = domainsAsJsonArray.get(i).getAsString();
            }
            banner.setDomains(domainsArray);
        }

        if (location == null || location.isJsonNull()) {
            banner.setLocation(new Location());
        } else {
            JsonObject locationAsJsonObject = location.getAsJsonObject();
            JsonElement areaCode = locationAsJsonObject.get("area_code");
            JsonElement latitude = locationAsJsonObject.get("latitude");
            JsonElement longitude = locationAsJsonObject.get("longitude");
            JsonElement city = locationAsJsonObject.get("city");
            JsonElement regionCode = locationAsJsonObject.get("region_code");
            JsonElement postalCode = locationAsJsonObject.get("postal_code");
            JsonElement dmaCode = locationAsJsonObject.get("dma_code");
            JsonElement countryCode = locationAsJsonObject.get("country_code");
            JsonElement countryCode3 = locationAsJsonObject.get("country_code3");
            JsonElement countryName = locationAsJsonObject.get("country_name");

            Location locationObject = new Location();
            if (areaCode == null || areaCode.isJsonNull()) {
                locationObject.setAreaCode(0);
            } else {
                locationObject.setAreaCode(areaCode.getAsInt());
            }

            if (latitude == null || latitude.isJsonNull()) {
                locationObject.setLatitude(0.0);
            } else {
                locationObject.setLatitude(latitude.getAsDouble());
            }

            if (longitude == null || location.isJsonNull()) {
                locationObject.setLongitude(0.0);
            } else {
                locationObject.setLongitude(longitude.getAsDouble());
            }

            if (city == null || city.isJsonNull()) {
                locationObject.setCity("unknown");
            } else {
                locationObject.setCity(city.getAsString());
            }

            if (regionCode == null || regionCode.isJsonNull()) {
                locationObject.setRegionCode("unknown");
            } else {
                locationObject.setRegionCode(regionCode.getAsString());
            }

            if (postalCode == null || postalCode.isJsonNull()) {
                locationObject.setPostalCode("unknown");
            } else {
                locationObject.setPostalCode(postalCode.getAsString());
            }

            if (dmaCode == null || dmaCode.isJsonNull()) {
                locationObject.setDmaCode("unknown");
            } else {
                locationObject.setDmaCode(dmaCode.getAsString());
            }

            if (countryCode == null || countryCode.isJsonNull()) {
                locationObject.setCountryCode("unknown");
            } else {
                locationObject.setCountryCode(countryCode.getAsString());
            }

            if (countryCode3 == null || countryCode3.isJsonNull()) {
                locationObject.setCountryCode3("unknown");
            } else {
                locationObject.setCountryCode3(countryCode3.getAsString());
            }

            if (countryName == null || countryName.isJsonNull()) {
                locationObject.setCountryName("unknown");
            } else {
                locationObject.setCountryName(countryName.getAsString());
            }

            banner.setLocation(locationObject);
        }

        final Options opts = new Options();
        if (options == null || options.isJsonNull()) {
            opts.setRaw("unknown");
        } else {
            JsonObject object = options.getAsJsonObject();
            JsonElement raw = object.get("raw");
            if (raw == null || raw.isJsonNull()) {
                opts.setRaw("unknown");
            } else {
                opts.setRaw(raw.getAsString());
            }
        }
        banner.setOptions(opts);

        final Metadata shodanMetadata = new Metadata();
        if (metadata == null || metadata.isJsonNull()) {
            shodanMetadata.setCrawler("unknown");
            shodanMetadata.setId("unknown");
            shodanMetadata.setModule("unknown");
        } else {
            JsonObject metadataAsJsonObject = metadata.getAsJsonObject();
            JsonElement crawler = metadataAsJsonObject.get("crawler");
            JsonElement id = metadataAsJsonObject.get("id");
            JsonElement module = metadataAsJsonObject.get("module");

            if (crawler == null || crawler.isJsonNull()) {
                shodanMetadata.setCrawler("unknown");
            } else {
                shodanMetadata.setCrawler(crawler.getAsString());
            }

            if (id == null || id.isJsonNull()) {
                shodanMetadata.setId("unknown");
            } else {
                shodanMetadata.setId(id.getAsString());
            }

            if (module == null || module.isJsonNull()) {
                shodanMetadata.setModule("unknown");
            } else {
                shodanMetadata.setModule(module.getAsString());
            }
        }
        banner.setMetadata(shodanMetadata);

        final SslInfo sslInfo = new SslInfo();
        if (ssl == null || ssl.isJsonNull()) {
            banner.setSslEnabled(false);
        } else {
            banner.setSslEnabled(true);

            JsonObject sslAsJsonObject = ssl.getAsJsonObject();
            JsonElement chain = sslAsJsonObject.get("chain");
            JsonArray chainAsJsonArray = chain.getAsJsonArray();
            String[] chainArray = new String[chainAsJsonArray.size()];
            for (int i = 0; i < chainAsJsonArray.size(); i++) {
                chainArray[i] = chainAsJsonArray.get(i).getAsString();
            }
            sslInfo.setChain(chainArray);

            JsonElement versions = sslAsJsonObject.get("versions");
            JsonArray versionAsJsonArray = versions.getAsJsonArray();
            String[] versionsArray = new String[versionAsJsonArray.size()];
            for (int i = 0; i < versionsArray.length; i++) {
                versionsArray[i] = versionAsJsonArray.get(i).getAsString();
            }
            sslInfo.setVersions(versionsArray);

            JsonElement cipher = sslAsJsonObject.get("cipher");
            JsonObject cipherAsJsonObject = cipher.getAsJsonObject();
            JsonElement bits = cipherAsJsonObject.get("bits");
            JsonElement cipherVersion = cipherAsJsonObject.get("version");
            JsonElement name = cipherAsJsonObject.get("name");

            final Cipher cipherObject = new Cipher();
            if (bits != null && !bits.isJsonNull()) {
                cipherObject.setBits(bits.getAsInt());
            }
            if (cipherVersion != null && !cipherVersion.isJsonNull()) {
                cipherObject.setVersion(cipherVersion.getAsString());
            } else {
                cipherObject.setVersion("unknown");
            }
            if (name == null || name.isJsonNull()) {
                cipherObject.setName("unknown");
            } else {
                cipherObject.setName(name.getAsString());
            }
            sslInfo.setCipher(cipherObject);

            JsonElement dhparams = sslAsJsonObject.get("dhparams");
            if (dhparams != null && !dhparams.isJsonNull()) {
                JsonObject dhparamsAsJsonObject = dhparams.getAsJsonObject();

                JsonElement bits1 = dhparamsAsJsonObject.get("bits");
                JsonElement prime = dhparamsAsJsonObject.get("prime");
                JsonElement publicKey = dhparamsAsJsonObject.get("public_key");
                JsonElement generator = dhparamsAsJsonObject.get("generator");
                JsonElement fingerprint = dhparamsAsJsonObject.get("fingerprint");

                final DiffieHellmanParams diffieHellmanParams = new DiffieHellmanParams();
                if (bits1 != null && !bits1.isJsonNull()) {
                    diffieHellmanParams.setBits(bits1.getAsInt());
                }
                if (prime == null || prime.isJsonNull()) {
                    diffieHellmanParams.setPrime("unknown");
                } else {
                    diffieHellmanParams.setPrime(prime.getAsString());
                }
                if (publicKey == null || publicKey.isJsonNull()) {
                    diffieHellmanParams.setPublicKey("unknown");
                } else {
                    diffieHellmanParams.setPublicKey(publicKey.getAsString());
                }
                if (generator == null || generator.isJsonNull()) {
                    diffieHellmanParams.setGenerator("unknown");
                } else {
                    diffieHellmanParams.setGenerator(generator.getAsString());
                }
                if (fingerprint == null || fingerprint.isJsonNull()) {
                    diffieHellmanParams.setFingerprint("unknown");
                } else {
                    diffieHellmanParams.setFingerprint(fingerprint.getAsString());
                }
                sslInfo.setDiffieHellmanParams(diffieHellmanParams);
            }
        }
        banner.setSslInfo(sslInfo);

        if (uptime == null || uptime.isJsonNull()) {
            banner.setUptime(0);
        } else {
            banner.setUptime(uptime.getAsInt());
        }

        if (link == null || link.isJsonNull()) {
            banner.setLink("unknown");
        } else {
            banner.setLink(link.getAsString());
        }

        if (title == null || title.isJsonNull()) {
            banner.setTitle("unknown");
        } else {
            banner.setTitle(title.getAsString());
        }

        if (html == null || html.isJsonNull()) {
            banner.setHtml("unknown");
        } else {
            banner.setHtml(html.getAsString());
        }

        if (product == null || product.isJsonNull()) {
            banner.setProduct("unknown");
        } else {
            banner.setProduct(product.getAsString());
        }

        if (version == null || version.isJsonNull()) {
            banner.setVersion("unknown");
        } else {
            banner.setVersion(version.getAsString());
        }

        if (isp == null || isp.isJsonNull()) {
            banner.setIsp("unknown");
        } else {
            banner.setIsp(isp.getAsString());
        }

        if (os == null || os.isJsonNull()) {
            banner.setOs("unknown");
        } else {
            banner.setOs(os.getAsString());
        }

        if (transport == null || transport.isJsonNull()) {
            banner.setTransport("unknown");
        } else {
            banner.setTransport(transport.getAsString());
        }

        if (devicetype == null || devicetype.isJsonNull()) {
            banner.setDeviceType("unknown");
        } else {
            banner.setDeviceType(devicetype.getAsString());
        }

        if (info == null || info.isJsonNull()) {
            banner.setInfo("unknown");
        } else {
            banner.setInfo(info.getAsString());
        }

        if (cpe == null || cpe.isJsonNull()) {
            banner.setCpe(new String[0]);
        } else {
            // cpe can be string or string[]. Fix for #4
            if (cpe.isJsonObject()) {
                banner.setCpe(new String[] { cpe.getAsString() });

            } else {
                JsonArray cpeAsJsonArray = cpe.getAsJsonArray();
                String[] cpeArray = new String[cpeAsJsonArray.size()];
                for (int i = 0; i < cpeAsJsonArray.size(); i++) {
                    cpeArray[i] = cpeAsJsonArray.get(i).getAsString();
                }
                banner.setCpe(cpeArray);
            }
        }

        banners.add(banner);
    }
    return banners;
}