forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
78 changed files
with
812 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../src/test/java/com/baeldung/jsonnodetoarraynode/JsonNodeToArrayNodeConverterUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.baeldung.jsonnodetoarraynode; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
|
||
public class JsonNodeToArrayNodeConverterUnitTest { | ||
@Test | ||
void givenJsonNode_whenUsingJsonNodeMethods_thenConvertToArrayNode() throws JsonProcessingException { | ||
int count = 0; | ||
String json = "{\"objects\": [\"One\", \"Two\", \"Three\"]}"; | ||
final JsonNode arrayNode = new ObjectMapper().readTree(json).get("objects"); | ||
if (arrayNode.isArray()) { | ||
for (final JsonNode objNode : arrayNode) { | ||
assertNotNull(objNode, "Array element should not be null"); | ||
count++; | ||
} | ||
} | ||
assertNotNull(arrayNode, "The 'objects' array should not be null"); | ||
assertTrue(arrayNode.isArray(), "The 'objects' should be an array"); | ||
assertEquals(3, count, "The 'objects' array should have 3 elements"); | ||
} | ||
|
||
@Test | ||
void givenJsonNode_whenUsingCreateArrayNode_thenConvertToArrayNode() throws Exception { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode originalJsonNode = objectMapper.readTree("{\"objects\": [\"One\", \"Two\", \"Three\"]}"); | ||
ArrayNode arrayNode = objectMapper.createArrayNode(); | ||
originalJsonNode.get("objects").elements().forEachRemaining(arrayNode::add); | ||
assertEquals("[\"One\",\"Two\",\"Three\"]", arrayNode.toString()); | ||
} | ||
|
||
@Test | ||
void givenJsonNode_whenUsingStreamSupport_thenConvertToArrayNode() throws Exception { | ||
String json = "{\"objects\": [\"One\", \"Two\", \"Three\"]}"; | ||
JsonNode obj = new ObjectMapper().readTree(json); | ||
List<JsonNode> objects = StreamSupport | ||
.stream(obj.get("objects").spliterator(), false) | ||
.collect(Collectors.toList()); | ||
|
||
assertEquals(3, objects.size(), "The 'objects' list should contain 3 elements"); | ||
|
||
JsonNode firstObject = objects.get(0); | ||
assertEquals("One", firstObject.asText(), "The first element should be One"); | ||
} | ||
|
||
@Test | ||
void givenJsonNode_whenUsingIterator_thenConvertToArrayNode() throws Exception { | ||
String json = "{\"objects\": [\"One\", \"Two\", \"Three\"]}"; | ||
JsonNode datasets = new ObjectMapper().readTree(json); | ||
Iterator<JsonNode> iterator = datasets.withArray("objects").elements(); | ||
|
||
int count = 0; | ||
while (iterator.hasNext()) { | ||
JsonNode dataset = iterator.next(); | ||
System.out.print(dataset.toString() + " "); | ||
count++; | ||
} | ||
assertEquals(3, count, "The 'objects' list should contain 3 elements"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>parent-modules</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>libraries-data-io-2</artifactId> | ||
<name>libraries-data-io-3</name> | ||
|
||
<properties> | ||
<maven.compiler.source>19</maven.compiler.source> | ||
<maven.compiler.target>19</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<google-flatbuffers.version>23.5.26</google-flatbuffers.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.flatbuffers</groupId> | ||
<artifactId>flatbuffers-java</artifactId> | ||
<version>${google-flatbuffers.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
18 changes: 18 additions & 0 deletions
18
libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Color.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// automatically generated by the FlatBuffers compiler, do not modify | ||
|
||
package MyGame.terrains; | ||
|
||
@SuppressWarnings("unused") | ||
public final class Color { | ||
private Color() { } | ||
public static final byte Brown = 0; | ||
public static final byte Red = 1; | ||
public static final byte Green = 2; | ||
public static final byte Blue = 3; | ||
public static final byte White = 4; | ||
|
||
public static final String[] names = { "Brown", "Red", "Green", "Blue", "White", }; | ||
|
||
public static String name(int e) { return names[e]; } | ||
} | ||
|
51 changes: 51 additions & 0 deletions
51
libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Effect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// automatically generated by the FlatBuffers compiler, do not modify | ||
|
||
package MyGame.terrains; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
import com.google.flatbuffers.BaseVector; | ||
import com.google.flatbuffers.Constants; | ||
import com.google.flatbuffers.FlatBufferBuilder; | ||
import com.google.flatbuffers.Table; | ||
|
||
@SuppressWarnings("unused") | ||
public final class Effect extends Table { | ||
public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); } | ||
public static Effect getRootAsEffect(ByteBuffer _bb) { return getRootAsEffect(_bb, new Effect()); } | ||
public static Effect getRootAsEffect(ByteBuffer _bb, Effect obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } | ||
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); } | ||
public Effect __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } | ||
|
||
public String name() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; } | ||
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(4, 1); } | ||
public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 4, 1); } | ||
public short damage() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 0; } | ||
public boolean mutateDamage(short damage) { int o = __offset(6); if (o != 0) { bb.putShort(o + bb_pos, damage); return true; } else { return false; } } | ||
|
||
public static int createEffect(FlatBufferBuilder builder, | ||
int nameOffset, | ||
short damage) { | ||
builder.startTable(2); | ||
Effect.addName(builder, nameOffset); | ||
Effect.addDamage(builder, damage); | ||
return Effect.endEffect(builder); | ||
} | ||
|
||
public static void startEffect(FlatBufferBuilder builder) { builder.startTable(2); } | ||
public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(0, nameOffset, 0); } | ||
public static void addDamage(FlatBufferBuilder builder, short damage) { builder.addShort(1, damage, 0); } | ||
public static int endEffect(FlatBufferBuilder builder) { | ||
int o = builder.endTable(); | ||
return o; | ||
} | ||
|
||
public static final class Vector extends BaseVector { | ||
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; } | ||
|
||
public Effect get(int j) { return get(new Effect(), j); } | ||
public Effect get(Effect obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); } | ||
} | ||
} | ||
|
59 changes: 59 additions & 0 deletions
59
libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Terrain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// automatically generated by the FlatBuffers compiler, do not modify | ||
|
||
package MyGame.terrains; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
import com.google.flatbuffers.BaseVector; | ||
import com.google.flatbuffers.Constants; | ||
import com.google.flatbuffers.FlatBufferBuilder; | ||
import com.google.flatbuffers.Table; | ||
|
||
@SuppressWarnings("unused") | ||
public final class Terrain extends Table { | ||
public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); } | ||
public static Terrain getRootAsTerrain(ByteBuffer _bb) { return getRootAsTerrain(_bb, new Terrain()); } | ||
public static Terrain getRootAsTerrain(ByteBuffer _bb, Terrain obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } | ||
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); } | ||
public Terrain __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } | ||
|
||
public MyGame.terrains.Vec3 pos() { return pos(new MyGame.terrains.Vec3()); } | ||
public MyGame.terrains.Vec3 pos(MyGame.terrains.Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; } | ||
public String name() { int o = __offset(6); return o != 0 ? __string(o + bb_pos) : null; } | ||
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(6, 1); } | ||
public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 6, 1); } | ||
public byte color() { int o = __offset(8); return o != 0 ? bb.get(o + bb_pos) : 3; } | ||
public boolean mutateColor(byte color) { int o = __offset(8); if (o != 0) { bb.put(o + bb_pos, color); return true; } else { return false; } } | ||
public String navigation() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; } | ||
public ByteBuffer navigationAsByteBuffer() { return __vector_as_bytebuffer(10, 1); } | ||
public ByteBuffer navigationInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 10, 1); } | ||
public MyGame.terrains.Effect effects(int j) { return effects(new MyGame.terrains.Effect(), j); } | ||
public MyGame.terrains.Effect effects(MyGame.terrains.Effect obj, int j) { int o = __offset(12); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; } | ||
public int effectsLength() { int o = __offset(12); return o != 0 ? __vector_len(o) : 0; } | ||
public MyGame.terrains.Effect.Vector effectsVector() { return effectsVector(new MyGame.terrains.Effect.Vector()); } | ||
public MyGame.terrains.Effect.Vector effectsVector(MyGame.terrains.Effect.Vector obj) { int o = __offset(12); return o != 0 ? obj.__assign(__vector(o), 4, bb) : null; } | ||
|
||
public static void startTerrain(FlatBufferBuilder builder) { builder.startTable(5); } | ||
public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); } | ||
public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(1, nameOffset, 0); } | ||
public static void addColor(FlatBufferBuilder builder, byte color) { builder.addByte(2, color, 3); } | ||
public static void addNavigation(FlatBufferBuilder builder, int navigationOffset) { builder.addOffset(3, navigationOffset, 0); } | ||
public static void addEffects(FlatBufferBuilder builder, int effectsOffset) { builder.addOffset(4, effectsOffset, 0); } | ||
public static int createEffectsVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); } | ||
public static void startEffectsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } | ||
public static int endTerrain(FlatBufferBuilder builder) { | ||
int o = builder.endTable(); | ||
return o; | ||
} | ||
public static void finishTerrainBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset); } | ||
public static void finishSizePrefixedTerrainBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset); } | ||
|
||
public static final class Vector extends BaseVector { | ||
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; } | ||
|
||
public Terrain get(int j) { return get(new Terrain(), j); } | ||
public Terrain get(Terrain obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); } | ||
} | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
libraries-data-io-2/src/main/java/com/baeldung/flatbuffers/MyGame/terrains/Vec3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// automatically generated by the FlatBuffers compiler, do not modify | ||
|
||
package MyGame.terrains; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
import com.google.flatbuffers.BaseVector; | ||
import com.google.flatbuffers.FlatBufferBuilder; | ||
import com.google.flatbuffers.Struct; | ||
|
||
@SuppressWarnings("unused") | ||
public final class Vec3 extends Struct { | ||
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); } | ||
public Vec3 __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } | ||
|
||
public float x() { return bb.getFloat(bb_pos + 0); } | ||
public void mutateX(float x) { bb.putFloat(bb_pos + 0, x); } | ||
public float y() { return bb.getFloat(bb_pos + 4); } | ||
public void mutateY(float y) { bb.putFloat(bb_pos + 4, y); } | ||
public float z() { return bb.getFloat(bb_pos + 8); } | ||
public void mutateZ(float z) { bb.putFloat(bb_pos + 8, z); } | ||
|
||
public static int createVec3(FlatBufferBuilder builder, float x, float y, float z) { | ||
builder.prep(4, 12); | ||
builder.putFloat(z); | ||
builder.putFloat(y); | ||
builder.putFloat(x); | ||
return builder.offset(); | ||
} | ||
|
||
public static final class Vector extends BaseVector { | ||
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; } | ||
|
||
public Vec3 get(int j) { return get(new Vec3(), j); } | ||
public Vec3 get(Vec3 obj, int j) { return obj.__assign(__element(j), bb); } | ||
} | ||
} | ||
|
Oops, something went wrong.