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();
}
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();
}
Aggregations