Search in sources :

Example 1 with StructField

use of org.ballerinalang.model.types.BStructType.StructField in project ballerina by ballerina-lang.

the class BStruct method stringValue.

/**
 * {@inheritDoc}
 */
@Override
public String stringValue() {
    int stringIndex = 0, intIndex = 0, longIndex = 0, doubleIndex = 0, byteIndex = 0, refValIndex = 0;
    StringJoiner sj = new StringJoiner(", ", "{", "}");
    for (StructField field : structType.getStructFields()) {
        String fieldName = field.getFieldName();
        Object fieldVal;
        BType fieldType = field.getFieldType();
        if (fieldType == BTypes.typeString) {
            fieldVal = "\"" + stringFields[stringIndex++] + "\"";
        } else if (fieldType == BTypes.typeInt) {
            fieldVal = longFields[longIndex++];
        } else if (fieldType == BTypes.typeFloat) {
            fieldVal = doubleFields[doubleIndex++];
        } else if (fieldType == BTypes.typeBoolean) {
            fieldVal = intFields[intIndex++] == 1;
        } else if (fieldType == BTypes.typeBlob) {
            byte[] blob = byteFields[byteIndex++];
            fieldVal = blob == null ? null : new String(blob, StandardCharsets.UTF_8);
        } else {
            BValue val = refFields[refValIndex++];
            fieldVal = val == null ? null : val.stringValue();
        }
        sj.add(fieldName + ":" + fieldVal);
    }
    return sj.toString();
}
Also used : StructField(org.ballerinalang.model.types.BStructType.StructField) BType(org.ballerinalang.model.types.BType) StringJoiner(java.util.StringJoiner)

Example 2 with StructField

use of org.ballerinalang.model.types.BStructType.StructField in project ballerina by ballerina-lang.

the class BJSON method stringValue.

@Override
public String stringValue() {
    JsonNode node = this.value();
    if (node.isValueNode()) {
        return this.value().asText();
    } else if (!node.isObject()) {
        return node.toString();
    }
    BStructType constrainedType = (BStructType) ((BJSONType) this.type).getConstrainedType();
    if (constrainedType == null) {
        return node.toString();
    }
    // If constrained JSON, print the only the fields in the constrained type.
    StringJoiner sj = new StringJoiner(",", "{", "}");
    for (StructField field : constrainedType.getStructFields()) {
        String key = field.fieldName;
        String stringValue = this.value().get(key).toString();
        sj.add("\"" + key + "\":" + stringValue);
    }
    return sj.toString();
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) StructField(org.ballerinalang.model.types.BStructType.StructField) JsonNode(org.ballerinalang.model.util.JsonNode) StringJoiner(java.util.StringJoiner)

Aggregations

StringJoiner (java.util.StringJoiner)2 StructField (org.ballerinalang.model.types.BStructType.StructField)2 BStructType (org.ballerinalang.model.types.BStructType)1 BType (org.ballerinalang.model.types.BType)1 JsonNode (org.ballerinalang.model.util.JsonNode)1