mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
JSON Refactor - compound type support
This commit is contained in:
1 parent
b313809894
commit
58755a51a9
68 files changed
+334
-2849
No files matched your search
@@ -65,7 +65,6 @@ import com.avaje.ebean.meta.MetaBeanInfo;
|
||||
import com.avaje.ebean.meta.MetaInfoManager;
|
||||
import com.avaje.ebean.text.csv.CsvReader;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
import com.avaje.ebeaninternal.api.LoadBeanRequest;
|
||||
import com.avaje.ebeaninternal.api.LoadManyRequest;
|
||||
import com.avaje.ebeaninternal.api.ScopeTrans;
|
||||
@@ -1989,10 +1988,6 @@ public final class DefaultServer implements SpiEbeanServer {
|
||||
if (typeInfo == null) {
|
||||
return false;
|
||||
}
|
||||
Class<?> beanType = typeInfo.getBeanType();
|
||||
if (JsonElement.class.isAssignableFrom(beanType)) {
|
||||
return true;
|
||||
}
|
||||
return getBeanDescriptor(typeInfo.getBeanType()) != null;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,6 @@ import com.avaje.ebean.config.ExternalTransactionManager;
|
||||
import com.avaje.ebean.config.ServerConfig;
|
||||
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.api.ClassUtil;
|
||||
import com.avaje.ebeaninternal.api.SpiBackgroundExecutor;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.autofetch.AutoFetchManager;
|
||||
@@ -30,7 +28,6 @@ import com.avaje.ebeaninternal.server.query.DefaultRelationalQueryEngine;
|
||||
import com.avaje.ebeaninternal.server.resource.ResourceManager;
|
||||
import com.avaje.ebeaninternal.server.resource.ResourceManagerFactory;
|
||||
import com.avaje.ebeaninternal.server.text.json.DJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.DefaultJsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.transaction.DefaultTransactionScopeManager;
|
||||
import com.avaje.ebeaninternal.server.transaction.ExternalTransactionScopeManager;
|
||||
import com.avaje.ebeaninternal.server.transaction.JtaTransactionManager;
|
||||
@@ -133,17 +130,8 @@ public class InternalConfiguration {
|
||||
}
|
||||
|
||||
public JsonContext createJsonContext(SpiEbeanServer server) {
|
||||
|
||||
String s = serverConfig.getProperty("json.pretty", "false");
|
||||
boolean dfltPretty = "true".equalsIgnoreCase(s);
|
||||
|
||||
s = serverConfig.getProperty("json.jsonValueAdapter", null);
|
||||
|
||||
JsonValueAdapter va = new DefaultJsonValueAdapter();
|
||||
if (s != null) {
|
||||
va = (JsonValueAdapter) ClassUtil.newInstance(s, this.getClass());
|
||||
}
|
||||
return new DJsonContext(server, va, dfltPretty);
|
||||
|
||||
return new DJsonContext(server);
|
||||
}
|
||||
|
||||
public XmlConfig getXmlConfig() {
|
||||
|
||||
@@ -3,14 +3,17 @@ package com.avaje.ebeaninternal.server.deploy;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.json.stream.JsonParser;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.config.ScalarTypeConverter;
|
||||
import com.avaje.ebean.json.EJson;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyCompound;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyChainBuilder;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.ebeaninternal.server.query.SqlBeanLoad;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
import com.avaje.ebeaninternal.server.type.CtCompoundProperty;
|
||||
import com.avaje.ebeaninternal.server.type.CtCompoundPropertyElAdapter;
|
||||
@@ -178,14 +181,31 @@ public class BeanPropertyCompound extends BeanProperty {
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJson ctx, EntityBean bean) {
|
||||
|
||||
Object valueObject = getValueIntercept(bean);
|
||||
//FIXME: compoundType.jsonWrite(ctx, valueObject, name);
|
||||
if (!jsonSerialize) {
|
||||
return;
|
||||
}
|
||||
Object value = getValueIntercept(bean);
|
||||
if (value == null) {
|
||||
ctx.gen().writeNull(name);
|
||||
} else {
|
||||
compoundType.jsonWrite(ctx, value, name);
|
||||
}
|
||||
}
|
||||
|
||||
public void jsonRead(ReadJsonContext ctx, EntityBean bean){
|
||||
|
||||
Object objValue = compoundType.jsonRead(ctx);
|
||||
public void jsonRead(JsonParser ctx, EntityBean bean) {
|
||||
|
||||
if (!jsonDeserialize) {
|
||||
return;
|
||||
}
|
||||
|
||||
Object value = EJson.parsePartial(ctx);
|
||||
if (value == null) {
|
||||
setValue(bean, null);
|
||||
} else {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String,Object> map = (Map<String,Object>)value;
|
||||
Object objValue = compoundType.jsonConvert(map);
|
||||
setValue(bean, objValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,12 +19,10 @@ import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.json.EJson;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
import com.avaje.ebean.text.json.JsonReadOptions;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
@@ -41,14 +39,8 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
private final SpiEbeanServer server;
|
||||
|
||||
//private final JsonValueAdapter dfltValueAdapter;
|
||||
|
||||
private final boolean dfltPretty;
|
||||
|
||||
public DJsonContext(SpiEbeanServer server, JsonValueAdapter dfltValueAdapter, boolean dfltPretty) {
|
||||
public DJsonContext(SpiEbeanServer server) {
|
||||
this.server = server;
|
||||
//this.dfltValueAdapter = dfltValueAdapter;
|
||||
this.dfltPretty = dfltPretty;
|
||||
}
|
||||
|
||||
public boolean isSupportedType(Type genericType) {
|
||||
@@ -60,44 +52,29 @@ public class DJsonContext implements JsonContext {
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, String json) {
|
||||
return toBean(cls, new StringReader(json), null);
|
||||
return toBean(cls, new StringReader(json));
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, Reader jsonReader) {
|
||||
return toBean(cls, createReader(jsonReader), null);
|
||||
return toBean(cls, createReader(jsonReader));
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, String json, JsonReadOptions options) {
|
||||
return toBean(cls, new StringReader(json), options);
|
||||
}
|
||||
|
||||
public <T> T toBean(Class<T> cls, Reader jsonReader, JsonReadOptions options) {
|
||||
return toBean(cls, createReader(jsonReader), options);
|
||||
}
|
||||
|
||||
private <T> T toBean(Class<T> cls, JsonParser parser, JsonReadOptions options) {
|
||||
private <T> T toBean(Class<T> cls, JsonParser parser) {
|
||||
|
||||
BeanDescriptor<T> d = getDecriptor(cls);
|
||||
return d.jsonRead(parser, null);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, String json) {
|
||||
return toList(cls, new StringReader(json), null);
|
||||
return toList(cls, new StringReader(json));
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, String json, JsonReadOptions options) {
|
||||
return toList(cls, new StringReader(json), options);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, Reader jsonReader) {
|
||||
return toList(cls, createReader(jsonReader), null);
|
||||
return toList(cls, createReader(jsonReader));
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, Reader jsonReader, JsonReadOptions options) {
|
||||
return toList(cls, createReader(jsonReader), options);
|
||||
}
|
||||
|
||||
private <T> List<T> toList(Class<T> cls, JsonParser src, JsonReadOptions options) {
|
||||
private <T> List<T> toList(Class<T> cls, JsonParser src) {
|
||||
|
||||
try {
|
||||
BeanDescriptor<T> d = getDecriptor(cls);
|
||||
@@ -128,89 +105,63 @@ public class DJsonContext implements JsonContext {
|
||||
}
|
||||
}
|
||||
|
||||
public Object toObject(Type genericType, String json, JsonReadOptions options) {
|
||||
public Object toObject(Type genericType, String json) {
|
||||
|
||||
TypeInfo info = ParamTypeHelper.getTypeInfo(genericType);
|
||||
Class<?> beanType = info.getBeanType();
|
||||
if (JsonElement.class.isAssignableFrom(beanType)) {
|
||||
return InternalJsonParser.parse(json);
|
||||
}
|
||||
|
||||
ManyType manyType = info.getManyType();
|
||||
switch (manyType) {
|
||||
case NONE:
|
||||
return toBean(info.getBeanType(), json, options);
|
||||
return toBean(info.getBeanType(), json);
|
||||
|
||||
case LIST:
|
||||
return toList(info.getBeanType(), json, options);
|
||||
return toList(info.getBeanType(), json);
|
||||
|
||||
default:
|
||||
String msg = "ManyType " + manyType + " not supported yet";
|
||||
throw new TextException(msg);
|
||||
throw new TextException("Type " + manyType + " not supported");
|
||||
}
|
||||
}
|
||||
|
||||
public Object toObject(Type genericType, Reader json, JsonReadOptions options) {
|
||||
public Object toObject(Type genericType, Reader json) {
|
||||
|
||||
TypeInfo info = ParamTypeHelper.getTypeInfo(genericType);
|
||||
Class<?> beanType = info.getBeanType();
|
||||
if (JsonElement.class.isAssignableFrom(beanType)) {
|
||||
return InternalJsonParser.parse(json);
|
||||
}
|
||||
|
||||
ManyType manyType = info.getManyType();
|
||||
switch (manyType) {
|
||||
case NONE:
|
||||
return toBean(info.getBeanType(), json, options);
|
||||
return toBean(info.getBeanType(), json);
|
||||
|
||||
case LIST:
|
||||
return toList(info.getBeanType(), json, options);
|
||||
return toList(info.getBeanType(), json);
|
||||
|
||||
default:
|
||||
throw new TextException("ManyType " + manyType + " not supported");
|
||||
throw new TextException("Type " + manyType + " not supported");
|
||||
}
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer) {
|
||||
toJsonWriter(o, writer, dfltPretty, null, null);
|
||||
toJsonWriter(o, writer, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty) {
|
||||
toJsonWriter(o, writer, pretty, null, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty, JsonWriteOptions options) {
|
||||
toJsonWriter(o, writer, pretty, null, null);
|
||||
}
|
||||
|
||||
public void toJsonWriter(Object o, Writer writer, boolean pretty, JsonWriteOptions options, String callback) {
|
||||
public void toJsonWriter(Object o, Writer writer, JsonWriteOptions options) {
|
||||
JsonGenerator generator = Json.createGenerator(writer);
|
||||
toJsonInternal(o, generator, pretty, options, callback);
|
||||
toJsonInternal(o, generator, options);
|
||||
generator.close();
|
||||
}
|
||||
|
||||
public String toJsonString(Object o) {
|
||||
return toJsonString(o, dfltPretty, null);
|
||||
return toJsonString(o, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty) {
|
||||
return toJsonString(o, pretty, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty, JsonWriteOptions options) {
|
||||
return toJsonString(o, pretty, options, null);
|
||||
}
|
||||
|
||||
public String toJsonString(Object o, boolean pretty, JsonWriteOptions options, String callback) {
|
||||
public String toJsonString(Object o, JsonWriteOptions options) {
|
||||
StringWriter writer = new StringWriter(500);
|
||||
JsonGenerator gen = Json.createGenerator(writer);
|
||||
toJsonInternal(o, gen, pretty, options, callback);
|
||||
toJsonInternal(o, gen, options);
|
||||
gen.close();
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void toJsonInternal(Object o, JsonGenerator gen, boolean pretty, JsonWriteOptions options, String requestCallback) {
|
||||
private void toJsonInternal(Object o, JsonGenerator gen, JsonWriteOptions options) {
|
||||
|
||||
if (o == null) {
|
||||
gen.writeNull();
|
||||
@@ -224,10 +175,10 @@ public class DJsonContext implements JsonContext {
|
||||
// } else if (o instanceof JsonElement) {
|
||||
|
||||
} else if (o instanceof Map<?, ?>) {
|
||||
toJsonFromMap((Map<Object, Object>) o, gen, pretty, options, requestCallback);
|
||||
toJsonFromMap((Map<Object, Object>) o, gen, options);
|
||||
|
||||
} else if (o instanceof Collection<?>) {
|
||||
toJsonFromCollection((Collection<?>) o, null, gen, pretty, options, requestCallback);
|
||||
toJsonFromCollection((Collection<?>) o, null, gen, options);
|
||||
|
||||
} else if (o instanceof EntityBean) {
|
||||
BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
@@ -241,7 +192,7 @@ public class DJsonContext implements JsonContext {
|
||||
return new WriteJson(server, gen, pathProps);
|
||||
}
|
||||
|
||||
private <T> void toJsonFromCollection(Collection<T> c, String key, JsonGenerator gen, boolean pretty, JsonWriteOptions options, String requestCallback) {
|
||||
private <T> void toJsonFromCollection(Collection<T> c, String key, JsonGenerator gen, JsonWriteOptions options) {
|
||||
|
||||
if (key == null) {
|
||||
gen.writeStartArray();
|
||||
@@ -255,13 +206,12 @@ public class DJsonContext implements JsonContext {
|
||||
while (it.hasNext()) {
|
||||
T t = it.next();
|
||||
BeanDescriptor<?> d = getDecriptor(t.getClass());
|
||||
//writeJson.setBean();
|
||||
d.jsonWrite(writeJson, (EntityBean)t, null);
|
||||
}
|
||||
gen.writeEnd();
|
||||
}
|
||||
|
||||
private void toJsonFromMap(Map<Object, Object> map, JsonGenerator gen, boolean pretty, JsonWriteOptions options, String requestCallback) {
|
||||
private void toJsonFromMap(Map<Object, Object> map, JsonGenerator gen, JsonWriteOptions options) {
|
||||
|
||||
Set<Entry<Object, Object>> entrySet = map.entrySet();
|
||||
Iterator<Entry<Object, Object>> it = entrySet.iterator();
|
||||
@@ -277,14 +227,14 @@ public class DJsonContext implements JsonContext {
|
||||
gen.writeNull(key);
|
||||
} else {
|
||||
if (value instanceof Collection<?>) {
|
||||
toJsonFromCollection((Collection<?>) value, key, gen, pretty, options, requestCallback);
|
||||
toJsonFromCollection((Collection<?>) value, key, gen, options);
|
||||
|
||||
} else if (value instanceof EntityBean) {
|
||||
BeanDescriptor<?> d = getDecriptor(value.getClass());
|
||||
d.jsonWrite(writeJson,(EntityBean) value, key);
|
||||
|
||||
} else {
|
||||
throw new RuntimeException("TODO process primitive");
|
||||
EJson.write(entry, gen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,7 @@ import java.sql.Timestamp;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
|
||||
public class DefaultJsonValueAdapter implements JsonValueAdapter {
|
||||
public class DefaultJsonValueAdapter {//implements JsonValueAdapter {
|
||||
|
||||
private final SimpleDateFormat dateTimeProto;
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
|
||||
public class InternalJsonParser {
|
||||
|
||||
public static JsonElement parse(String s) {
|
||||
|
||||
ReadJsonSourceString src = new ReadJsonSourceString(s);
|
||||
ReadBasicJsonContext b = new ReadBasicJsonContext(src);
|
||||
return ReadJsonRawReader.readJsonElement(b);
|
||||
}
|
||||
|
||||
public static JsonElement parse(Reader s) {
|
||||
|
||||
ReadJsonSourceReader src = new ReadJsonSourceReader(s, 512, 256);
|
||||
ReadBasicJsonContext b = new ReadBasicJsonContext(src);
|
||||
return ReadJsonRawReader.readJsonElement(b);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,273 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
|
||||
public class ReadBasicJsonContext implements ReadJsonInterface {
|
||||
|
||||
private final ReadJsonSource src;
|
||||
|
||||
private char tokenStart;
|
||||
private String tokenKey;
|
||||
private boolean pushedTokenKey;
|
||||
|
||||
public ReadBasicJsonContext(ReadJsonSource src) {
|
||||
this.src = src;
|
||||
}
|
||||
|
||||
/**
|
||||
* Push the current token key back onto the 'stack'.
|
||||
*/
|
||||
public void pushTokenKey() {
|
||||
pushedTokenKey = true;
|
||||
}
|
||||
|
||||
public char getToken() {
|
||||
return tokenStart;
|
||||
}
|
||||
|
||||
public String getTokenKey() {
|
||||
return tokenKey;
|
||||
}
|
||||
|
||||
public boolean isTokenKey() {
|
||||
return '\"' == tokenStart;
|
||||
}
|
||||
|
||||
public boolean isTokenObjectEnd() {
|
||||
return '}' == tokenStart;
|
||||
}
|
||||
|
||||
public boolean readObjectBegin() {
|
||||
readNextToken();
|
||||
if ('{' == tokenStart){
|
||||
return true;
|
||||
} else if ('n' == tokenStart) {
|
||||
return false;
|
||||
} else if (']' == tokenStart) {
|
||||
// an empty array
|
||||
return false;
|
||||
}
|
||||
throw new RuntimeException("Expected object begin at "+src.getErrorHelp());
|
||||
}
|
||||
|
||||
public boolean readKeyNext() {
|
||||
readNextToken();
|
||||
if ('\"' == tokenStart){
|
||||
return true;
|
||||
} else if ('}' == tokenStart) {
|
||||
return false;
|
||||
}
|
||||
throw new RuntimeException("Expected '\"' or '}' at "+src.getErrorHelp());
|
||||
}
|
||||
|
||||
public boolean readValueNext() {
|
||||
readNextToken();
|
||||
if (',' == tokenStart){
|
||||
return true;
|
||||
} else if ('}' == tokenStart) {
|
||||
return false;
|
||||
}
|
||||
throw new RuntimeException("Expected ',' or '}' at "+src.getErrorHelp()+" but got "+tokenStart);
|
||||
}
|
||||
|
||||
public boolean readArrayBegin() {
|
||||
readNextToken();
|
||||
if ('[' == tokenStart){
|
||||
return true;
|
||||
} else if ('n' == tokenStart) {
|
||||
return false;
|
||||
}
|
||||
throw new RuntimeException("Expected array begin at "+src.getErrorHelp());
|
||||
}
|
||||
|
||||
public boolean readArrayNext() {
|
||||
readNextToken();
|
||||
if (',' == tokenStart){
|
||||
return true;
|
||||
}
|
||||
if (']' == tokenStart){
|
||||
return false;
|
||||
}
|
||||
throw new RuntimeException("Expected ',' or ']' at "+src.getErrorHelp());
|
||||
}
|
||||
|
||||
public void readNextToken() {
|
||||
|
||||
if (pushedTokenKey) {
|
||||
// Do nothing
|
||||
pushedTokenKey = false;
|
||||
return;
|
||||
}
|
||||
|
||||
ignoreWhiteSpace();
|
||||
|
||||
tokenStart = src.nextChar("EOF finding next token");
|
||||
switch (tokenStart) {
|
||||
case '"':
|
||||
internalReadKey();
|
||||
break;
|
||||
case '{': break;
|
||||
case '}': break;
|
||||
case '[': break; // not expected
|
||||
case ']': break; // not expected
|
||||
case ',': break; // not expected
|
||||
case ':': break; // not expected
|
||||
case 'n':
|
||||
internalReadNull();
|
||||
break; // not expected
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Unexpected tokenStart["+tokenStart+"] "+src.getErrorHelp());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String readQuotedValue() {
|
||||
|
||||
boolean escape = false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
do {
|
||||
char ch = src.nextChar("EOF reading quoted value");
|
||||
if (escape) {
|
||||
// in escape mode so just append the character
|
||||
escape = false;
|
||||
switch (ch) {
|
||||
case 'n':
|
||||
sb.append('\n');
|
||||
break;
|
||||
case 'r':
|
||||
sb.append('\r');
|
||||
break;
|
||||
case 't':
|
||||
sb.append('\t');
|
||||
break;
|
||||
case 'f':
|
||||
sb.append('\f');
|
||||
break;
|
||||
case 'b':
|
||||
sb.append('\b');
|
||||
break;
|
||||
case '"':
|
||||
sb.append('"');
|
||||
break;
|
||||
case 'u':
|
||||
String msg = "EOF reading unicode value";
|
||||
char c1 = src.nextChar(msg);
|
||||
char c2 = src.nextChar(msg);
|
||||
char c3 = src.nextChar(msg);
|
||||
char c4 = src.nextChar(msg);
|
||||
char u = (char) Integer.parseInt(""+c1+c2+c3+c4, 16);
|
||||
sb.append(u);
|
||||
break;
|
||||
|
||||
default:
|
||||
sb.append('\\');
|
||||
sb.append(ch);
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
switch (ch) {
|
||||
case '\\':
|
||||
// put into 'escape' mode for next character
|
||||
escape = true;
|
||||
break;
|
||||
case '"':
|
||||
return sb.toString();
|
||||
|
||||
default:
|
||||
sb.append(ch);
|
||||
}
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
|
||||
public String readUnquotedValue(char c) {
|
||||
String v = readUnquotedValueRaw(c);
|
||||
if ("null".equals(v)){
|
||||
return null;
|
||||
} else {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
private String readUnquotedValueRaw(char c) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(c);
|
||||
|
||||
do {
|
||||
tokenStart = src.nextChar("EOF reading unquoted value");
|
||||
switch (tokenStart) {
|
||||
case ',':
|
||||
src.back();
|
||||
return sb.toString();
|
||||
|
||||
case '}':
|
||||
src.back();
|
||||
return sb.toString();
|
||||
|
||||
case ' ':
|
||||
return sb.toString();
|
||||
|
||||
case '\t':
|
||||
return sb.toString();
|
||||
|
||||
case '\r':
|
||||
return sb.toString();
|
||||
|
||||
case '\n':
|
||||
return sb.toString();
|
||||
|
||||
default:
|
||||
sb.append(tokenStart);
|
||||
}
|
||||
|
||||
} while (true);
|
||||
|
||||
}
|
||||
|
||||
private void internalReadNull() {
|
||||
|
||||
StringBuilder sb = new StringBuilder(4);
|
||||
sb.append(tokenStart);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
char c = src.nextChar("EOF reading null ");
|
||||
sb.append(c);
|
||||
}
|
||||
if (!"null".equals(sb.toString())){
|
||||
throw new TextException("Expected 'null' but got "+sb.toString()+" "+src.getErrorHelp());
|
||||
}
|
||||
}
|
||||
|
||||
private void internalReadKey() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
do {
|
||||
char c = src.nextChar("EOF reading key");
|
||||
if ('\"' == c){
|
||||
tokenKey = sb.toString();
|
||||
break;
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
} while (true);
|
||||
|
||||
ignoreWhiteSpace();
|
||||
|
||||
char c = src.nextChar("EOF reading ':'");
|
||||
if (':' != c){
|
||||
throw new TextException("Expected to find colon after key at "+(src.pos()-1)+" but found ["+c+"]"+src.getErrorHelp());
|
||||
}
|
||||
}
|
||||
|
||||
public void ignoreWhiteSpace() {
|
||||
src.ignoreWhiteSpace();
|
||||
}
|
||||
|
||||
public char nextChar() {
|
||||
tokenStart = src.nextChar("EOF getting nextChar for raw json");
|
||||
return tokenStart;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,177 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
import com.avaje.ebean.text.json.JsonReadBeanVisitor;
|
||||
import com.avaje.ebean.text.json.JsonReadOptions;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.util.ArrayStack;
|
||||
|
||||
public class ReadJsonContext extends ReadBasicJsonContext {
|
||||
|
||||
private final Map<String, JsonReadBeanVisitor<?>> visitorMap;
|
||||
|
||||
private final JsonValueAdapter valueAdapter;
|
||||
|
||||
private final PathStack pathStack;
|
||||
|
||||
private final ArrayStack<ReadBeanState> beanState;
|
||||
private ReadBeanState currentState;
|
||||
|
||||
public ReadJsonContext(ReadJsonSource src, JsonValueAdapter dfltValueAdapter, JsonReadOptions options) {
|
||||
super(src);
|
||||
this.beanState = new ArrayStack<ReadBeanState>();
|
||||
if (options == null){
|
||||
this.valueAdapter = dfltValueAdapter;
|
||||
this.visitorMap = null;
|
||||
this.pathStack = null;
|
||||
} else {
|
||||
this.valueAdapter = getValueAdapter(dfltValueAdapter, options.getValueAdapter());
|
||||
this.visitorMap = options.getVisitorMap();
|
||||
this.pathStack = (visitorMap == null || visitorMap.isEmpty()) ? null : new PathStack();
|
||||
}
|
||||
}
|
||||
|
||||
private JsonValueAdapter getValueAdapter(JsonValueAdapter dfltValueAdapter, JsonValueAdapter valueAdapter) {
|
||||
return valueAdapter == null ? dfltValueAdapter : valueAdapter;
|
||||
}
|
||||
|
||||
public JsonValueAdapter getValueAdapter() {
|
||||
return valueAdapter;
|
||||
}
|
||||
|
||||
public String readScalarValue() {
|
||||
|
||||
ignoreWhiteSpace();
|
||||
|
||||
char prevChar = nextChar();//"EOF reading scalarValue?");
|
||||
if ('"' == prevChar){
|
||||
return readQuotedValue();
|
||||
} else {
|
||||
return readUnquotedValue(prevChar);
|
||||
}
|
||||
}
|
||||
|
||||
public void pushBean(Object bean, String path, BeanDescriptor<?> beanDescriptor){
|
||||
currentState = new ReadBeanState(bean, beanDescriptor);
|
||||
beanState.push(currentState);
|
||||
if (pathStack != null){
|
||||
pathStack.pushPathKey(path);
|
||||
}
|
||||
}
|
||||
|
||||
public ReadBeanState popBeanState() {
|
||||
if (pathStack != null){
|
||||
String path = pathStack.peekWithNull();
|
||||
JsonReadBeanVisitor<?> beanVisitor = visitorMap.get(path);
|
||||
if (beanVisitor != null){
|
||||
currentState.visit(beanVisitor);
|
||||
}
|
||||
pathStack.pop();
|
||||
}
|
||||
|
||||
// return the current ReadBeanState as we can't call setLoadedState()
|
||||
// yet. We might bind master/detail beans together via mappedBy property
|
||||
// so wait until after that before calling ReadBeanStatesetLoadedState();
|
||||
ReadBeanState s = currentState;
|
||||
|
||||
beanState.pop();
|
||||
currentState = beanState.peekWithNull();
|
||||
return s;
|
||||
}
|
||||
|
||||
public void setProperty(String propertyName){
|
||||
currentState.setLoaded(propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Got a key that doesn't map to a known property so read the json value
|
||||
* which could be json primitive, object or array.
|
||||
* <p>
|
||||
* Provide these values to a JsonReadBeanVisitor if registered.
|
||||
* </p>
|
||||
*/
|
||||
public JsonElement readUnmappedJson(String key) {
|
||||
|
||||
JsonElement rawJsonValue = ReadJsonRawReader.readJsonElement(this);
|
||||
if (visitorMap != null){
|
||||
currentState.addUnmappedJson(key, rawJsonValue);
|
||||
}
|
||||
return rawJsonValue;
|
||||
}
|
||||
|
||||
public static class ReadBeanState implements PropertyChangeListener {
|
||||
|
||||
private final Object bean;
|
||||
private final BeanDescriptor<?> beanDescriptor;
|
||||
private final EntityBeanIntercept ebi;
|
||||
private final Set<String> loadedProps;
|
||||
private Map<String,JsonElement> unmapped;
|
||||
|
||||
private ReadBeanState(Object bean, BeanDescriptor<?> beanDescriptor) {
|
||||
this.bean = bean;
|
||||
this.beanDescriptor = beanDescriptor;
|
||||
if (bean instanceof EntityBean){
|
||||
this.ebi = ((EntityBean)bean)._ebean_getIntercept();
|
||||
this.loadedProps = new HashSet<String>();
|
||||
} else {
|
||||
this.ebi = null;
|
||||
this.loadedProps = null;
|
||||
}
|
||||
}
|
||||
public String toString(){
|
||||
return bean.getClass().getSimpleName()+" loaded:"+loadedProps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a loaded/set property to the set of loadedProps.
|
||||
*/
|
||||
public void setLoaded(String propertyName){
|
||||
if (ebi != null){
|
||||
loadedProps.add(propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
private void addUnmappedJson(String key, JsonElement value){
|
||||
if (unmapped == null){
|
||||
unmapped = new LinkedHashMap<String, JsonElement>();
|
||||
}
|
||||
unmapped.put(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> void visit(JsonReadBeanVisitor<T> beanVisitor) {
|
||||
// listen for property change events so that
|
||||
// we can update the loadedProps if necessary
|
||||
if (ebi != null){
|
||||
ebi.addPropertyChangeListener(this);
|
||||
}
|
||||
beanVisitor.visit((T)bean, unmapped);
|
||||
if (ebi != null){
|
||||
ebi.removePropertyChangeListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void propertyChange(PropertyChangeEvent evt) {
|
||||
String propName = evt.getPropertyName();
|
||||
loadedProps.add(propName);
|
||||
}
|
||||
|
||||
public Object getBean() {
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
public interface ReadJsonInterface {
|
||||
|
||||
public void ignoreWhiteSpace();
|
||||
|
||||
public char nextChar();
|
||||
|
||||
public String getTokenKey();
|
||||
|
||||
public boolean readKeyNext();
|
||||
|
||||
public boolean readValueNext();
|
||||
|
||||
public boolean readArrayNext();
|
||||
|
||||
public String readQuotedValue();
|
||||
|
||||
public String readUnquotedValue(char c);
|
||||
|
||||
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
import com.avaje.ebean.text.json.JsonElementArray;
|
||||
import com.avaje.ebean.text.json.JsonElementBoolean;
|
||||
import com.avaje.ebean.text.json.JsonElementNull;
|
||||
import com.avaje.ebean.text.json.JsonElementNumber;
|
||||
import com.avaje.ebean.text.json.JsonElementObject;
|
||||
import com.avaje.ebean.text.json.JsonElementString;
|
||||
|
||||
|
||||
|
||||
public class ReadJsonRawReader {
|
||||
|
||||
public static JsonElement readJsonElement(ReadJsonInterface ctx) {
|
||||
return new ReadJsonRawReader(ctx).readJsonElement();
|
||||
}
|
||||
|
||||
private final ReadJsonInterface ctx;
|
||||
|
||||
private ReadJsonRawReader(ReadJsonInterface ctx){
|
||||
this.ctx = ctx;
|
||||
}
|
||||
|
||||
private JsonElement readJsonElement() {
|
||||
return readValue();
|
||||
}
|
||||
|
||||
private JsonElement readValue() {
|
||||
|
||||
ctx.ignoreWhiteSpace();
|
||||
|
||||
char c = ctx.nextChar();
|
||||
|
||||
switch (c) {
|
||||
case '{':
|
||||
return readObject();
|
||||
|
||||
case '[':
|
||||
return readArray();
|
||||
|
||||
case '"':
|
||||
return readString();
|
||||
|
||||
default:
|
||||
return readUnquoted(c);
|
||||
}
|
||||
}
|
||||
|
||||
private JsonElement readArray() {
|
||||
|
||||
JsonElementArray a = new JsonElementArray();
|
||||
|
||||
do {
|
||||
JsonElement value = readValue();
|
||||
a.add(value);
|
||||
if (!ctx.readArrayNext()){
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
private JsonElement readObject() {
|
||||
|
||||
JsonElementObject o = new JsonElementObject();
|
||||
|
||||
do {
|
||||
if (!ctx.readKeyNext()){
|
||||
break;
|
||||
} else {
|
||||
// we read a property key ...
|
||||
String key = ctx.getTokenKey();
|
||||
JsonElement value = readValue();
|
||||
|
||||
o.put(key, value);
|
||||
|
||||
if (!ctx.readValueNext()){
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
private JsonElement readString() {
|
||||
String s = ctx.readQuotedValue();
|
||||
return new JsonElementString(s);
|
||||
}
|
||||
|
||||
private JsonElement readUnquoted(char c) {
|
||||
String s = ctx.readUnquotedValue(c);
|
||||
if ("null".equals(s)){
|
||||
return JsonElementNull.NULL;
|
||||
|
||||
} else if ("true".equals(s)){
|
||||
return JsonElementBoolean.TRUE;
|
||||
|
||||
} else if ("false".equals(s)) {
|
||||
return JsonElementBoolean.FALSE;
|
||||
|
||||
}
|
||||
return new JsonElementNumber(s);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
public interface ReadJsonSource {
|
||||
|
||||
public char nextChar(String eofMsg);
|
||||
|
||||
public void ignoreWhiteSpace();
|
||||
|
||||
public void back();
|
||||
|
||||
public int pos();
|
||||
|
||||
public String getErrorHelp();
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
|
||||
public class ReadJsonSourceReader implements ReadJsonSource {
|
||||
|
||||
private final Reader reader;
|
||||
|
||||
private char[] localBuffer;
|
||||
|
||||
private int totalPos;
|
||||
private int localPos;
|
||||
private int localPosEnd;
|
||||
|
||||
public ReadJsonSourceReader(Reader reader, int localBufferSize, int bufferSize) {
|
||||
this.reader = new BufferedReader(reader,bufferSize);
|
||||
this.localBuffer = new char[localBufferSize];
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.valueOf(localBuffer);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getErrorHelp() {
|
||||
int prev = localPos - 30;
|
||||
if (prev < 0){
|
||||
prev = 0;
|
||||
}
|
||||
String c = new String(localBuffer, prev, (localPos-prev));
|
||||
return "pos:"+pos()+" preceding:"+c;
|
||||
}
|
||||
|
||||
public int pos() {
|
||||
return totalPos+localPos;
|
||||
}
|
||||
|
||||
|
||||
public void ignoreWhiteSpace() {
|
||||
do {
|
||||
char c = nextChar("EOF ignoring whitespace");
|
||||
if (!Character.isWhitespace(c)){
|
||||
--localPos;
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
}
|
||||
|
||||
public void back() {
|
||||
localPos--;
|
||||
}
|
||||
|
||||
public char nextChar(String eofMsg) {
|
||||
if (localPos >= localPosEnd){
|
||||
if (!loadLocalBuffer()) {
|
||||
throw new TextException(eofMsg+" at pos:"+(totalPos+localPos));
|
||||
}
|
||||
}
|
||||
return localBuffer[localPos++];
|
||||
}
|
||||
|
||||
private boolean loadLocalBuffer() {
|
||||
try {
|
||||
localPosEnd = reader.read(localBuffer);
|
||||
if (localPosEnd > 0){
|
||||
totalPos += localPos;
|
||||
localPos = 0;
|
||||
return true;
|
||||
} else {
|
||||
this.localBuffer = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (IOException e){
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
|
||||
public class ReadJsonSourceString implements ReadJsonSource {
|
||||
|
||||
private final String source;
|
||||
private final int sourceLength;
|
||||
private int pos;
|
||||
|
||||
public ReadJsonSourceString(String source){
|
||||
this.source = source;
|
||||
this.sourceLength = source.length();
|
||||
}
|
||||
|
||||
public String getErrorHelp() {
|
||||
int prev = pos - 50;
|
||||
if (prev < 0){
|
||||
prev = 0;
|
||||
}
|
||||
String c = source.substring(prev, pos);
|
||||
return "pos:"+pos+" precedingcontent:"+c;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return source;
|
||||
}
|
||||
|
||||
public int pos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
public void back() {
|
||||
pos--;
|
||||
}
|
||||
|
||||
public char nextChar(String eofMsg) {
|
||||
if (pos >= sourceLength){
|
||||
throw new TextException(eofMsg+" at pos:"+pos);
|
||||
}
|
||||
return source.charAt(pos++);
|
||||
}
|
||||
|
||||
public void ignoreWhiteSpace() {
|
||||
do {
|
||||
char c = source.charAt(pos);
|
||||
if (Character.isWhitespace(c)){
|
||||
++pos;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,9 @@ import com.avaje.ebeaninternal.server.util.ArrayStack;
|
||||
public class WriteJson {
|
||||
|
||||
private final SpiEbeanServer server;
|
||||
|
||||
private final JsonGenerator generator;
|
||||
|
||||
private final PathProperties pathProperties;
|
||||
|
||||
private final PathStack pathStack = new PathStack();
|
||||
@@ -49,7 +51,7 @@ public class WriteJson {
|
||||
parentBeans.pop();
|
||||
}
|
||||
|
||||
public void beginAssocOne(String key, EntityBean bean) {
|
||||
public void beginAssocOne(String key, Object bean) {
|
||||
parentBeans.push(bean);
|
||||
pathStack.pushPathKey(key);
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
|
||||
public interface WriteJsonBuffer extends Appendable {
|
||||
|
||||
public WriteJsonBuffer append(String content);
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class WriteJsonBufferString implements WriteJsonBuffer {
|
||||
|
||||
private final StringBuilder buffer;
|
||||
|
||||
public WriteJsonBufferString(){
|
||||
this.buffer = new StringBuilder(256);
|
||||
}
|
||||
|
||||
public WriteJsonBufferString append(CharSequence csq) throws IOException {
|
||||
buffer.append(csq);
|
||||
return this;
|
||||
}
|
||||
|
||||
public WriteJsonBufferString append(CharSequence csq, int start, int end) throws IOException {
|
||||
buffer.append(csq, start, end);
|
||||
return this;
|
||||
}
|
||||
|
||||
public WriteJsonBufferString append(char c) throws IOException {
|
||||
buffer.append(c);
|
||||
return this;
|
||||
}
|
||||
|
||||
public WriteJsonBufferString append(String content){
|
||||
buffer.append(content);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getBufferOutput() {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
|
||||
public class WriteJsonBufferWriter implements WriteJsonBuffer {
|
||||
|
||||
private final Writer buffer;
|
||||
|
||||
public WriteJsonBufferWriter(Writer buffer){
|
||||
this.buffer = buffer;
|
||||
}
|
||||
|
||||
public WriteJsonBufferWriter append(String content){
|
||||
try {
|
||||
buffer.write(content);
|
||||
return this;
|
||||
} catch (IOException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public WriteJsonBufferWriter append(CharSequence csq) throws IOException {
|
||||
return append(csq, 0, csq.length());
|
||||
}
|
||||
|
||||
public WriteJsonBufferWriter append(CharSequence csq, int start, int end) throws IOException {
|
||||
for (int i = start; i < end; i++) {
|
||||
buffer.append(csq.charAt(i));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public WriteJsonBufferWriter append(char c) throws IOException {
|
||||
try {
|
||||
buffer.write(c);
|
||||
return this;
|
||||
} catch (IOException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,376 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
//import java.util.Collection;
|
||||
//import java.util.Iterator;
|
||||
//import java.util.Map;
|
||||
//import java.util.Set;
|
||||
//
|
||||
//import com.avaje.ebean.bean.EntityBean;
|
||||
//import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
//import com.avaje.ebean.text.PathProperties;
|
||||
//import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
//import com.avaje.ebean.text.json.JsonWriteBeanVisitor;
|
||||
//import com.avaje.ebean.text.json.JsonWriteOptions;
|
||||
//import com.avaje.ebean.text.json.JsonWriter;
|
||||
//import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
//import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
//import com.avaje.ebeaninternal.server.type.EscapeJson;
|
||||
//import com.avaje.ebeaninternal.server.type.ScalarType;
|
||||
//import com.avaje.ebeaninternal.server.util.ArrayStack;
|
||||
|
||||
|
||||
public class WriteJsonContext {//implements JsonWriter {
|
||||
|
||||
// private final SpiEbeanServer server;
|
||||
//
|
||||
// private final WriteJsonBuffer buffer;
|
||||
//
|
||||
// private final boolean pretty;
|
||||
//
|
||||
// private final JsonValueAdapter valueAdapter;
|
||||
//
|
||||
// private final ArrayStack<Object> parentBeans = new ArrayStack<Object>();
|
||||
//
|
||||
// private final PathProperties pathProperties;
|
||||
//
|
||||
// private final Map<String, JsonWriteBeanVisitor<?>> visitorMap;
|
||||
//
|
||||
// private final String callback;
|
||||
//
|
||||
// private final PathStack pathStack;
|
||||
//
|
||||
// private WriteBeanState beanState;
|
||||
//
|
||||
// private int depthOffset;
|
||||
//
|
||||
// boolean assocOne;
|
||||
//
|
||||
// public WriteJsonContext(WriteJsonBuffer buffer, boolean pretty, JsonValueAdapter dfltValueAdapter,
|
||||
// JsonWriteOptions options, String requestCallback, SpiEbeanServer server){
|
||||
//
|
||||
// this.server = server;
|
||||
// this.buffer = buffer;
|
||||
// this.pretty = pretty;
|
||||
// this.pathStack = new PathStack();
|
||||
// this.callback = getCallback(requestCallback, options);
|
||||
// if (options == null){
|
||||
// this.valueAdapter = dfltValueAdapter;
|
||||
// this.visitorMap = null;
|
||||
// this.pathProperties = null;
|
||||
//
|
||||
// } else {
|
||||
// this.valueAdapter = getValueAdapter(dfltValueAdapter, options.getValueAdapter());
|
||||
// this.visitorMap = emptyToNull(options.getVisitorMap());
|
||||
// this.pathProperties = emptyToNull(options.getPathProperties());
|
||||
// }
|
||||
//
|
||||
// if (callback != null){
|
||||
// buffer.append(requestCallback).append("(");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void toJson(String name, Collection<?> c) {
|
||||
//
|
||||
// beginAssocMany(name);
|
||||
//
|
||||
// Iterator<?> it = c.iterator();
|
||||
// if (!it.hasNext()){
|
||||
// endAssocMany();
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// EntityBean o = (EntityBean)it.next();
|
||||
// BeanDescriptor<?> d = getDecriptor(o.getClass());
|
||||
//
|
||||
// d.jsonWrite(this, o);
|
||||
// while (it.hasNext()) {
|
||||
// appendComma();
|
||||
// EntityBean t = (EntityBean)it.next();
|
||||
// d.jsonWrite(this, t);
|
||||
// }
|
||||
// endAssocMany();
|
||||
// }
|
||||
//
|
||||
// private <T> BeanDescriptor<T> getDecriptor(Class<T> cls) {
|
||||
// BeanDescriptor<T> d = server.getBeanDescriptor(cls);
|
||||
// if (d == null){
|
||||
// String msg = "No BeanDescriptor found for "+cls;
|
||||
// throw new RuntimeException(msg);
|
||||
// }
|
||||
// return d;
|
||||
// }
|
||||
//
|
||||
// public void appendRawValue(String key, String rawJsonValue) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// buffer.append(rawJsonValue);
|
||||
// }
|
||||
//
|
||||
// public void appendQuoteEscapeValue(String key, String valueToEscape) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// EscapeJson.escapeQuote(valueToEscape, buffer);
|
||||
// }
|
||||
//
|
||||
// public void end() {
|
||||
// if (callback != null){
|
||||
// buffer.append(")");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private <MK,MV> Map<MK,MV> emptyToNull(Map<MK,MV> m){
|
||||
// if ( m == null || m.isEmpty()) {
|
||||
// return null;
|
||||
// } else {
|
||||
// return m;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private PathProperties emptyToNull(PathProperties m){
|
||||
// if ( m == null || m.isEmpty()) {
|
||||
// return null;
|
||||
// } else {
|
||||
// return m;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private String getCallback(String requestCallback, JsonWriteOptions options) {
|
||||
// if (requestCallback != null){
|
||||
// return requestCallback;
|
||||
// }
|
||||
// if (options != null){
|
||||
// return options.getCallback();
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// private JsonValueAdapter getValueAdapter(JsonValueAdapter dfltValueAdapter, JsonValueAdapter valueAdapter) {
|
||||
// return valueAdapter == null ? dfltValueAdapter : valueAdapter;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Return the set of properties to write to JSON. If null is returned then
|
||||
// * the default will output the properties loaded for this bean.
|
||||
// */
|
||||
// public Set<String> getIncludeProperties() {
|
||||
// if (pathProperties != null){
|
||||
// String path = pathStack.peekWithNull();
|
||||
// return pathProperties.get(path);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// public JsonWriteBeanVisitor<?> getBeanVisitor() {
|
||||
// if (visitorMap != null){
|
||||
// String path = pathStack.peekWithNull();
|
||||
// return visitorMap.get(path);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// public String getJson() {
|
||||
// return buffer.toString();
|
||||
// }
|
||||
//
|
||||
// private void appendIndent(){
|
||||
//
|
||||
// buffer.append("\n");
|
||||
// int depth = depthOffset + parentBeans.size();
|
||||
// for (int i = 0; i < depth; i++) {
|
||||
// buffer.append(" ");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void appendObjectBegin(){
|
||||
// if (pretty && !assocOne){
|
||||
// appendIndent();
|
||||
// }
|
||||
// buffer.append("{");
|
||||
// }
|
||||
// public void appendObjectEnd(){
|
||||
// buffer.append("}");
|
||||
// }
|
||||
//
|
||||
// public void appendArrayBegin(){
|
||||
// if (pretty){
|
||||
// appendIndent();
|
||||
// }
|
||||
// buffer.append("[");
|
||||
// depthOffset++;
|
||||
// }
|
||||
//
|
||||
// public void appendArrayEnd(){
|
||||
// depthOffset--;
|
||||
// if (pretty){
|
||||
// appendIndent();
|
||||
// }
|
||||
// buffer.append("]");
|
||||
// }
|
||||
//
|
||||
// public void appendComma(){
|
||||
// buffer.append(",");
|
||||
// }
|
||||
//
|
||||
// public void addDepthOffset(int offset){
|
||||
// depthOffset += offset;
|
||||
// }
|
||||
//
|
||||
// public void beginAssocOneIsNull(String key) {
|
||||
// depthOffset++;
|
||||
// internalAppendKeyBegin(key);
|
||||
// appendNull();
|
||||
// depthOffset--;
|
||||
// }
|
||||
//
|
||||
// public void beginAssocOne(String key) {
|
||||
// pathStack.pushPathKey(key);
|
||||
//
|
||||
// internalAppendKeyBegin(key);
|
||||
// assocOne = true;
|
||||
// }
|
||||
//
|
||||
// public void endAssocOne() {
|
||||
//
|
||||
// pathStack.pop();
|
||||
// assocOne = false;
|
||||
// }
|
||||
//
|
||||
// public Boolean includeMany(String key) {
|
||||
// if (pathProperties != null){
|
||||
// String fullPath = pathStack.peekFullPath(key);
|
||||
// return pathProperties.hasPath(fullPath);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// public void beginAssocMany(String key) {
|
||||
//
|
||||
// pathStack.pushPathKey(key);
|
||||
//
|
||||
// depthOffset--;
|
||||
// internalAppendKeyBegin(key);
|
||||
// depthOffset++;
|
||||
// buffer.append("[");
|
||||
// }
|
||||
//
|
||||
// public void endAssocMany(){
|
||||
//
|
||||
// pathStack.pop();
|
||||
//
|
||||
// if (pretty){
|
||||
// depthOffset--;
|
||||
// appendIndent();
|
||||
// depthOffset++;
|
||||
// }
|
||||
// buffer.append("]");
|
||||
// }
|
||||
//
|
||||
// private void internalAppendKeyBegin(String key) {
|
||||
// if (!beanState.isFirstKey()){
|
||||
// buffer.append(",");
|
||||
// }
|
||||
// if (pretty){
|
||||
// appendIndent();
|
||||
// }
|
||||
// appendKeyWithComma(key, false);
|
||||
// }
|
||||
//
|
||||
// public <T> void appendNameValue(String key, ScalarType<T> scalarType, T value) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// scalarType.jsonWrite(buffer, value, getValueAdapter());
|
||||
// }
|
||||
//
|
||||
// public void appendDiscriminator(String key, String discValue) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// buffer.append("\"");
|
||||
// buffer.append(discValue);
|
||||
// buffer.append("\"");
|
||||
// }
|
||||
//
|
||||
// private void appendKeyWithComma(String key, boolean withComma) {
|
||||
// if (withComma){
|
||||
// if (!beanState.isFirstKey()){
|
||||
// buffer.append(",");
|
||||
// }
|
||||
// }
|
||||
// buffer.append("\"");
|
||||
// if(key == null) {
|
||||
// buffer.append("null");
|
||||
// } else {
|
||||
// buffer.append(key);
|
||||
// }
|
||||
// buffer.append("\":");
|
||||
// }
|
||||
//
|
||||
// public void appendNull(String key) {
|
||||
// appendKeyWithComma(key, true);
|
||||
// buffer.append("null");
|
||||
// }
|
||||
//
|
||||
// public void appendNull() {
|
||||
// buffer.append("null");
|
||||
// }
|
||||
//
|
||||
// public JsonValueAdapter getValueAdapter() {
|
||||
// return valueAdapter;
|
||||
// }
|
||||
//
|
||||
// public String toString() {
|
||||
// return buffer.toString();
|
||||
// }
|
||||
//
|
||||
// public void popParentBean(){
|
||||
// parentBeans.pop();
|
||||
// }
|
||||
//
|
||||
// public void pushParentBean(Object parentBean){
|
||||
// parentBeans.push(parentBean);
|
||||
// }
|
||||
//
|
||||
// public void popParentBeanMany(){
|
||||
// parentBeans.pop();
|
||||
// depthOffset--;
|
||||
// }
|
||||
//
|
||||
// public void pushParentBeanMany(Object parentBean){
|
||||
// parentBeans.push(parentBean);
|
||||
// depthOffset++;
|
||||
// }
|
||||
//
|
||||
// public boolean isParentBean(Object bean){
|
||||
// if (parentBeans.isEmpty()){
|
||||
// return false;
|
||||
// } else {
|
||||
// return parentBeans.contains(bean);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public WriteBeanState pushBeanState(Object bean) {
|
||||
// WriteBeanState newState = new WriteBeanState();//bean);
|
||||
// WriteBeanState prevState = beanState;
|
||||
// beanState = newState;
|
||||
// return prevState;
|
||||
// }
|
||||
//
|
||||
// public void pushPreviousState(WriteBeanState previousState) {
|
||||
// this.beanState = previousState;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public static class WriteBeanState {
|
||||
//
|
||||
// private boolean firstKeyOut;
|
||||
//
|
||||
// public WriteBeanState() {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public boolean isFirstKey() {
|
||||
// if (!firstKeyOut){
|
||||
// firstKeyOut = true;
|
||||
// return true;
|
||||
// } else {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
}
|
||||
@@ -6,17 +6,10 @@ import java.util.Map;
|
||||
|
||||
import com.avaje.ebean.config.CompoundType;
|
||||
import com.avaje.ebean.config.CompoundTypeProperty;
|
||||
import com.avaje.ebean.text.json.JsonElement;
|
||||
import com.avaje.ebean.text.json.JsonElementObject;
|
||||
import com.avaje.ebeaninternal.server.text.json.ReadJsonContext;
|
||||
//import com.avaje.ebeaninternal.server.text.json.WriteJsonContext;
|
||||
//import com.avaje.ebeaninternal.server.text.json.WriteJsonContext.WriteBeanState;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
/**
|
||||
* The internal representation of a Compound Type (Immutable Compound Value
|
||||
* Object).
|
||||
*
|
||||
* @author rbygrave
|
||||
* The internal representation of a Compound Type (Immutable Compound Value Object).
|
||||
*
|
||||
* @param <V>
|
||||
* The Type of the "Immutable Compound Value Object".
|
||||
@@ -168,47 +161,26 @@ public final class CtCompoundType<V> implements ScalarDataReader<V> {
|
||||
return parent + "." + propName;
|
||||
}
|
||||
}
|
||||
|
||||
public Object jsonRead(ReadJsonContext ctx) {
|
||||
|
||||
if (!ctx.readObjectBegin()) {
|
||||
// the object is null
|
||||
return null;
|
||||
}
|
||||
|
||||
JsonElementObject jsonObject = new JsonElementObject();
|
||||
do {
|
||||
if (!ctx.readKeyNext()){
|
||||
break;
|
||||
} else {
|
||||
// we read a property key ...
|
||||
String propName = ctx.getTokenKey();
|
||||
JsonElement unmappedJson = ctx.readUnmappedJson(propName);
|
||||
jsonObject.put(propName, unmappedJson);
|
||||
|
||||
if (!ctx.readValueNext()){
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return readJsonElementObject(ctx, jsonObject);
|
||||
|
||||
public Object jsonConvert(Map<String, Object> map) {
|
||||
return readJsonElementObject(map);
|
||||
}
|
||||
|
||||
private Object readJsonElementObject(ReadJsonContext ctx, JsonElementObject jsonObject){
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object readJsonElementObject(Map<String,Object> jsonObject){
|
||||
|
||||
boolean nullValue = false;
|
||||
Object[] values = new Object[propReaders.length];
|
||||
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
String propName = properties[i].getName();
|
||||
JsonElement jsonElement = jsonObject.get(propName);
|
||||
Object jsonElement = jsonObject.get(propName);
|
||||
|
||||
if (propReaders[i] instanceof CtCompoundType<?>) {
|
||||
values[i] = ((CtCompoundType<?>)propReaders[i]).readJsonElementObject(ctx, (JsonElementObject)jsonElement);
|
||||
|
||||
values[i] = ((CtCompoundType<?>)propReaders[i]).readJsonElementObject((Map<String,Object>)jsonElement);
|
||||
} else {
|
||||
values[i] = ((ScalarType<?>)propReaders[i]).jsonFromString(jsonElement.toPrimitiveString(), ctx.getValueAdapter());
|
||||
//((ScalarType<?>)propReaders[i]).jsonFromString(jsonElement.toPrimitiveString(), ctx.getValueAdapter());
|
||||
values[i] = ((ScalarType<?>)propReaders[i]).parse(jsonElement.toString());;
|
||||
}
|
||||
if (values[i] == null){
|
||||
nullValue = true;
|
||||
@@ -223,40 +195,35 @@ public final class CtCompoundType<V> implements ScalarDataReader<V> {
|
||||
}
|
||||
|
||||
|
||||
// public void jsonWrite(WriteJsonContext ctx, Object valueObject, String propertyName) {
|
||||
//
|
||||
// if (valueObject == null){
|
||||
// ctx.beginAssocOneIsNull(propertyName);
|
||||
//
|
||||
// } else {
|
||||
// ctx.pushParentBean(valueObject);
|
||||
// ctx.beginAssocOne(propertyName);
|
||||
// jsonWriteProps(ctx, valueObject, propertyName);
|
||||
// ctx.endAssocOne();
|
||||
// ctx.popParentBean();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
// private void jsonWriteProps(WriteJsonContext ctx, Object valueObject, String propertyName) {
|
||||
//
|
||||
// ctx.appendObjectBegin();
|
||||
// WriteBeanState prevState = ctx.pushBeanState(valueObject);
|
||||
//
|
||||
// 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<?>) {
|
||||
// ((CtCompoundType)propReaders[i]).jsonWrite(ctx, value, propName);
|
||||
//
|
||||
// } else {
|
||||
// ctx.appendNameValue(propName, (ScalarType)propReaders[i], value);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// ctx.pushPreviousState(prevState);
|
||||
// ctx.appendObjectEnd();
|
||||
// }
|
||||
public void jsonWrite(WriteJson ctx, Object valueObject, String propertyName) {
|
||||
|
||||
ctx.beginAssocOne(propertyName, valueObject);
|
||||
jsonWriteProps(ctx, valueObject, propertyName);
|
||||
ctx.endAssocOne();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void jsonWriteProps(WriteJson ctx, Object valueObject, String propertyName) {
|
||||
|
||||
if (propertyName != null) {
|
||||
ctx.gen().writeStartObject(propertyName);
|
||||
} else {
|
||||
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<?>) {
|
||||
((CtCompoundType) propReaders[i]).jsonWrite(ctx, value, propName);
|
||||
|
||||
} else {
|
||||
((ScalarType) propReaders[i]).jsonWrite(ctx.gen(), propName, value);
|
||||
//ctx.appendNameValue(propName, (ScalarType) propReaders[i], value);
|
||||
}
|
||||
}
|
||||
|
||||
ctx.gen().writeEnd();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import java.io.IOException;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.util.StringHelper;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
public class EscapeJson {
|
||||
|
||||
@@ -45,24 +44,6 @@ public class EscapeJson {
|
||||
|
||||
}
|
||||
|
||||
public static void escape(String value, WriteJsonBuffer sb) {
|
||||
if (value == null) {
|
||||
sb.append("null");
|
||||
} else {
|
||||
escapeAppend(value, sb);
|
||||
}
|
||||
}
|
||||
|
||||
public static void escapeQuote(String value, WriteJsonBuffer sb) {
|
||||
if (value == null) {
|
||||
sb.append("null");
|
||||
} else {
|
||||
sb.append("\"");
|
||||
escapeAppend(value, sb);
|
||||
sb.append("\"");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape quotes, \, /, \r, \n, \b, \f, \t and characters (U+0000 through
|
||||
* U+001F).
|
||||
|
||||
@@ -11,8 +11,6 @@ import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.StringFormatter;
|
||||
import com.avaje.ebean.text.StringParser;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
/**
|
||||
* Describes a scalar type.
|
||||
@@ -187,18 +185,12 @@ public interface ScalarType<T> extends StringParser, StringFormatter, ScalarData
|
||||
*/
|
||||
public boolean isDateTimeCapable();
|
||||
|
||||
public void jsonWrite(WriteJsonBuffer buffer, T value, JsonValueAdapter ctx);
|
||||
|
||||
public String jsonToString(T value, JsonValueAdapter ctx);
|
||||
public Object readData(DataInput dataInput) throws IOException;
|
||||
|
||||
public T jsonFromString(String value, JsonValueAdapter ctx);
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException;
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException;
|
||||
public Object jsonRead(JsonParser ctx, Event event);
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException;
|
||||
|
||||
public Object jsonRead(JsonParser ctx, Event event);
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value);
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value);
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
|
||||
|
||||
@@ -74,25 +72,12 @@ public abstract class ScalarTypeBase<T> implements ScalarType<T> {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
dataReader.incrementPos(1);
|
||||
}
|
||||
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
|
||||
list.addScalarType(propName, this);
|
||||
}
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
dataReader.incrementPos(1);
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonBuffer buffer, T value, JsonValueAdapter ctx) {
|
||||
String v = jsonToString(value, ctx);
|
||||
buffer.append(v);
|
||||
}
|
||||
|
||||
public String jsonToString(T value, JsonValueAdapter ctx) {
|
||||
return formatValue(value);
|
||||
}
|
||||
|
||||
public T jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return parse(value);
|
||||
}
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
|
||||
list.addScalarType(propName, this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,9 +11,6 @@ import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
/**
|
||||
* Base class for Date types.
|
||||
*/
|
||||
@@ -68,8 +65,7 @@ public abstract class ScalarTypeBaseDate<T> extends ScalarTypeBase<T> {
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
if (ctx.isIntegralNumber()) {
|
||||
long millis = ctx.getLong();
|
||||
return parseDateTime(millis);
|
||||
return parseDateTime(ctx.getLong());
|
||||
} else {
|
||||
String string = ctx.getString();
|
||||
throw new RuntimeException("convert "+string);
|
||||
@@ -83,23 +79,6 @@ public abstract class ScalarTypeBaseDate<T> extends ScalarTypeBase<T> {
|
||||
|
||||
public abstract long convertToMillis(Object value);
|
||||
|
||||
@Override
|
||||
public String jsonToString(T value, JsonValueAdapter ctx) {
|
||||
Date date = convertToDate(value);
|
||||
return ctx.jsonFromDate(date);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(WriteJsonBuffer buffer, T value, JsonValueAdapter ctx) {
|
||||
String s = jsonToString(value, ctx);
|
||||
buffer.append(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
Date ts = ctx.jsonToDate(value);
|
||||
return convertFromDate(ts);
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
|
||||
@@ -11,9 +11,6 @@ import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
/**
|
||||
* Base type for DateTime types.
|
||||
*/
|
||||
@@ -83,24 +80,6 @@ public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(WriteJsonBuffer buffer, T value, JsonValueAdapter ctx) {
|
||||
String v = jsonToString(value, ctx);
|
||||
buffer.append(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonToString(T value, JsonValueAdapter ctx) {
|
||||
Timestamp ts = convertToTimestamp(value);
|
||||
return ctx.jsonFromTimestamp(ts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
Timestamp ts = ctx.jsonToTimestamp(value);
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
|
||||
@@ -11,8 +11,6 @@ import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
/**
|
||||
* Base ScalarType for types which converts to and from a VARCHAR database
|
||||
@@ -85,19 +83,6 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
return formatValue((T) v);
|
||||
}
|
||||
|
||||
public T jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return parse(EscapeJson.unescapeSlash(value));
|
||||
}
|
||||
|
||||
public String toJsonString(Object value, JsonValueAdapter ctx) {
|
||||
return EscapeJson.escapeQuote(format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(WriteJsonBuffer buffer, T value, JsonValueAdapter ctx) {
|
||||
EscapeJson.escapeQuote(format(value), buffer);
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
|
||||
@@ -10,8 +10,6 @@ import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
/**
|
||||
* Encrypted ScalarType that wraps a byte[] types.
|
||||
@@ -115,18 +113,6 @@ public class ScalarTypeBytesEncrypted implements ScalarType<byte[]> {
|
||||
baseType.accumulateScalarTypes(propName, list);
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonBuffer buffer, byte[] value, JsonValueAdapter ctx) {
|
||||
baseType.jsonWrite(buffer, value, ctx);
|
||||
}
|
||||
|
||||
public String jsonToString(byte[] value, JsonValueAdapter ctx) {
|
||||
return baseType.jsonToString(value, ctx);
|
||||
}
|
||||
|
||||
public byte[] jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return baseType.jsonFromString(value, ctx);
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
int len = dataInput.readInt();
|
||||
byte[] value = new byte[len];
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.sql.Types;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
/**
|
||||
@@ -62,16 +61,6 @@ public class ScalarTypeChar extends ScalarTypeBaseVarchar<Character> {
|
||||
public Character parse(String value) {
|
||||
return value.charAt(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return value.charAt(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonToString(Character value, JsonValueAdapter ctx) {
|
||||
return EscapeJson.escapeQuote(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
@@ -8,7 +7,6 @@ import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
/**
|
||||
@@ -64,16 +62,6 @@ public class ScalarTypeCharArray extends ScalarTypeBaseVarchar<char[]>{
|
||||
public char[] parse(String value) {
|
||||
return value.toCharArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public char[] jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return value.toCharArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonToString(char[] value, JsonValueAdapter ctx) {
|
||||
return EscapeJson.escapeQuote(String.valueOf(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.avaje.ebeaninternal.server.type;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
/**
|
||||
@@ -11,66 +10,55 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
*/
|
||||
public class ScalarTypeClob extends ScalarTypeBaseVarchar<String> {
|
||||
|
||||
static final int clobBufferSize = 512;
|
||||
|
||||
static final int stringInitialSize = 512;
|
||||
|
||||
protected ScalarTypeClob(boolean jdbcNative, int jdbcType) {
|
||||
super(String.class, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
public ScalarTypeClob() {
|
||||
super(String.class, true, Types.CLOB);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertFromDbString(String dbValue) {
|
||||
return dbValue;
|
||||
static final int clobBufferSize = 512;
|
||||
|
||||
static final int stringInitialSize = 512;
|
||||
|
||||
protected ScalarTypeClob(boolean jdbcNative, int jdbcType) {
|
||||
super(String.class, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
public ScalarTypeClob() {
|
||||
super(String.class, true, Types.CLOB);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertFromDbString(String dbValue) {
|
||||
return dbValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(String beanValue) {
|
||||
return beanValue;
|
||||
}
|
||||
|
||||
public void bind(DataBind b, String value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
b.setString(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(String beanValue) {
|
||||
return beanValue;
|
||||
}
|
||||
public String read(DataReader dataReader) throws SQLException {
|
||||
|
||||
public void bind(DataBind b, String value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
b.setString(value);
|
||||
}
|
||||
}
|
||||
return dataReader.getStringClob();
|
||||
}
|
||||
|
||||
public String read(DataReader dataReader) throws SQLException {
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
|
||||
return dataReader.getStringClob();
|
||||
}
|
||||
public String toBeanType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
public String formatValue(String t) {
|
||||
return t;
|
||||
}
|
||||
|
||||
public String toBeanType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
public String parse(String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
public String formatValue(String t) {
|
||||
return t;
|
||||
}
|
||||
|
||||
public String parse(String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonToString(String value, JsonValueAdapter ctx) {
|
||||
return EscapeJson.escapeQuote(value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,9 +9,6 @@ import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
public class ScalarTypeEncryptedWrapper<T> implements ScalarType<T> {
|
||||
|
||||
private final ScalarType<T> wrapped;
|
||||
@@ -122,18 +119,6 @@ public class ScalarTypeEncryptedWrapper<T> implements ScalarType<T> {
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
|
||||
wrapped.accumulateScalarTypes(propName, list);
|
||||
}
|
||||
|
||||
public String jsonToString(T value, JsonValueAdapter ctx) {
|
||||
return wrapped.jsonToString(value, ctx);
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonBuffer buffer, T value, JsonValueAdapter ctx) {
|
||||
wrapped.jsonWrite(buffer, value, ctx);
|
||||
}
|
||||
|
||||
public T jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return wrapped.jsonFromString(value, ctx);
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
wrapped.jsonWrite(ctx, name, value);
|
||||
|
||||
@@ -12,7 +12,6 @@ import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
|
||||
|
||||
/**
|
||||
@@ -240,16 +239,6 @@ public class ScalarTypeEnumStandard {
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
ctx.write(name, formatValue(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return parse(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonToString(Object value, JsonValueAdapter ctx) {
|
||||
return EscapeJson.escapeQuote(format(value));
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
|
||||
@@ -11,7 +11,6 @@ import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
/**
|
||||
@@ -67,22 +66,13 @@ public class ScalarTypeInteger extends ScalarTypeBase<Integer> {
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
return Integer.valueOf(ctx.getInt());
|
||||
}
|
||||
|
||||
public String jsonToString(Integer value, JsonValueAdapter ctx) {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
public Integer jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return Integer.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
return Integer.valueOf(ctx.getInt());
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
ctx.write(name, (Integer)value);
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
ctx.write(name, (Integer) value);
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,7 @@ import javax.json.stream.JsonGenerator;
|
||||
import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
/**
|
||||
* ScalarType for String.
|
||||
@@ -60,23 +58,6 @@ public class ScalarTypeString extends ScalarTypeBase<String> {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void jsonWrite(WriteJsonBuffer buffer, String value, JsonValueAdapter ctx) {
|
||||
String s = format(value);
|
||||
EscapeJson.escapeQuote(s, buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String jsonToString(String value, JsonValueAdapter ctx) {
|
||||
return EscapeJson.escapeQuote(value);
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
|
||||
@@ -10,8 +10,6 @@ import javax.json.stream.JsonParser;
|
||||
import javax.json.stream.JsonParser.Event;
|
||||
|
||||
import com.avaje.ebean.config.ScalarTypeConverter;
|
||||
import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJsonBuffer;
|
||||
|
||||
/**
|
||||
* A ScalarType that uses a ScalarTypeConverter to convert to and from another
|
||||
@@ -169,22 +167,6 @@ public class ScalarTypeWrapper<B, S> implements ScalarType<B> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String jsonToString(B value, JsonValueAdapter ctx) {
|
||||
|
||||
S sv = converter.unwrapValue(value);
|
||||
return scalarType.jsonToString(sv, ctx);
|
||||
}
|
||||
|
||||
public void jsonWrite(WriteJsonBuffer buffer, B value, JsonValueAdapter ctx) {
|
||||
S sv = converter.unwrapValue(value);
|
||||
scalarType.jsonWrite(buffer, sv, ctx);
|
||||
}
|
||||
|
||||
public B jsonFromString(String value, JsonValueAdapter ctx) {
|
||||
S s = scalarType.jsonFromString(value, ctx);
|
||||
return converter.wrapValue(s);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, Event event) {
|
||||
@@ -194,9 +176,9 @@ public class ScalarTypeWrapper<B, S> implements ScalarType<B> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object beanValue) {
|
||||
@SuppressWarnings("unchecked")
|
||||
S unwrapValue = converter.unwrapValue((B)beanValue);
|
||||
scalarType.jsonWrite(ctx, name, unwrapValue);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user