diff options
author | Paulo Pinheiro <paulovictor.pinheiro@gmail.com> | 2019-09-10 19:06:47 +0200 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-09-10 10:06:47 -0700 |
commit | b5560fcd525a7b45ee45c8037ec5f95a5ea81439 (patch) | |
tree | 46b0e7fcfda1344796ef286bff3e8e08ea64d813 /docs/source | |
parent | 782b865c5533ad020a61ae79ec8a658acfa96861 (diff) | |
download | flatbuffers-b5560fcd525a7b45ee45c8037ec5f95a5ea81439.tar.gz |
[Java][FlexBuffers] Improve documentation for FlexBuffers in Java. (#5506)
Also add a FlexBuffer constructor to simplify usage
Diffstat (limited to 'docs/source')
-rw-r--r-- | docs/source/FlexBuffers.md | 44 | ||||
-rw-r--r-- | docs/source/doxyfile | 2 | ||||
-rw-r--r-- | docs/source/doxygen_layout.xml | 2 |
3 files changed, 43 insertions, 5 deletions
diff --git a/docs/source/FlexBuffers.md b/docs/source/FlexBuffers.md index f481cbab..974dd693 100644 --- a/docs/source/FlexBuffers.md +++ b/docs/source/FlexBuffers.md @@ -29,9 +29,7 @@ FlexBuffers is still slower than regular FlatBuffers though, so we recommend to only use it if you need it. -# Usage - -This is for C++, other languages may follow. +# Usage in C++ Include the header `flexbuffers.h`, which in turn depends on `flatbuffers.h` and `util.h`. @@ -122,6 +120,46 @@ map["unknown"].IsNull(); // true ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Usage in Java + +Java implementation follows the C++ one, closely. + +For creating the equivalent of the same JSON `{ vec: [ -100, "Fred", 4.0 ], foo: 100 }`, +one could use the following code: + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java} +FlexBuffersBuilder builder = new FlexBuffersBuilder(ByteBuffer.allocate(512), + FlexBuffersBuilder.BUILDER_FLAG_SHARE_KEYS_AND_STRINGS); +int smap = builder.startMap(); +int svec = builder.startVector(); +builder.putInt(-100); +builder.putString("Fred"); +builder.putFloat(4.0); +builder.endVector("vec", svec, false, false); +builder.putInt("foo", 100); +builder.endMap(null, smap); +ByteBuffer bb = builder.finish(); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Similarly, to read the data, just: + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java} +FlexBuffers.Map map = FlexBuffers.getRoot(bb).asMap(); +map.size(); // 2 +FlexBuffers.Vector vec = map.get("vec").asVector(); +vec.size(); // 3 +vec.get(0).asLong(); // -100; +vec.get(1).asString(); // "Fred"; +vec.get(1).asLong(); // 0 (Number parsing failed). +vec.get(2).asFloat(); // 4.0 +vec.get(2).asString().isEmpty(); // true (Wrong Type). +vec.get(2).asString(); // "" (This still works though). +vec.get(2).toString(); // "4.0" (Or have it converted). +map.get("foo").asUInt(); // 100 +map.get("unknown").isNull(); // true +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + # Binary encoding A description of how FlexBuffers are encoded is in the diff --git a/docs/source/doxyfile b/docs/source/doxyfile index 6ba3c108..b381e3dd 100644 --- a/docs/source/doxyfile +++ b/docs/source/doxyfile @@ -778,7 +778,7 @@ INPUT = "FlatBuffers.md" \ "../../php/FlatbufferBuilder.php" \ "../../net/FlatBuffers/FlatBufferBuilder.cs" \ "../../include/flatbuffers/flatbuffers.h" \ - "../../go/builder.go" + "../../go/builder.go" \ "../../rust/flatbuffers/src/builder.rs" # This tag can be used to specify the character encoding of the source files diff --git a/docs/source/doxygen_layout.xml b/docs/source/doxygen_layout.xml index a2325075..8253903d 100644 --- a/docs/source/doxygen_layout.xml +++ b/docs/source/doxygen_layout.xml @@ -48,7 +48,7 @@ <tab type="user" url="@ref flatbuffers_guide_use_rust" title="Use in Rust"/> <tab type="user" url="@ref flexbuffers" - title="Schema-less version"/> + title="FlexBuffers (Schema-less version)"/> <tab type="usergroup" url="" title="gRPC"> <tab type="user" url="@ref flatbuffers_grpc_guide_use_cpp" title="Use in C++"/> |