From bb3ea7659475fd7acaa84f157e6c2bab37ee2223 Mon Sep 17 00:00:00 2001 From: outersky Date: Wed, 24 Oct 2012 22:35:02 +0800 Subject: [PATCH] Add Expose support for Ebean entities. can be applied to simple property, or One/ManyToMany association. --- .../server/deploy/BeanProperty.java | 20 +++++++++++++++++-- .../server/deploy/BeanPropertyAssocMany.java | 8 ++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) 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 e78c882fc..1ae331eed 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanProperty.java @@ -17,6 +17,7 @@ import javax.naming.directory.BasicAttribute; import javax.persistence.PersistenceException; import com.avaje.ebean.InvalidValue; +import com.avaje.ebean.annotation.Expose; import com.avaje.ebean.bean.EntityBean; import com.avaje.ebean.config.EncryptKey; import com.avaje.ebean.config.dbplatform.DbEncryptFunction; @@ -270,6 +271,10 @@ public class BeanProperty implements ElPropertyValue { int deployOrder; + final boolean jsonSerialize; + + final boolean jsonDeserialize; + public BeanProperty(DeployBeanProperty deploy) { this(null, null, deploy); } @@ -349,6 +354,10 @@ public class BeanProperty implements ElPropertyValue { EntityType et = descriptor == null ? null : descriptor.getEntityType(); this.elPlaceHolder = tableAliasIntern(descriptor, deploy.getElPlaceHolder(et), false, null); this.elPlaceHolderEncrypted = tableAliasIntern(descriptor, deploy.getElPlaceHolder(et), dbEncrypted, dbColumn); + + Expose expose = deploy.getField().getAnnotation(Expose.class); + this.jsonSerialize = expose==null || expose.serialize(); + this.jsonDeserialize = expose==null || expose.deserialize(); } private String tableAliasIntern(BeanDescriptor descriptor, String s, boolean dbEncrypted, String dbColumn) { @@ -431,6 +440,9 @@ public class BeanProperty implements ElPropertyValue { this.elPlaceHolder = override.replace(source.elPlaceHolder, source.dbColumn); this.elPlaceHolderEncrypted = override.replace(source.elPlaceHolderEncrypted, source.dbColumn); + + this.jsonSerialize = source.jsonSerialize; + this.jsonDeserialize = source.jsonDeserialize; } /** @@ -1283,7 +1295,9 @@ public class BeanProperty implements ElPropertyValue { @SuppressWarnings("unchecked") public void jsonWrite(WriteJsonContext ctx, Object bean) { - + if(!jsonSerialize){ + return; + } Object value = getValueIntercept(bean); if (value == null) { ctx.appendNull(name); @@ -1293,7 +1307,9 @@ public class BeanProperty implements ElPropertyValue { } public void jsonRead(ReadJsonContext ctx, Object bean) { - + if(jsonDeserialize){ + return; + } String jsonValue; try { jsonValue = ctx.readScalarValue(); diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocMany.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocMany.java index 833d59d88..8744430a0 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocMany.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocMany.java @@ -754,7 +754,9 @@ public class BeanPropertyAssocMany extends BeanPropertyAssoc { } public void jsonWrite(WriteJsonContext ctx, Object bean) { - + if(!this.jsonSerialize){ + return; + } Boolean include = ctx.includeMany(name); if (Boolean.FALSE.equals(include)){ return; @@ -769,7 +771,9 @@ public class BeanPropertyAssocMany extends BeanPropertyAssoc { } public void jsonRead(ReadJsonContext ctx, Object bean){ - + if(!this.jsonDeserialize){ + return; + } if (!ctx.readArrayBegin()) { // the array is null return;