e.java.JsonStringEncoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of e-java Show documentation
Show all versions of e-java Show documentation
A zero-dependency micro library to deal with errors
package e.java;
import java.util.Map;
import java.util.StringJoiner;
public final class JsonStringEncoder implements Encoder {
private static JsonStringEncoder instance;
private JsonStringEncoder() {}
public static JsonStringEncoder get() {
if (instance == null) {
instance = new JsonStringEncoder();
}
return instance;
}
@Override public String encode(E e) {
StringJoiner joiner = new StringJoiner(",", "{", "}");
if (e.hasName()) joiner.add(String.format("\"name\":\"%s\"", escape(e.name())));
if (e.hasMessage()) joiner.add(String.format("\"message\":\"%s\"", escape(e.message())));
if (e.hasCode()) joiner.add(String.format("\"code\":%d", e.code()));;
if (e.hasCause()) joiner.add(String.format("\"cause\":%s", encodeCause(e)));
if (e.hasData()) joiner.add(String.format("\"data\":%s", encodeData(e)));
return joiner.toString();
}
private String encodeData(E e) {
StringJoiner joiner = new StringJoiner(",", "{", "}");
for (Map.Entry entry : e.data().entrySet()) {
joiner.add(String.format("\"%s\":\"%s\"", escape(entry.getKey()), escape(entry.getValue())));
}
return joiner.toString();
}
private String encodeCause(E e) {
return e.cause() == null ? "null" : String.format("\"%s\"", escape(e.cause().getMessage()));
}
private String escape(String s) {
return s.replace("\"", "\\\"");
}
}