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:org.apache.airavata.common.utils.JSONUtil.java

License:Apache License

private static boolean isEqual(JsonPrimitive primitiveOrig, JsonPrimitive primitiveNew) {
    if (primitiveOrig == null && primitiveNew == null) {
        return true;
    } else if (primitiveOrig == null || primitiveNew == null) {
        return false;
    } else {/*from ww w.  ja v  a  2  s  . co m*/
        if (primitiveOrig.isString() && primitiveNew.isString()) {
            if (!primitiveOrig.getAsString().equals(primitiveNew.getAsString())) {
                return false;
            }
        } else if (primitiveOrig.isBoolean() && primitiveNew.isBoolean()) {
            if ((Boolean.valueOf(primitiveOrig.getAsBoolean()).compareTo(primitiveNew.getAsBoolean()) != 0)) {
                return false;
            }
        } else if (primitiveOrig.isNumber() && primitiveNew.isNumber()) {
            if (new Double(primitiveOrig.getAsDouble()).compareTo(primitiveNew.getAsDouble()) != 0) {
                return false;
            }
        } else {
            return primitiveOrig.isJsonNull() && primitiveNew.isJsonNull();
        }
    }
    return true;
}

From source file:org.apache.airavata.workflow.model.graph.impl.GraphImpl.java

License:Apache License

protected void parse(JsonObject graphObject) throws GraphException {
    JsonPrimitive jsonPrimitive = graphObject.getAsJsonPrimitive(GraphSchema.GRAPH_ID_TAG);
    if (jsonPrimitive != null) {
        this.id = jsonPrimitive.getAsString();
    }// ww  w  .  java 2 s . co m
    jsonPrimitive = graphObject.getAsJsonPrimitive(GraphSchema.GRAPH_NAME_TAG);
    if (jsonPrimitive != null) {
        this.name = jsonPrimitive.getAsString();
    }
    jsonPrimitive = graphObject.getAsJsonPrimitive(GraphSchema.GRAPH_DESCRIPTION_TAG);
    if (jsonPrimitive != null) {
        this.description = jsonPrimitive.getAsString();
    }

    JsonArray jArray = graphObject.getAsJsonArray(GraphSchema.NODE_TAG);
    for (JsonElement jsonElement : jArray) {
        addNode(this.factory.createNode((JsonObject) jsonElement));
    }

    jArray = graphObject.getAsJsonArray(GraphSchema.PORT_TAG);
    for (JsonElement jsonElement : jArray) {
        addPort(this.factory.createPort((JsonObject) jsonElement));
    }

    jArray = graphObject.getAsJsonArray(GraphSchema.EDGE_TAG);
    for (JsonElement jsonElement : jArray) {
        addEdge(this.factory.createEdge((JsonObject) jsonElement));
    }

    indexToPointer();
}

From source file:org.apache.airavata.workflow.model.graph.impl.PortImpl.java

License:Apache License

protected void parse(JsonObject portObject) {
    this.id = portObject.getAsJsonPrimitive(GraphSchema.PORT_ID_TAG).getAsString();

    JsonPrimitive jPrimitive = portObject.getAsJsonPrimitive(GraphSchema.PORT_NAME_TAG);
    if (jPrimitive != null) {
        this.name = jPrimitive.getAsString();
    }//w w  w  .j  av a2s  . c  om

    this.nodeID = portObject.getAsJsonPrimitive(GraphSchema.PORT_NODE_TAG).getAsString();
}

From source file:org.apache.airavata.workflow.model.wf.Workflow.java

License:Apache License

private void parse(JsonObject workflowObject) throws GraphException, ComponentException {
    // Graph// w  w  w .  ja  v  a 2 s  .co m
    if (workflowObject.getAsJsonObject(WORKFLOW_TAG) == null) {
        throw new GraphException("Failed to parse the json object, workflow object doesn't exist");
    }
    JsonObject workflowObj = workflowObject.getAsJsonObject(WORKFLOW_TAG);
    JsonObject graphObject = workflowObj.getAsJsonObject(GraphSchema.GRAPH_TAG);
    this.graph = WSGraphFactory.createGraph(graphObject);

    bindComponents();

    // Image
    JsonPrimitive imagePrimitive = workflowObj.getAsJsonPrimitive(IMAGE_TAG);
    if (imagePrimitive != null) {
        String base64 = imagePrimitive.getAsString();
        byte[] bytes = Base64.decodeBase64(base64.getBytes());

        try {
            this.image = ImageIO.read(new ByteArrayInputStream(bytes));
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
}

From source file:org.apache.hadoop.hive.json.JsonSchemaFinder.java

License:Apache License

private static HiveType pickType(JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive prim = (JsonPrimitive) json;
        if (prim.isBoolean()) {
            return new BooleanType();
        } else if (prim.isNumber()) {
            BigDecimal dec = prim.getAsBigDecimal();
            if (dec.scale() > 0) {
                return new FloatingPointType(dec.doubleValue());
            } else {
                return new IntegerType(dec.longValue());
            }//ww  w  .  ja  v  a2s  . co  m
        } else {
            String str = prim.getAsString();
            if (DATE_PATTERN.matcher(str).matches()) {
                return new TimestampType();
            } else if (HEX_PATTERN.matcher(str).matches()) {
                return new BinaryType();
            } else {
                return new StringType();
            }
        }
    } else if (json.isJsonNull()) {
        return new NullType();
    } else if (json.isJsonArray()) {
        ListType result = new ListType();
        for (JsonElement child : ((JsonArray) json)) {
            HiveType sub = pickType(child);
            if (result.elementType == null) {
                result.elementType = sub;
            } else {
                result.elementType = mergeType(result.elementType, sub);
            }
        }
        return result;
    } else {
        JsonObject obj = (JsonObject) json;
        StructType result = new StructType();
        for (Map.Entry<String, JsonElement> field : obj.entrySet()) {
            String fieldName = field.getKey();
            HiveType type = pickType(field.getValue());
            result.fields.put(fieldName, type);
        }
        StringBuilder builder = new StringBuilder();
        boolean first = true;
        for (String key : result.fields.keySet()) {
            if (first) {
                first = false;
            } else {
                builder.append(",");
            }
            builder.append(key);
        }
        result.values.add(builder.toString());
        return result;
    }
}

From source file:org.apache.hadoop.hive.json.JsonShredder.java

License:Apache License

private void shredObject(String name, JsonElement json) throws IOException {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = (JsonPrimitive) json;
        if (primitive.isBoolean()) {
            getFile(name).append(primitive.getAsBoolean() + "\n");
        } else if (primitive.isString()) {
            getFile(name).append(primitive.getAsString().replace("\\", "\\\\").replace("\n", "\\n") + "\n");
        } else if (primitive.isNumber()) {
            getFile(name).append(primitive.getAsNumber() + "\n");
        }//from   w w  w  .  java  2  s  . c o m
    } else if (json.isJsonNull()) {
        // just skip it
    } else if (json.isJsonArray()) {
        for (JsonElement child : ((JsonArray) json)) {
            shredObject(name, child);
        }
    } else {
        JsonObject obj = (JsonObject) json;
        for (Map.Entry<String, JsonElement> field : obj.entrySet()) {
            String fieldName = field.getKey();
            shredObject(name + "." + fieldName, field.getValue());
        }
    }
}

From source file:org.apache.metamodel.elasticsearch.elastic1.rest.JestElasticSearchDataSet.java

License:Apache License

@Override
public boolean next() {
    final JsonArray hits = _searchResponse.getJsonObject().getAsJsonObject("hits").getAsJsonArray("hits");
    if (hits.size() == 0) {
        // break condition for the scroll
        _currentHit = null;//w  ww .j a v  a2 s.  co  m
        return false;
    }

    if (_hitIndex < hits.size()) {
        // pick the next hit within this search response
        _currentHit = hits.get(_hitIndex).getAsJsonObject();
        _hitIndex++;
        return true;
    }

    final JsonPrimitive scrollId = _searchResponse.getJsonObject().getAsJsonPrimitive("_scroll_id");
    if (scrollId == null) {
        // this search response is not scrollable - then it's the end.
        _currentHit = null;
        return false;
    }

    // try to scroll to the next set of hits
    final SearchScroll scroll = new SearchScroll.Builder(scrollId.getAsString(),
            ElasticSearchRestDataContext.TIMEOUT_SCROLL).build();

    _searchResponse = JestClientExecutor.execute(_client, scroll);

    // start over (recursively)
    _hitIndex = 0;
    return next();
}

From source file:org.apache.orc.tools.json.JsonSchemaFinder.java

License:Apache License

static HiveType pickType(JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive prim = (JsonPrimitive) json;
        if (prim.isBoolean()) {
            return new BooleanType();
        } else if (prim.isNumber()) {
            Matcher matcher = DECIMAL_PATTERN.matcher(prim.getAsString());
            if (matcher.matches()) {
                int intDigits = matcher.group("int").length();
                String fraction = matcher.group("fraction");
                int scale = fraction == null ? 0 : fraction.length();
                if (scale == 0) {
                    if (intDigits < 19) {
                        long value = prim.getAsLong();
                        if (value >= -128 && value < 128) {
                            return new NumericType(HiveType.Kind.BYTE, intDigits, scale);
                        } else if (value >= -32768 && value < 32768) {
                            return new NumericType(HiveType.Kind.SHORT, intDigits, scale);
                        } else if (value >= -2147483648 && value < 2147483648L) {
                            return new NumericType(HiveType.Kind.INT, intDigits, scale);
                        } else {
                            return new NumericType(HiveType.Kind.LONG, intDigits, scale);
                        }//  www. ja v  a 2s.  com
                    } else if (intDigits == 19) {
                        // at 19 digits, it may fit inside a long, but we need to check
                        BigInteger val = prim.getAsBigInteger();
                        if (val.compareTo(MIN_LONG) >= 0 && val.compareTo(MAX_LONG) <= 0) {
                            return new NumericType(HiveType.Kind.LONG, intDigits, scale);
                        }
                    }
                }
                if (intDigits + scale <= MAX_DECIMAL_DIGITS) {
                    return new NumericType(HiveType.Kind.DECIMAL, intDigits, scale);
                }
            }
            double value = prim.getAsDouble();
            if (value >= Float.MIN_VALUE && value <= Float.MAX_VALUE) {
                return new NumericType(HiveType.Kind.FLOAT, 0, 0);
            } else {
                return new NumericType(HiveType.Kind.DOUBLE, 0, 0);
            }
        } else {
            String str = prim.getAsString();
            if (TIMESTAMP_PATTERN.matcher(str).matches()) {
                return new StringType(HiveType.Kind.TIMESTAMP);
            } else if (HEX_PATTERN.matcher(str).matches()) {
                return new StringType(HiveType.Kind.BINARY);
            } else {
                return new StringType(HiveType.Kind.STRING);
            }
        }
    } else if (json.isJsonNull()) {
        return new NullType();
    } else if (json.isJsonArray()) {
        ListType result = new ListType();
        result.elementType = new NullType();
        for (JsonElement child : ((JsonArray) json)) {
            HiveType sub = pickType(child);
            if (result.elementType.subsumes(sub)) {
                result.elementType.merge(sub);
            } else if (sub.subsumes(result.elementType)) {
                sub.merge(result.elementType);
                result.elementType = sub;
            } else {
                result.elementType = new UnionType(result.elementType, sub);
            }
        }
        return result;
    } else {
        JsonObject obj = (JsonObject) json;
        StructType result = new StructType();
        for (Map.Entry<String, JsonElement> field : obj.entrySet()) {
            String fieldName = field.getKey();
            HiveType type = pickType(field.getValue());
            result.fields.put(fieldName, type);
        }
        return result;
    }
}

From source file:org.apache.orc.tools.json.JsonShredder.java

License:Apache License

private void shredObject(String name, JsonElement json) throws IOException {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = (JsonPrimitive) json;
        getFile(name).println(primitive.getAsString());
    } else if (json.isJsonNull()) {
        // just skip it
    } else if (json.isJsonArray()) {
        for (JsonElement child : ((JsonArray) json)) {
            shredObject(name + ".list", child);
        }/*from  w  ww  .ja v  a2s  .  com*/
    } else {
        JsonObject obj = (JsonObject) json;
        for (Map.Entry<String, JsonElement> field : obj.entrySet()) {
            String fieldName = field.getKey();
            shredObject(name + "." + fieldName, field.getValue());
        }
    }
}

From source file:org.apache.qpid.disttest.json.PropertyValueAdapter.java

License:Apache License

@Override
public PropertyValue deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    } else if (json.isJsonPrimitive()) {
        Object result = null;/*ww  w  . j  av a 2 s.c om*/
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            result = primitive.getAsString();
        } else if (primitive.isNumber()) {
            String asString = primitive.getAsString();
            if (asString.indexOf('.') != -1 || asString.indexOf('e') != -1) {
                result = primitive.getAsDouble();
            } else {
                result = primitive.getAsLong();
            }
        } else if (primitive.isBoolean()) {
            result = primitive.getAsBoolean();
        } else {
            throw new JsonParseException("Unsupported primitive value " + primitive);
        }
        return new SimplePropertyValue(result);
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        List<Object> result = new ArrayList<Object>(array.size());
        for (JsonElement element : array) {
            result.add(context.deserialize(element, Object.class));
        }
        return new SimplePropertyValue(result);
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        JsonElement defElement = object.getAsJsonPrimitive(DEF_FIELD);
        Class<?> classInstance = null;
        if (defElement != null) {
            try {
                classInstance = _factory.getPropertyValueClass(defElement.getAsString());
            } catch (ClassNotFoundException e) {
                // ignore
            }
        }
        if (classInstance == null) {
            Map<String, Object> result = new HashMap<String, Object>();
            for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
                Object value = context.deserialize(entry.getValue(), Object.class);
                result.put(entry.getKey(), value);
            }
            return new SimplePropertyValue(result);
        } else {
            return context.deserialize(json, classInstance);
        }
    } else {
        throw new JsonParseException("Unsupported JSON type " + json);
    }
}