Example usage for com.google.gson JsonPrimitive getAsString

List of usage examples for com.google.gson JsonPrimitive getAsString

Introduction

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

Prototype

@Override
public String getAsString() 

Source Link

Document

convenience method to get this element as a String.

Usage

From source file:guru.qas.martini.report.column.ExceptionColumn.java

License:Apache License

@Override
public void addResult(State state, Cell cell, JsonObject o) {
    JsonArray array = o.getAsJsonArray(KEY_STEPS);
    int size = array.size();

    String value = null;//from   w w w.  j a  v  a2s . co  m
    for (int i = 0; null == value && i < size; i++) {
        JsonElement element = array.get(i);
        JsonObject step = element.getAsJsonObject();
        JsonElement keyElement = step.get(KEY_EXCEPTION);
        JsonPrimitive primitive = null != keyElement && keyElement.isJsonPrimitive()
                ? keyElement.getAsJsonPrimitive()
                : null;
        String stackTrace = null == primitive ? null : primitive.getAsString().trim();
        value = null != stackTrace && !stackTrace.isEmpty() ? stackTrace : null;
    }

    RichTextString richTextString = new XSSFRichTextString(value);
    cell.setCellValue(richTextString);
}

From source file:guru.qas.martini.report.column.ScenarioNameColumn.java

License:Apache License

@Override
public void addResult(State state, Cell cell, JsonObject o) {
    JsonPrimitive primitive = o.getAsJsonPrimitive("name");
    String name = null == primitive ? null : primitive.getAsString();
    RichTextString richTextString = new XSSFRichTextString(name);
    cell.setCellValue(richTextString);//from   ww w .java  2s.co m
}

From source file:guru.qas.martini.report.column.StatusColumn.java

License:Apache License

@Override
public void addResult(State state, Cell cell, JsonObject o) {
    JsonPrimitive primitive = o.getAsJsonPrimitive(KEY);
    String status = null == primitive ? null : primitive.getAsString();

    RichTextString richTextString = new XSSFRichTextString(status);
    cell.setCellValue(richTextString);//from   w  ww.j  a  v a2s  .  c om
    state.setStatus(cell, status);
}

From source file:guru.qas.martini.report.column.TagColumn.java

License:Apache License

protected String getTag(JsonElement element) {
    JsonObject entry = element.getAsJsonObject();
    JsonElement nameElement = entry.get(KEY_NAME);
    String name = null == nameElement ? null : nameElement.getAsString();

    String tag = null;/* w  w  w . j  a v  a  2  s . c  om*/
    if (null != name) {
        JsonElement argumentElement = entry.get(KEY_ARGUMENT);
        JsonPrimitive primitive = null != argumentElement && argumentElement.isJsonPrimitive()
                ? argumentElement.getAsJsonPrimitive()
                : null;
        String argument = null == primitive ? "" : String.format("\"%s\"", primitive.getAsString());
        tag = String.format("@%s(%s)", name, argument);
    }
    return tag;
}

From source file:guru.qas.martini.report.column.ThreadColumn.java

License:Apache License

@Override
public void addResult(State state, Cell cell, JsonObject o) {
    JsonPrimitive primitive = o.getAsJsonPrimitive(KEY_GROUP);
    String group = null == primitive ? "" : primitive.getAsString();

    primitive = o.getAsJsonPrimitive(KEY_NAME);
    String thread = null == primitive ? "" : primitive.getAsString();

    String value = group.isEmpty() ? thread : String.format("%s %s", group, thread);

    RichTextString richTextString = new XSSFRichTextString(value);
    cell.setCellValue(richTextString);/*  ww w . ja  v  a 2  s .c  o  m*/
}

From source file:guru.qas.martini.report.column.TimestampColumn.java

License:Apache License

protected void addResult(Cell cell, JsonPrimitive primitive) {
    String timestamp = primitive.getAsString();

    String value;/* w  w w.ja  v  a2s  . co  m*/
    try {
        Date date = new Date(Long.parseLong(timestamp));
        value = String.format("%s\n(%s)", timestamp, date);
    } catch (NumberFormatException e) {
        value = timestamp;
        LOGGER.warn("unable to parse '{}' to a long", timestamp, e);
    }

    RichTextString richTextString = new XSSFRichTextString(value);
    cell.setCellValue(richTextString);
}

From source file:guru.qas.martini.report.DefaultState.java

License:Apache License

protected String getId(JsonObject object) {
    JsonPrimitive primitive = object.getAsJsonPrimitive("id");
    return primitive.getAsString();
}

From source file:guru.qas.martini.report.DefaultState.java

License:Apache License

@Override
public void updateSuites(Sheet sheet) {
    int lastRowNum = sheet.getLastRowNum();
    Row row = sheet.createRow(0 == lastRowNum ? 0 : lastRowNum + 1);
    row.createCell(0, CellType.STRING).setCellValue("ID");
    row.createCell(1, CellType.STRING).setCellValue("Date");
    row.createCell(2, CellType.STRING).setCellValue("Name");
    row.createCell(3, CellType.STRING).setCellValue("Hostname");
    row.createCell(4, CellType.STRING).setCellValue("IP");
    row.createCell(5, CellType.STRING).setCellValue("Username");
    row.createCell(6, CellType.STRING).setCellValue("Profiles");
    row.createCell(7, CellType.STRING).setCellValue("Environment Variables");

    for (Map.Entry<String, JsonObject> mapEntry : suites.entrySet()) {
        row = sheet.createRow(sheet.getLastRowNum() + 1);

        String id = mapEntry.getKey();
        row.createCell(0, CellType.STRING).setCellValue(id);

        JsonObject suite = mapEntry.getValue();
        JsonPrimitive primitive = suite.getAsJsonPrimitive("startTimestamp");
        Long timestamp = null == primitive ? null : primitive.getAsLong();

        Cell cell = row.createCell(1);/* w  w  w  . j  a v  a 2 s. com*/
        if (null != timestamp) {
            Workbook workbook = sheet.getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            CreationHelper creationHelper = workbook.getCreationHelper();
            cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm"));
            cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
            cell.setCellValue(new Date(timestamp));
            cell.setCellStyle(cellStyle);
        }

        cell = row.createCell(2);
        primitive = suite.getAsJsonPrimitive("name");
        cell.setCellValue(null == primitive ? "" : primitive.getAsString());

        cell = row.createCell(3);
        JsonObject host = suite.getAsJsonObject("host");
        primitive = null == host ? null : host.getAsJsonPrimitive("name");
        cell.setCellValue(null == primitive ? "" : primitive.getAsString());

        cell = row.createCell(4);
        primitive = null == host ? null : host.getAsJsonPrimitive("ip");
        cell.setCellValue(null == primitive ? "" : primitive.getAsString());

        cell = row.createCell(5);
        primitive = null == host ? null : host.getAsJsonPrimitive("username");
        cell.setCellValue(null == primitive ? "" : primitive.getAsString());

        cell = row.createCell(6);
        JsonArray array = suite.getAsJsonArray("profiles");
        List<String> profiles = Lists.newArrayList();
        if (null != array) {
            int size = array.size();
            for (int i = 0; i < size; i++) {
                JsonElement element = array.get(i);
                String profile = null == element ? null : element.getAsString();
                profiles.add(profile);
            }
            String profilesValue = Joiner.on('\n').skipNulls().join(profiles);
            cell.setCellValue(profilesValue);
        }

        cell = row.createCell(7);
        JsonObject environmentVariables = suite.getAsJsonObject("environment");
        Map<String, String> index = new TreeMap<>();
        if (null != environmentVariables) {
            Set<Map.Entry<String, JsonElement>> entries = environmentVariables.entrySet();
            for (Map.Entry<String, JsonElement> environmentEntry : entries) {
                String key = environmentEntry.getKey();
                JsonElement element = environmentEntry.getValue();
                String value = null == element ? "" : element.getAsString();
                index.put(key, value);
            }

            String variablesValue = Joiner.on('\n').withKeyValueSeparator('=').useForNull("").join(index);
            cell.setCellValue(variablesValue);
        }
    }

    for (int i = 0; i < 8; i++) {
        sheet.autoSizeColumn(i, false);
    }
}

From source file:hdm.stuttgart.esell.router.GeneralObjectDeserializer.java

License:Apache License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    } else if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            return primitive.getAsString();
        } else if (primitive.isNumber()) {
            return primitive.getAsNumber();
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        }/*from w  w  w.jav  a  2s.co  m*/
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        Object[] result = new Object[array.size()];
        int i = 0;
        for (JsonElement element : array) {
            result[i] = deserialize(element, null, context);
            ++i;
        }
        return result;
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        Map<String, Object> result = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            Object value = deserialize(entry.getValue(), null, context);
            result.put(entry.getKey(), value);
        }
        return result;
    } else {
        throw new JsonParseException("Unknown JSON type for JsonElement " + json.toString());
    }
    return null;
}

From source file:io.flutter.dart.FlutterDartAnalysisServer.java

License:Open Source License

/**
 * Handle the given {@link JsonObject} response.
 *//*from  ww w  . j  a  v a 2 s . c o  m*/
private void processResponse(JsonObject response) {
    if (processNotification(response)) {
        return;
    }

    if (response.has("error")) {
        return;
    }

    final JsonObject resultObject = response.getAsJsonObject("result");
    if (resultObject == null) {
        return;
    }

    final JsonPrimitive idJsonPrimitive = (JsonPrimitive) response.get("id");
    if (idJsonPrimitive == null) {
        return;
    }
    final String idString = idJsonPrimitive.getAsString();

    final Consumer<JsonObject> consumer = responseConsumers.remove(idString);
    if (consumer == null) {
        return;
    }

    consumer.consume(resultObject);
}