-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[json-node] Add equals/hashcode to JsonNode
- Loading branch information
Showing
22 changed files
with
374 additions
and
3 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
8 changes: 8 additions & 0 deletions
8
blackbox-test/src/main/java/org/example/customer/node/HelloMixed.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,8 @@ | ||
package org.example.customer.node; | ||
|
||
import io.avaje.json.node.JsonNode; | ||
import io.avaje.jsonb.Json; | ||
|
||
@Json | ||
public record HelloMixed(String name, JsonNode other) { | ||
} |
26 changes: 26 additions & 0 deletions
26
blackbox-test/src/test/java/org/example/customer/node/HelloMixedTest.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,26 @@ | ||
package org.example.customer.node; | ||
|
||
import io.avaje.json.node.JsonObject; | ||
import io.avaje.jsonb.JsonType; | ||
import io.avaje.jsonb.Jsonb; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class HelloMixedTest { | ||
|
||
Jsonb jsonb = Jsonb.builder().build(); | ||
JsonType<HelloMixed> jsonType = jsonb.type(HelloMixed.class); | ||
|
||
@Test | ||
void test() { | ||
HelloMixed mixed = new HelloMixed("hi", JsonObject.create().add("a", "b")); | ||
|
||
String asJson = jsonType.toJson(mixed); | ||
assertThat(asJson).isEqualTo("{\"name\":\"hi\",\"other\":{\"a\":\"b\"}}"); | ||
|
||
HelloMixed fromJson = jsonType.fromJson(asJson); | ||
|
||
assertThat(fromJson).isEqualTo(mixed); | ||
} | ||
} |
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,54 @@ | ||
# avaje-json-core | ||
|
||
Provides the core API including JsonAdapter, JsonReader, JsonWriter, JsonStream API. | ||
|
||
## Dependency | ||
|
||
```xml | ||
<dependency> | ||
<groupId>io.avaje</groupId> | ||
<artifactId>avaje-json</artifactId> | ||
<version>3.0</version> | ||
</dependency> | ||
``` | ||
|
||
## SimpleMapper | ||
|
||
If you only have simple use cases you can use avaje-json | ||
without avaje-jsonb. | ||
|
||
For use cases that only want to map to the following types: | ||
- String | ||
- Boolean | ||
- Integer | ||
- Long | ||
- Double | ||
- Map<String, Object> | ||
- List<Object> | ||
|
||
### Create a SimpleMapper | ||
|
||
static final SimpleMapper mapper = SimpleMapper.builder().build() | ||
|
||
|
||
### Map example | ||
|
||
var map = Map.of("key", "some value", "otherKey", 42). | ||
|
||
String asJson = mapper.toJson(map); | ||
|
||
// read json into a Map | ||
Map<String,Object> asMap = mapper.fromJsonObject(asJson); | ||
|
||
|
||
### List example | ||
|
||
var map0 = Map.of("key", "a", "otherKey", 42); | ||
var map1 = Map.of("key", "b", "otherKey", 99); | ||
|
||
var list = List.of(map0, map1). | ||
|
||
String asJson = mapper.toJson(list); | ||
|
||
// read json into a List | ||
List<Object> asList = mapper.fromJsonArray(asJson); |
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,76 @@ | ||
# avaje-json-node | ||
|
||
Provides JsonNode types and associated JsonAdapters. | ||
|
||
## Dependency | ||
|
||
```xml | ||
<dependency> | ||
<groupId>io.avaje</groupId> | ||
<artifactId>avaje-json-node</artifactId> | ||
<version>3.0</version> | ||
</dependency> | ||
``` | ||
|
||
## Examples | ||
|
||
```java | ||
var jsonObject = JsonObject.create() | ||
.add("person", JsonObject.create().add("name", "myName").add("active", true)) | ||
.add("address", JsonObject.create().add("street", "42 some").add("city", "Bar")); | ||
``` | ||
|
||
```java | ||
var jsonArray = JsonArray.create() | ||
.add(42) | ||
.add("foo"); | ||
``` | ||
|
||
|
||
## Extract | ||
|
||
One reason for using JsonNode is to ease the filtering and transformation | ||
of json content. | ||
|
||
We can use the `extract()` methods to help with the Stream API filtering | ||
and mapping. | ||
|
||
```java | ||
JsonObject object = mapper.fromJsonObject(content); | ||
JsonArray arrayWithNestedPerson = (JsonArray) object.get("people"); | ||
|
||
List<String> lastNames = | ||
arrayWithNestedPerson.stream() | ||
.filter(node -> "family".equals(node.extract("type"))) | ||
.map(node -> node.extract("person.lastName")) | ||
.toList(); | ||
``` | ||
```java | ||
List<JsonNode> peopleNodes = | ||
arrayWithNestedPerson.stream() | ||
.filter(node -> "family".equals(node.extract("type"))) | ||
.map(node -> node.extractNode("person")) | ||
.toList(); | ||
``` | ||
|
||
|
||
### JsonNodeMapper | ||
|
||
If you don't need avaje-jsonb then we can just use JsonNodeMapper. | ||
|
||
```java | ||
// create a JsonNodeMapper | ||
static final JsonNodeMapper mapper = JsonNodeMapper.builder().build(); | ||
``` | ||
|
||
```java | ||
JsonArray jsonArray = JsonArray.create() | ||
.add(42) | ||
.add("foo"); | ||
|
||
var asJson = mapper.toJson(jsonArray); | ||
|
||
// read ARRAY from json | ||
JsonArray arrayFromJson = mapper.fromJsonArray(asJson); | ||
``` | ||
|
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
Oops, something went wrong.