#585 - Refactor of ScalarType jsonWrite() method such that it only takes JsonGenerator - part 1, remove field name

This commit is contained in:
Robin Bygrave
2016-03-03 19:51:07 +13:00
parent 5b10d06348
commit fc96685ed0
43 changed files with 105 additions and 111 deletions
@@ -11,5 +11,5 @@ public interface JsonScalar {
* Write the scalar type to JSON where the value can be any type known to Ebean
* including Enums, Java8 time types, Joda types, URL, URI etc.
*/
void write(String name, Object value) throws IOException;
void write(Object value) throws IOException;
}
@@ -135,6 +135,11 @@ public interface JsonWriter {
*/
void writeNumber(long value);
/**
* Write a double value.
*/
void writeNumber(double value);
/**
* Write a BigDecimal value (typically inside a list).
*/
@@ -119,11 +119,13 @@ public class ChangeJsonBuilder {
ValuePair value = entry.getValue();
Object newValue = value.getNewValue();
if (newValue != null) {
scalarWriter.write("new", newValue);
gen.writeFieldName("new");
scalarWriter.write(newValue);
}
Object oldValue = value.getOldValue();
if (oldValue != null) {
scalarWriter.write("old", oldValue);
gen.writeFieldName("old");
scalarWriter.write(oldValue);
}
gen.writeEndObject();
}
@@ -1243,7 +1243,8 @@ public class BeanProperty implements ElPropertyValue, Property {
writeJson.writeNullField(name);
} else {
if (scalarType != null) {
scalarType.jsonWrite(writeJson, name, value);
writeJson.writeFieldName(name);
scalarType.jsonWrite(writeJson, value);
} else {
writeJson.writeValueUsingObjectMapper(name, value);
}
@@ -22,17 +22,17 @@ public class DefaultJsonScalar implements JsonScalar {
}
@Override
public void write(String name, Object value) throws IOException {
public void write(Object value) throws IOException {
if (value instanceof String) {
writeJson.writeStringField(name, (String)value);
writeJson.writeString((String)value);
} else {
ScalarType scalarType = (ScalarType)typeManager.getScalarType(value.getClass());
if (scalarType == null) {
throw new IllegalArgumentException("unhandled type " + value.getClass());
}
scalarType.jsonWrite(writeJson, name, value);
scalarType.jsonWrite(writeJson, value);
}
}
}
@@ -313,6 +313,15 @@ public class WriteJson implements JsonWriter {
}
}
@Override
public void writeNumber(double value) {
try {
generator.writeNumber(value);
} catch (IOException e) {
throw new JsonIOException(e);
}
}
@Override
public void writeNumber(BigDecimal value) {
try {
@@ -173,26 +173,25 @@ public final class CtCompoundType<V> implements ScalarDataReader<V> {
public void jsonWrite(WriteJson ctx, Object valueObject, String propertyName) throws IOException {
ctx.beginAssocOne(propertyName, valueObject);
jsonWriteProps(ctx, valueObject, propertyName);
jsonWriteProps(ctx, valueObject);
ctx.endAssocOne();
}
@SuppressWarnings({"unchecked", "rawtypes"})
private void jsonWriteProps(WriteJson ctx, Object valueObject, String propertyName) throws IOException {
private void jsonWriteProps(WriteJson ctx, Object valueObject) throws IOException {
if (propertyName != null) {
ctx.gen().writeFieldName(propertyName);
}
ctx.gen().writeStartObject();
for (int i = 0; i < properties.length; i++) {
String propName = properties[i].getName();
Object value = properties[i].getValue((V) valueObject);
if (propReaders[i] instanceof CtCompoundType<?>) {
ctx.writeFieldName(propName);
((CtCompoundType) propReaders[i]).jsonWrite(ctx, value, propName);
} else {
((ScalarType) propReaders[i]).jsonWrite(ctx, propName, value);
ctx.writeFieldName(propName);
((ScalarType) propReaders[i]).jsonWrite(ctx, value);
}
}
@@ -208,6 +208,6 @@ public interface ScalarType<T> extends StringParser, StringFormatter, ScalarData
/**
* Write the value to the JsonGenerator.
*/
void jsonWrite(JsonWriter writer, String name, T value) throws IOException;
void jsonWrite(JsonWriter writer, T value) throws IOException;
}
@@ -36,6 +36,7 @@ public abstract class ScalarTypeBase<T> implements ScalarType<T> {
/**
* Just return 0.
*/
@Override
public int getLength() {
return 0;
}
@@ -78,9 +78,9 @@ public abstract class ScalarTypeBaseDate<T> extends ScalarTypeBase<T> {
}
}
public void jsonWrite(JsonWriter writer, String name, T value) throws IOException {
long millis = convertToMillis(value);
writer.writeNumberField(name, millis);
@Override
public void jsonWrite(JsonWriter writer, T value) throws IOException {
writer.writeNumber(convertToMillis(value));
}
@Override
@@ -104,21 +104,19 @@ public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, T value) throws IOException {
public void jsonWrite(JsonWriter writer, T value) throws IOException {
switch (mode) {
case ISO8601: {
writer.writeFieldName(name);
writer.gen().writeString(toJsonISO8601(value));
break;
}
case NANOS: {
writer.writeFieldName(name);
writer.gen().writeNumber(toJsonNanos(value));
break;
}
default: {
writer.gen().writeNumberField(name, convertToMillis(value));
writer.gen().writeNumber(convertToMillis(value));
}
}
}
@@ -129,8 +129,8 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, T value) throws IOException {
writer.writeStringField(name, format(value));
public void jsonWrite(JsonWriter writer, T value) throws IOException {
writer.writeString(format(value));
}
@Override
@@ -84,8 +84,8 @@ public class ScalarTypeBigDecimal extends ScalarTypeBase<BigDecimal> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, BigDecimal value) throws IOException {
writer.writeNumberField(name, value);
public void jsonWrite(JsonWriter writer, BigDecimal value) throws IOException {
writer.writeNumber(value);
}
@Override
@@ -336,8 +336,8 @@ public class ScalarTypeBoolean {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Boolean value) throws IOException {
writer.writeBooleanField(name, value);
public void jsonWrite(JsonWriter writer, Boolean value) throws IOException {
writer.writeBoolean(value);
}
@Override
@@ -43,7 +43,7 @@ public class ScalarTypeByte extends ScalarTypeBase<Byte> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Byte value) throws IOException {
public void jsonWrite(JsonWriter writer, Byte value) throws IOException {
throw new IOException("Not supported");
}
@@ -6,6 +6,7 @@ import com.avaje.ebean.text.json.JsonWriter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
@@ -42,8 +43,9 @@ public abstract class ScalarTypeBytesBase extends ScalarTypeBase<byte[]> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, byte[] value) throws IOException {
writer.writeBinaryField(name, value);
public void jsonWrite(JsonWriter writer, byte[] value) throws IOException {
ByteArrayInputStream is = new ByteArrayInputStream(value);
writer.writeBinary(is, value.length);
}
@Override
@@ -5,6 +5,7 @@ import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyType;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
@@ -65,8 +66,9 @@ public class ScalarTypeBytesEncrypted implements ScalarType<byte[]> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, byte[] value) throws IOException {
writer.writeBinaryField(name, value);
public void jsonWrite(JsonWriter writer, byte[] value) throws IOException {
ByteArrayInputStream is = new ByteArrayInputStream(value);
writer.writeBinary(is, value.length);
}
@Override
@@ -91,8 +91,8 @@ public class ScalarTypeDouble extends ScalarTypeBase<Double> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Double value) throws IOException {
writer.writeNumberField(name, value);
public void jsonWrite(JsonWriter writer, Double value) throws IOException {
writer.writeNumber(value);
}
@Override
@@ -108,8 +108,8 @@ public class ScalarTypeDuration extends ScalarTypeBase<Duration> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Duration value) throws IOException {
writer.writeStringField(name, value.toString());
public void jsonWrite(JsonWriter writer, Duration value) throws IOException {
writer.writeString(value.toString());
}
@Override
@@ -139,8 +139,8 @@ public class ScalarTypeEncryptedWrapper<T> implements ScalarType<T> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, T value) throws IOException {
wrapped.jsonWrite(writer, name, value);
public void jsonWrite(JsonWriter writer, T value) throws IOException {
wrapped.jsonWrite(writer, value);
}
@Override
@@ -238,8 +238,8 @@ public class ScalarTypeEnumStandard {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Object value) throws IOException {
writer.writeStringField(name, formatValue(value));
public void jsonWrite(JsonWriter writer, Object value) throws IOException {
writer.writeString(formatValue(value));
}
@Override
@@ -112,8 +112,7 @@ public class ScalarTypeFile extends ScalarTypeBase<File> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, File value) throws IOException {
writer.writeFieldName(name);
public void jsonWrite(JsonWriter writer, File value) throws IOException {
InputStream is = getInputStream(value);
writer.writeBinary(is, (int) value.length());
}
@@ -91,8 +91,8 @@ public class ScalarTypeFloat extends ScalarTypeBase<Float> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Float value) throws IOException {
writer.writeNumberField(name, value);
public void jsonWrite(JsonWriter writer, Float value) throws IOException {
writer.writeNumber(value);
}
@Override
@@ -91,8 +91,8 @@ public class ScalarTypeInteger extends ScalarTypeBase<Integer> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Integer value) throws IOException {
writer.writeNumberField(name, value);
public void jsonWrite(JsonWriter writer, Integer value) throws IOException {
writer.writeNumber(value);
}
@Override
@@ -72,8 +72,8 @@ public class ScalarTypeJodaLocalTime extends ScalarTypeBase<LocalTime> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, LocalTime value) throws IOException {
writer.writeStringField(name, value.toString());
public void jsonWrite(JsonWriter writer, LocalTime value) throws IOException {
writer.writeString(value.toString());
}
@Override
@@ -194,16 +194,8 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Map value) throws IOException {
// write the field name followed by the Map/JSON Object
if (value == null) {
writer.writeNullField(name);
} else {
if (!value.isEmpty() || writer.isIncludeEmpty()) {
writer.writeFieldName(name);
EJson.write(value, writer.gen());
}
}
public void jsonWrite(JsonWriter writer, Map value) throws IOException {
EJson.write(value, writer.gen());
}
@Override
@@ -206,14 +206,8 @@ public abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, JsonNode value) throws IOException {
// write the field name followed by the JsonNode object
if (value == null) {
writer.writeNullField(name);
} else {
writer.writeFieldName(name);
objectMapper.writeTree(writer.gen(), value);
}
public void jsonWrite(JsonWriter writer, JsonNode value) throws IOException {
objectMapper.writeTree(writer.gen(), value);
}
@Override
@@ -100,8 +100,8 @@ public class ScalarTypeLocalTime extends ScalarTypeBase<LocalTime> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, LocalTime value) throws IOException {
writer.writeStringField(name, value.toString());
public void jsonWrite(JsonWriter writer, LocalTime value) throws IOException {
writer.writeString(value.toString());
}
@Override
@@ -91,8 +91,8 @@ public class ScalarTypeLong extends ScalarTypeBase<Long> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Long value) throws IOException {
writer.writeNumberField(name, value);
public void jsonWrite(JsonWriter writer, Long value) throws IOException {
writer.writeNumber(value);
}
@Override
@@ -98,8 +98,8 @@ public class ScalarTypeMathBigInteger extends ScalarTypeBase<BigInteger> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, BigInteger value) throws IOException {
writer.writeNumberField(name, value.longValue());
public void jsonWrite(JsonWriter writer, BigInteger value) throws IOException {
writer.writeNumber(value.longValue());
}
@Override
@@ -125,8 +125,8 @@ public class ScalarTypeMonthDay extends ScalarTypeBase<MonthDay> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, MonthDay value) throws IOException {
writer.writeStringField(name, format(value));
public void jsonWrite(JsonWriter writer, MonthDay value) throws IOException {
writer.writeString(format(value));
}
@Override
@@ -113,14 +113,8 @@ public class ScalarTypePostgresHstore extends ScalarTypeBase<Map> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Map value) throws IOException {
// write the field name followed by the Map/JSON Object
if (value == null) {
writer.writeNullField(name);
} else {
writer.writeFieldName(name);
EJson.write(value, writer.gen());
}
public void jsonWrite(JsonWriter writer, Map value) throws IOException {
EJson.write(value, writer.gen());
}
@Override
@@ -92,8 +92,8 @@ public class ScalarTypeShort extends ScalarTypeBase<Short> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Short value) throws IOException {
writer.writeNumberField(name, value);
public void jsonWrite(JsonWriter writer, Short value) throws IOException {
writer.writeNumber(value);
}
@Override
@@ -91,8 +91,8 @@ public class ScalarTypeString extends ScalarTypeBase<String> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, String value) throws IOException {
writer.writeStringField(name, value);
public void jsonWrite(JsonWriter writer, String value) throws IOException {
writer.writeString(value);
}
@Override
@@ -93,8 +93,8 @@ public class ScalarTypeTime extends ScalarTypeBase<Time> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Time value) throws IOException {
writer.writeStringField(name, format(value));
public void jsonWrite(JsonWriter writer, Time value) throws IOException {
writer.writeString(format(value));
}
@Override
@@ -150,8 +150,8 @@ public class ScalarTypeUUIDBinary extends ScalarTypeBase<UUID> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, UUID value) throws IOException {
writer.writeStringField(name, value.toString());
public void jsonWrite(JsonWriter writer, UUID value) throws IOException {
writer.writeString(value.toString());
}
@Override
@@ -100,20 +100,12 @@ public class ScalarTypeUUIDNative extends ScalarTypeBase<UUID> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, UUID value) throws IOException {
// write the field name followed by the Map/JSON Object
if (value == null) {
writer.writeNullField(name);
} else {
writer.writeStringField(name, formatValue(value));
}
public void jsonWrite(JsonWriter writer, UUID value) throws IOException {
writer.writeString(formatValue(value));
}
@Override
public UUID jsonRead(JsonParser parser, JsonToken event) throws IOException {
// at this point the BeanProperty has read the START_OBJECT token
// to check for a null value. Pass the START_OBJECT token through to
// the EJson parsing so that it knows the first token has been read
String strValue = parser.getValueAsString();
return strValue == null ? null : parse(strValue);
}
@@ -189,9 +189,9 @@ public class ScalarTypeWrapper<B, S> implements ScalarType<B> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, B beanValue) throws IOException {
public void jsonWrite(JsonWriter writer, B beanValue) throws IOException {
S unwrapValue = converter.unwrapValue(beanValue);
scalarType.jsonWrite(writer, name, unwrapValue);
scalarType.jsonWrite(writer, unwrapValue);
}
@Override
@@ -95,8 +95,8 @@ public class ScalarTypeYear extends ScalarTypeBase<Year> {
}
@Override
public void jsonWrite(JsonWriter writer, String name, Year value) throws IOException {
writer.writeNumberField(name, value.getValue());
public void jsonWrite(JsonWriter writer, Year value) throws IOException {
writer.writeNumber(value.getValue());
}
@Override