
com.dslplatform.json.BinaryConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-json Show documentation
Show all versions of dsl-json Show documentation
DSL Platform compatible Java JSON library (https://dsl-platform.com)
The newest version!
package com.dslplatform.json;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
public abstract class BinaryConverter {
public static final JsonReader.ReadObject READER = new JsonReader.ReadObject() {
@Nullable
@Override
public byte[] read(JsonReader reader) throws IOException {
return reader.wasNull() ? null : deserialize(reader);
}
};
public static final JsonWriter.WriteObject WRITER = (writer, value) -> serialize(value, writer);
public static final byte[] EMPTY_ARRAY = new byte[0];
static void registerDefault(DslJson json) {
json.registerReader(byte[].class, READER);
json.registerWriter(byte[].class, (writer, value) -> serialize(value, writer));
}
public static void serialize(@Nullable final byte[] value, final JsonWriter sw) {
if (value == null) {
sw.writeNull();
} else if (value.length == 0) {
sw.writeAscii("\"\"");
} else {
sw.writeBinary(value);
}
}
public static byte[] deserialize(final JsonReader reader) throws IOException {
return reader.readBase64();
}
@SuppressWarnings("unchecked")
public static ArrayList deserializeCollection(final JsonReader reader) throws IOException {
return reader.deserializeCollection(READER);
}
public static void deserializeCollection(final JsonReader reader, final Collection res) throws IOException {
reader.deserializeCollection(READER, res);
}
@SuppressWarnings("unchecked")
public static ArrayList deserializeNullableCollection(final JsonReader reader) throws IOException {
return reader.deserializeNullableCollection(READER);
}
public static void deserializeNullableCollection(final JsonReader reader, final Collection res) throws IOException {
reader.deserializeNullableCollection(READER, res);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy