Skip to content

Commit

Permalink
Add toString and toPrettyString method for JsonValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Apr 15, 2021
1 parent 895ca12 commit 2dce77b
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.syntaxphoenix.syntaxapi.json;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

import com.syntaxphoenix.syntaxapi.json.io.JsonWriter;
import com.syntaxphoenix.syntaxapi.json.value.JsonBigDecimal;
import com.syntaxphoenix.syntaxapi.json.value.JsonBigInteger;
import com.syntaxphoenix.syntaxapi.json.value.JsonByte;
Expand All @@ -17,6 +19,9 @@

public abstract class JsonValue<E> {

private static final JsonWriter PRETTY = new JsonWriter().setPretty(true);
private static final JsonWriter UNPRETTY = new JsonWriter();

@SuppressWarnings("unchecked")
public static <E> JsonValue<E> fromPrimitive(E primitive) {
if (primitive == null) {
Expand Down Expand Up @@ -65,4 +70,21 @@ public boolean isPrimitive() {
return getType().isPrimitive();
}

@Override
public String toString() {
try {
return UNPRETTY.toString(this);
} catch (IOException e) {
return "";
}
}

public String toPrettyString() {
try {
return PRETTY.toString(this);
} catch (IOException e) {
return "";
}
}

}

0 comments on commit 2dce77b

Please sign in to comment.