diff --git a/java/com/google/flatbuffers/FlexBuffers.java b/java/com/google/flatbuffers/FlexBuffers.java index 3a5c6558b..7f71e0508 100644 --- a/java/com/google/flatbuffers/FlexBuffers.java +++ b/java/com/google/flatbuffers/FlexBuffers.java @@ -740,6 +740,11 @@ public class FlexBuffers { */ @Override public StringBuilder toString(StringBuilder sb) { + return sb.append(toString()); + } + + @Override + public String toString() { int size; for (int i = end; ; i++) { if (bb.get(i) == 0) { @@ -747,8 +752,7 @@ public class FlexBuffers { break; } } - sb.append(Utf8.getDefault().decodeUtf8(bb, end, size)); - return sb; + return Utf8.getDefault().decodeUtf8(bb, end, size); } int compareTo(byte[] other) { diff --git a/tests/JavaTest.java b/tests/JavaTest.java index 2a1e21574..b24601b8a 100644 --- a/tests/JavaTest.java +++ b/tests/JavaTest.java @@ -683,6 +683,7 @@ class JavaTest { // mymap vector FlexBuffers.Map mymap = m.get("mymap").asMap(); TestEq(mymap.keys().get(0), m.keys().get(0)); // These should be equal by pointer equality, since key and value are shared. + TestEq(mymap.keys().get(0).toString(), "bar"); TestEq(mymap.values().get(0).asString(), vec.get(1).asString()); TestEq(mymap.get("int").asInt(), -120); TestEq((float)mymap.get("float").asFloat(), -123.0f);