-
-
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 pull request #33 from sebastian-toepfer/printable_json
add posibility to print a json object
- Loading branch information
Showing
13 changed files
with
398 additions
and
0 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
30 changes: 30 additions & 0 deletions
30
...json-api/src/main/java/io/github/sebastiantoepfer/ddd/media/json/JsonObjectPrintable.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,30 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
import io.github.sebastiantoepfer.ddd.media.json.printable.JsonMappedPrintables; | ||
import jakarta.json.JsonObject; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class JsonObjectPrintable implements Printable { | ||
|
||
private final JsonObject json; | ||
|
||
public JsonObjectPrintable(final JsonObject json) { | ||
this.json = Objects.requireNonNull(json); | ||
} | ||
|
||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
T result = media; | ||
for (Printable printable : createPrintables()) { | ||
result = printable.printOn(result); | ||
} | ||
return result; | ||
} | ||
|
||
private List<Printable> createPrintables() { | ||
return json.entrySet().stream().map(JsonMappedPrintables::new).map(JsonMappedPrintables::toPrintable).toList(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...c/main/java/io/github/sebastiantoepfer/ddd/media/json/printable/ArrayJsonMappedValue.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,35 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
import jakarta.json.JsonArray; | ||
import java.util.List; | ||
|
||
class ArrayJsonMappedValue implements JsonMappedValue { | ||
|
||
private final JsonArray array; | ||
|
||
public ArrayJsonMappedValue(final JsonArray array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public List<?> toValue() { | ||
return array | ||
.stream() | ||
.map(JsonMappedValues::new) | ||
.map(JsonMappedValues::toMappedValue) | ||
.map(JsonMappedValue::toValue) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public Printable toPrintable(final String name) { | ||
return new Printable() { | ||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(name, toValue()); | ||
} | ||
}; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...main/java/io/github/sebastiantoepfer/ddd/media/json/printable/BooleanJsonMappedValue.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,29 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
import jakarta.json.JsonValue; | ||
|
||
class BooleanJsonMappedValue implements JsonMappedValue { | ||
|
||
private final JsonValue.ValueType value; | ||
|
||
public BooleanJsonMappedValue(final JsonValue.ValueType value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public Boolean toValue() { | ||
return value == JsonValue.ValueType.TRUE; | ||
} | ||
|
||
@Override | ||
public Printable toPrintable(final String name) { | ||
return new Printable() { | ||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(name, toValue()); | ||
} | ||
}; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ava/io/github/sebastiantoepfer/ddd/media/json/printable/DecimalNumberJsonMappedValue.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,29 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
import jakarta.json.JsonNumber; | ||
|
||
class DecimalNumberJsonMappedValue implements JsonMappedValue { | ||
|
||
private final JsonNumber value; | ||
|
||
public DecimalNumberJsonMappedValue(final JsonNumber value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public Printable toPrintable(final String name) { | ||
return new Printable() { | ||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(name, toValue()); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Double toValue() { | ||
return value.doubleValue(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ava/io/github/sebastiantoepfer/ddd/media/json/printable/IntegerNumberJsonMappedValue.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,29 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
import jakarta.json.JsonNumber; | ||
|
||
class IntegerNumberJsonMappedValue implements JsonMappedValue { | ||
|
||
private final JsonNumber value; | ||
|
||
public IntegerNumberJsonMappedValue(final JsonNumber value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public Printable toPrintable(final String name) { | ||
return new Printable() { | ||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(name, toValue()); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Long toValue() { | ||
return value.longValueExact(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...c/main/java/io/github/sebastiantoepfer/ddd/media/json/printable/JsonMappedPrintables.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,20 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
import jakarta.json.JsonValue; | ||
import java.util.Map; | ||
|
||
public class JsonMappedPrintables { | ||
|
||
private final Map.Entry<String, JsonValue> entry; | ||
|
||
@SuppressFBWarnings("EI_EXPOSE_REP2") | ||
public JsonMappedPrintables(final Map.Entry<String, JsonValue> entry) { | ||
this.entry = entry; | ||
} | ||
|
||
public Printable toPrintable() { | ||
return new JsonMappedValues(entry.getValue()).toMappedValue().toPrintable(entry.getKey()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...pi/src/main/java/io/github/sebastiantoepfer/ddd/media/json/printable/JsonMappedValue.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,9 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
|
||
interface JsonMappedValue { | ||
Object toValue(); | ||
|
||
Printable toPrintable(String name); | ||
} |
38 changes: 38 additions & 0 deletions
38
...i/src/main/java/io/github/sebastiantoepfer/ddd/media/json/printable/JsonMappedValues.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 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonNumber; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonString; | ||
import jakarta.json.JsonValue; | ||
|
||
class JsonMappedValues { | ||
|
||
private final JsonValue value; | ||
|
||
public JsonMappedValues(final JsonValue value) { | ||
this.value = value; | ||
} | ||
|
||
public JsonMappedValue toMappedValue() { | ||
final JsonMappedValue result; | ||
if (value instanceof JsonString str) { | ||
result = new StringJsonMappedValue(str); | ||
} else if (value instanceof JsonNumber number) { | ||
if (number.isIntegral()) { | ||
result = new IntegerNumberJsonMappedValue(number); | ||
} else { | ||
result = new DecimalNumberJsonMappedValue(number); | ||
} | ||
} else if (value instanceof JsonArray array) { | ||
result = new ArrayJsonMappedValue(array); | ||
} else if (value instanceof JsonObject obj) { | ||
result = new ObjectJsonMappedValue(obj); | ||
} else if (value.getValueType() == JsonValue.ValueType.NULL) { | ||
result = new NullJsonMappedValue(); | ||
} else { | ||
result = new BooleanJsonMappedValue(value.getValueType()); | ||
} | ||
return result; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...rc/main/java/io/github/sebastiantoepfer/ddd/media/json/printable/NullJsonMappedValue.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,22 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
|
||
class NullJsonMappedValue implements JsonMappedValue { | ||
|
||
@Override | ||
public Void toValue() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Printable toPrintable(final String name) { | ||
return new Printable() { | ||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media; | ||
} | ||
}; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../main/java/io/github/sebastiantoepfer/ddd/media/json/printable/ObjectJsonMappedValue.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,30 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable; | ||
import jakarta.json.JsonObject; | ||
|
||
class ObjectJsonMappedValue implements JsonMappedValue { | ||
|
||
private final JsonObject json; | ||
|
||
public ObjectJsonMappedValue(final JsonObject json) { | ||
this.json = json; | ||
} | ||
|
||
@Override | ||
public Printable toPrintable(final String name) { | ||
return new Printable() { | ||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(name, toValue()); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Printable toValue() { | ||
return new JsonObjectPrintable(json); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
.../main/java/io/github/sebastiantoepfer/ddd/media/json/printable/StringJsonMappedValue.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,29 @@ | ||
package io.github.sebastiantoepfer.ddd.media.json.printable; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.ddd.common.Printable; | ||
import jakarta.json.JsonString; | ||
|
||
class StringJsonMappedValue implements JsonMappedValue { | ||
|
||
private final JsonString value; | ||
|
||
public StringJsonMappedValue(final JsonString value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public Printable toPrintable(final String name) { | ||
return new Printable() { | ||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(name, toValue()); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public String toValue() { | ||
return value.getString(); | ||
} | ||
} |
Oops, something went wrong.