feat(serialize): rework Transport api for writing
This commit is contained in:
parent
833494346e
commit
1580c3f98d
|
@ -7,6 +7,7 @@ import io.gitlab.jfronny.commons.serialize.Transport;
|
|||
import io.gitlab.jfronny.commons.serialize.databind.api.TypeToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -21,8 +22,10 @@ public class DatabindSerializer<TEx extends Throwable, Reader extends SerializeR
|
|||
|
||||
@Override
|
||||
public String serialize(Object object) throws IOException {
|
||||
try {
|
||||
return transport.write(writer -> mapper.serialize(object, writer));
|
||||
try (StringWriter sw = new StringWriter(); Writer writer = transport.createWriter(sw)) {
|
||||
mapper.serialize(object, writer);
|
||||
writer.close();
|
||||
return sw.toString();
|
||||
} catch (Throwable e) {
|
||||
throw e instanceof IOException x ? x : new IOException(e);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import io.gitlab.jfronny.commons.serialize.Transport;
|
|||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
public class JsonTransport implements Transport<IOException, JsonReader, JsonWriter> {
|
||||
@Override
|
||||
|
@ -13,11 +14,8 @@ public class JsonTransport implements Transport<IOException, JsonReader, JsonWri
|
|||
}
|
||||
|
||||
@Override
|
||||
public String write(PerformWrite<IOException, JsonWriter> write) throws IOException {
|
||||
try (StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw)) {
|
||||
write.write(writer);
|
||||
return sw.toString();
|
||||
}
|
||||
public JsonWriter createWriter(Writer target) throws IOException {
|
||||
return new JsonWriter(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -9,12 +9,7 @@ public interface Transport<TEx extends Throwable, Reader extends SerializeReader
|
|||
return createReader(new StringReader(source));
|
||||
}
|
||||
|
||||
String write(PerformWrite<TEx, Writer> write) throws TEx;
|
||||
Writer createWriter(java.io.Writer target) throws TEx;
|
||||
|
||||
String getFormatMime();
|
||||
|
||||
@FunctionalInterface
|
||||
interface PerformWrite<TEx extends Throwable, W extends SerializeWriter<TEx, W>> {
|
||||
void write(W writer) throws TEx, MalformedDataException;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import io.gitlab.jfronny.muscript.data.additional.context.Scope;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
|
||||
public class JsonLib {
|
||||
public static Scope addTo(Scope scope) {
|
||||
|
@ -22,13 +23,14 @@ public class JsonLib {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String write(PerformWrite<IOException, JsonWriter> write) throws IOException {
|
||||
return super.write(writer -> write.write(writer.setLenient(false)
|
||||
public JsonWriter createWriter(Writer target) throws IOException {
|
||||
return super.createWriter(target)
|
||||
.setLenient(false)
|
||||
.setNewline("")
|
||||
.setIndent("")
|
||||
.setSerializeSpecialFloatingPointValues(true)
|
||||
.setSerializeNulls(true)
|
||||
.setOmitQuotes(false)));
|
||||
.setOmitQuotes(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,13 +43,14 @@ public class JsonLib {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String write(PerformWrite<IOException, JsonWriter> write) throws IOException {
|
||||
return super.write(writer -> write.write(writer.setLenient(true)
|
||||
public JsonWriter createWriter(Writer target) throws IOException {
|
||||
return super.createWriter(target)
|
||||
.setLenient(true)
|
||||
.setNewline("\n")
|
||||
.setIndent(" ")
|
||||
.setSerializeSpecialFloatingPointValues(true)
|
||||
.setSerializeNulls(true)
|
||||
.setOmitQuotes(true)));
|
||||
.setOmitQuotes(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,8 @@ import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
|
|||
import io.gitlab.jfronny.muscript.data.dynamic.type.DType;
|
||||
import io.gitlab.jfronny.muscript.json.impl.DynamicTypeAdapter;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import static io.gitlab.jfronny.muscript.data.dynamic.type.DSL.*;
|
||||
import static io.gitlab.jfronny.muscript.data.dynamic.type.DSL.BOOL;
|
||||
|
||||
|
@ -42,7 +44,11 @@ public class TransportLib<TEx extends Throwable, Reader extends SerializeReader<
|
|||
Dynamic source = args.get(0);
|
||||
boolean lenient = args.size() > 1 && args.get(1).asBool().getValue();
|
||||
try {
|
||||
return DFinal.of((lenient ? lenientTransport : strictTransport).write(writer -> adapter.serialize(source, writer)));
|
||||
try (StringWriter sw = new StringWriter(); Writer writer = (lenient ? lenientTransport : strictTransport).createWriter(sw)) {
|
||||
adapter.serialize(source, writer);
|
||||
writer.close();
|
||||
return DFinal.of(sw.toString());
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
throw new IntentionalException("Could not serialize value: " + e.getMessage());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue