From 32bcd5e853b289b85d2a27b2bacdbb8606a0a008 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Mon, 7 Mar 2016 16:27:16 +1300 Subject: [PATCH] #589 - ElasticSearch - JsonReader given BeanType --- .../java/com/avaje/ebean/text/json/JsonContext.java | 11 +++++++++++ .../ebeaninternal/server/deploy/BeanProperty.java | 10 ++++++++-- .../ebeaninternal/server/text/json/DJsonContext.java | 9 +++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/avaje/ebean/text/json/JsonContext.java b/src/main/java/com/avaje/ebean/text/json/JsonContext.java index 8f5d424d6..dc67d9824 100644 --- a/src/main/java/com/avaje/ebean/text/json/JsonContext.java +++ b/src/main/java/com/avaje/ebean/text/json/JsonContext.java @@ -1,6 +1,7 @@ package com.avaje.ebean.text.json; import com.avaje.ebean.FetchPath; +import com.avaje.ebean.plugin.BeanType; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; @@ -67,6 +68,16 @@ public interface JsonContext { */ JsonBeanReader createBeanReader(Class cls, JsonParser parser, JsonReadOptions options) throws JsonIOException; + /** + * Create and return a new bean reading for the bean type given the JSON options and source. + *

+ * Note that JsonOption provides an option for setting a persistence context and also enabling + * further lazy loading. Further lazy loading requires a persistence context so if that is set + * on then a persistence context is created if there is not one set. + *

+ */ + JsonBeanReader createBeanReader(BeanType beanType, JsonParser parser, JsonReadOptions options) throws JsonIOException; + /** * Convert json string input into a list of beans of a specific type. * diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java index 75a0a7da4..3c8b6e112 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java @@ -5,7 +5,6 @@ import com.avaje.ebean.bean.EntityBean; import com.avaje.ebean.config.EncryptKey; import com.avaje.ebean.config.dbplatform.DbEncryptFunction; import com.avaje.ebean.config.dbplatform.DbType; -import com.avaje.ebeaninternal.server.type.ScalarTypeEnum; import com.avaje.ebeanservice.docstore.api.mapping.DocMappingBuilder; import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyMapping; import com.avaje.ebean.plugin.Property; @@ -1319,7 +1318,7 @@ public class BeanProperty implements ElPropertyValue, Property { DocPropertyType type = scalarType.getDocType(); DocPropertyOptions options = docOptions.copy(); - if (id || DocPropertyType.ENUM == type) { + if (DocPropertyType.ENUM == type || isStringId(type)) { options.setCode(true); } @@ -1327,4 +1326,11 @@ public class BeanProperty implements ElPropertyValue, Property { } } + /** + * Return true if this is a String Id property and should be treated as a code by the document store. + */ + private boolean isStringId(DocPropertyType type) { + return DocPropertyType.STRING == type && id; + } + } diff --git a/src/main/java/com/avaje/ebeaninternal/server/text/json/DJsonContext.java b/src/main/java/com/avaje/ebeaninternal/server/text/json/DJsonContext.java index 693ec76e1..21d6ffd6f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/text/json/DJsonContext.java +++ b/src/main/java/com/avaje/ebeaninternal/server/text/json/DJsonContext.java @@ -3,6 +3,7 @@ package com.avaje.ebeaninternal.server.text.json; import com.avaje.ebean.FetchPath; import com.avaje.ebean.bean.EntityBean; import com.avaje.ebean.config.JsonConfig; +import com.avaje.ebean.plugin.BeanType; import com.avaje.ebean.text.json.EJson; import com.avaje.ebean.text.json.JsonContext; import com.avaje.ebean.text.json.JsonIOException; @@ -125,6 +126,14 @@ public class DJsonContext implements JsonContext { return new DJsonBeanReader(desc, readJson); } + @Override + public DJsonBeanReader createBeanReader(BeanType beanType, JsonParser parser, JsonReadOptions options) throws JsonIOException { + + BeanDescriptor desc = (BeanDescriptor)beanType; + ReadJson readJson = new ReadJson(desc, parser, options, determineObjectMapper(options)); + return new DJsonBeanReader(desc, readJson); + } + public List toList(Class cls, String json) throws JsonIOException { return toList(cls, new StringReader(json)); }