Skip to content

Commit

Permalink
Merge pull request #33 from sebastian-toepfer/printable_json
Browse files Browse the repository at this point in the history
add posibility to print a json object
  • Loading branch information
sebastian-toepfer authored Mar 12, 2023
2 parents b06ed8a + e81b639 commit 4770ca2
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 0 deletions.
12 changes: 12 additions & 0 deletions media-json-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand All @@ -37,6 +42,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>media-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
Expand Down
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();
}
}
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());
}
};
}
}
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());
}
};
}
}
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();
}
}
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();
}
}
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());
}
}
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);
}
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;
}
}
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;
}
};
}
}
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);
}
}
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();
}
}
Loading

0 comments on commit 4770ca2

Please sign in to comment.