Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#38 - toString overrides. #39

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/com/vzurauskas/nereides/javax/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface Json {
* }
* </pre>
*/
public final class Of implements Json {
final class Of implements Json {
private final Json origin;

/**
Expand Down Expand Up @@ -124,5 +124,10 @@ private Of(Json json) {
public InputStream bytes() {
return origin.bytes();
}

@Override
public String toString() {
return new String(new ByteArray(this).value());
}
}
}
17 changes: 11 additions & 6 deletions src/main/java/com/vzurauskas/nereides/javax/MutableJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ private MutableJson(JsonObject base, JsonPatchBuilder patch) {
this.patch = patch;
}

@Override
public InputStream bytes() {
this.base = patch.build().apply(base);
return new Json.Of(base).bytes();
}

/**
* Add a {@code String} field to this JSON.
* @param name Name of the field.
Expand Down Expand Up @@ -113,4 +107,15 @@ public MutableJson with(String name, Json value) {
patch.add('/' + name, new SmartJson(value).jsonStructure());
return this;
}

@Override
public InputStream bytes() {
this.base = patch.build().apply(base);
return new Json.Of(base).bytes();
}

@Override
public String toString() {
return new String(new ByteArray(this).value());
}
}
15 changes: 10 additions & 5 deletions src/main/java/com/vzurauskas/nereides/javax/SmartJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ private SmartJson(Json origin, Unchecked<JsonStructure> structure) {
this.structure = structure;
}

@Override
public InputStream bytes() {
return origin.bytes();
}

/**
* Represent this JSON in textual form.
* @return String representing this JSON in textual form.
Expand Down Expand Up @@ -250,4 +245,14 @@ public SmartJson at(String path) {
public boolean isMissing() {
return byteArray().length == 0;
}

@Override
public InputStream bytes() {
return origin.bytes();
}

@Override
public String toString() {
return new String(new ByteArray(this).value());
}
}
9 changes: 9 additions & 0 deletions src/test/java/com/vzurauskas/nereides/javax/JsonOfTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vzurauskas.nereides.javax;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -86,4 +87,12 @@ void understandsArrays() {
new ByteArray(new Json.Of(string)).value()
);
}

@Test
void toStringWorksEvenIfMalformed() {
assertEquals(
"malformed",
new Json.Of("malformed").toString()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ void buildsOnBase() {
).with("nereid", new EmptyJson())
).affirm();
}

@Test
void toStringOnEmpty() {
assertEquals(
"{}",
new MutableJson().toString()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,12 @@ void knowsIfMissing() {
void knowsIfNotMissing() {
assertFalse(new SmartJson(new Json.Of("{}")).isMissing());
}

@Test
void toStringWorksOfMalformed() {
assertEquals(
"malformed",
new SmartJson(new Json.Of("malformed")).toString()
);
}
}