Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java][Flexbuffers] Add API to add nullables into the buffer. #7521

Merged
merged 1 commit into from Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions java/com/google/flatbuffers/FlexBuffersBuilder.java
Expand Up @@ -173,6 +173,21 @@ public ReadWriteBuf getBuffer() {
return bb;
}

/**
* Insert a null value into the buffer
*/
public void putNull() {
putNull(null);
}

/**
* Insert a null value into the buffer
* @param key key used to store element in map
*/
public void putNull(String key) {
stack.add(Value.nullValue(putKey(key)));
}

/**
* Insert a single boolean into the buffer
* @param val true or false
Expand Down Expand Up @@ -675,6 +690,10 @@ private static class Value {
this.iValue = Long.MIN_VALUE;
}

static Value nullValue(int key) {
return new Value(key, FBT_NULL, WIDTH_8, 0);
}

static Value bool(int key, boolean b) {
return new Value(key, FBT_BOOL, WIDTH_8, b ? 1 : 0);
}
Expand Down
15 changes: 10 additions & 5 deletions tests/JavaTest.java
Expand Up @@ -959,21 +959,24 @@ public static void testSingleElementVector() {
int vecPos = b.startVector();
b.putInt(99);
b.putString("wow");
b.putNull();
int vecpos2 = b.startVector();
b.putInt(99);
b.putString("wow");
b.putNull();
b.endVector(null, vecpos2, false, false);
b.endVector(null, vecPos, false, false);
b.finish();

FlexBuffers.Reference r = FlexBuffers.getRoot(b.getBuffer());
TestEq(FlexBuffers.FBT_VECTOR, r.getType());
FlexBuffers.Vector vec = FlexBuffers.getRoot(b.getBuffer()).asVector();
TestEq(3, vec.size());
TestEq(4, vec.size());
TestEq(99, vec.get(0).asInt());
TestEq("wow", vec.get(1).asString());
TestEq("[ 99, \"wow\" ]", vec.get(2).toString());
TestEq("[ 99, \"wow\", [ 99, \"wow\" ] ]", FlexBuffers.getRoot(b.getBuffer()).toString());
TestEq(true, vec.get(2).isNull());
TestEq("[ 99, \"wow\", null ]", vec.get(3).toString());
TestEq("[ 99, \"wow\", null, [ 99, \"wow\", null ] ]", FlexBuffers.getRoot(b.getBuffer()).toString());
}

public static void testSingleElementMap() {
Expand All @@ -983,6 +986,7 @@ public static void testSingleElementMap() {
b.putInt("myInt", 0x7fffffbbbfffffffL);
b.putString("myString", "wow");
b.putString("myString2", "incredible");
b.putNull("myNull");
int start = b.startVector();
b.putInt(99);
b.putString("wow");
Expand All @@ -995,14 +999,15 @@ public static void testSingleElementMap() {
FlexBuffers.Reference r = FlexBuffers.getRoot(b.getBuffer());
TestEq(FlexBuffers.FBT_MAP, r.getType());
FlexBuffers.Map map = FlexBuffers.getRoot(b.getBuffer()).asMap();
TestEq(5, map.size());
TestEq(6, map.size());
TestEq(0x7fffffbbbfffffffL, map.get("myInt").asLong());
TestEq("wow", map.get("myString").asString());
TestEq("incredible", map.get("myString2").asString());
TestEq(true, map.get("myNull").isNull());
TestEq(99, map.get("myVec").asVector().get(0).asInt());
TestEq("wow", map.get("myVec").asVector().get(1).asString());
TestEq(Double.compare(0x1.ffffbbbffffffP+1023, map.get("double").asFloat()), 0);
TestEq("{ \"double\" : 1.7976894783391937E308, \"myInt\" : 9223371743723257855, \"myString\" : \"wow\", \"myString2\" : \"incredible\", \"myVec\" : [ 99, \"wow\" ] }",
TestEq("{ \"double\" : 1.7976894783391937E308, \"myInt\" : 9223371743723257855, \"myNull\" : null, \"myString\" : \"wow\", \"myString2\" : \"incredible\", \"myVec\" : [ 99, \"wow\" ] }",
FlexBuffers.getRoot(b.getBuffer()).toString());
}

Expand Down