#587 - Refactor - Change JsonContext.getScalar() to JsonContext.writeScalar()

This commit is contained in:
Robin Bygrave committed 2016-03-03 23:38:09 +13:00
1 parent 290ca6ba90
commit 53dd810b9a
11 files changed
+48 -84

No files matched your search

@@ -7,7 +7,6 @@ import com.avaje.ebean.text.json.EJson;
import com.avaje.ebean.text.json.JsonContext;
import com.avaje.ebean.text.json.JsonIOException;
import com.avaje.ebean.text.json.JsonReadOptions;
import com.avaje.ebean.text.json.JsonScalar;
import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
import com.avaje.ebean.text.json.JsonWriteOptions;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
@@ -51,16 +50,19 @@ public class DJsonContext implements JsonContext {
private final JsonConfig.Include defaultInclude;
private final DJsonScalar jsonScalar;
public DJsonContext(SpiEbeanServer server, JsonFactory jsonFactory, TypeManager typeManager) {
this.server = server;
this.typeManager = typeManager;
this.jsonFactory = (jsonFactory != null) ? jsonFactory : new JsonFactory();
this.defaultObjectMapper = this.server.getServerConfig().getObjectMapper();
this.defaultInclude = this.server.getServerConfig().getJsonInclude();
this.jsonScalar = new DJsonScalar(typeManager);
}
public JsonScalar getScalar(JsonGenerator generator) {
return new DefaultJsonScalar(typeManager, new WriteJson(generator, defaultInclude));
public void writeScalar(JsonGenerator generator, Object scalarValue) throws IOException {
jsonScalar.write(generator, scalarValue);
}
public boolean isSupportedType(Type genericType) {
@@ -1,39 +1,34 @@
package com.avaje.ebeaninternal.server.text.json;
import com.avaje.ebean.text.json.JsonScalar;
import com.avaje.ebeaninternal.server.type.ScalarType;
import com.avaje.ebeaninternal.server.type.TypeManager;
import com.fasterxml.jackson.core.JsonGenerator;
import java.io.IOException;
/**
* Default implementation of JsonScalar.
*/
public class DefaultJsonScalar implements JsonScalar {
public class DJsonScalar {
private final TypeManager typeManager;
private final WriteJson writeJson;
public DefaultJsonScalar(TypeManager typeManager, WriteJson writeJson) {
public DJsonScalar(TypeManager typeManager) {
this.typeManager = typeManager;
this.writeJson = writeJson;
}
@Override
@SuppressWarnings("unchecked")
public void write(Object value) throws IOException {
public void write(JsonGenerator gen, Object value) throws IOException {
if (value instanceof String) {
writeJson.writeString((String)value);
gen.writeString((String)value);
} else {
ScalarType scalarType = typeManager.getScalarType(value.getClass());
if (scalarType == null) {
throw new IllegalArgumentException("unhandled type " + value.getClass());
}
scalarType.jsonWrite(writeJson.gen(), value);
scalarType.jsonWrite(gen, value);
}
}
}