From e01074b2531db8c65fce717e69c3535a91672b19 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 12 Apr 2007 06:17:09 +0000 Subject: [PATCH] added a mechanism to customize the conversion process. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@3042 71c3de6d-444a-0410-be80-ed276b4c234a --- .../main/java/hudson/api/CustomExposureBean.java | 13 +++++++++++++ core/src/main/java/hudson/api/Property.java | 5 +++++ core/src/main/java/hudson/model/Result.java | 8 +++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/hudson/api/CustomExposureBean.java diff --git a/core/src/main/java/hudson/api/CustomExposureBean.java b/core/src/main/java/hudson/api/CustomExposureBean.java new file mode 100644 index 0000000000..0f0b238033 --- /dev/null +++ b/core/src/main/java/hudson/api/CustomExposureBean.java @@ -0,0 +1,13 @@ +package hudson.api; + +/** + * Interface that an exposed bean can implement, to do the equivalent + * of writeReplace in Java serialization. + * @author Kohsuke Kawaguchi + */ +public interface CustomExposureBean { + /** + * The returned object will be introspected and written as JSON/XML. + */ + Object toExposedObject(); +} diff --git a/core/src/main/java/hudson/api/Property.java b/core/src/main/java/hudson/api/Property.java index efdb1b6db0..a53e42ce6b 100644 --- a/core/src/main/java/hudson/api/Property.java +++ b/core/src/main/java/hudson/api/Property.java @@ -58,6 +58,11 @@ abstract class Property implements Comparable { return; } + if(value instanceof CustomExposureBean) { + writeValue(((CustomExposureBean)value).toExposedObject(),depth,writer); + return; + } + Class c = value.getClass(); if(STRING_TYPES.contains(c)) { diff --git a/core/src/main/java/hudson/model/Result.java b/core/src/main/java/hudson/model/Result.java index dc4a9aff68..7ca52f808f 100644 --- a/core/src/main/java/hudson/model/Result.java +++ b/core/src/main/java/hudson/model/Result.java @@ -5,12 +5,14 @@ import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter; import java.io.Serializable; +import hudson.api.CustomExposureBean; + /** * The build outcome. * * @author Kohsuke Kawaguchi */ -public final class Result implements Serializable { +public final class Result implements Serializable, CustomExposureBean { /** * The build didn't have any fatal errors not errors. */ @@ -75,6 +77,10 @@ public final class Result implements Serializable { return FAILURE; } + public Object toExposedObject() { + return name; + } + private static final long serialVersionUID = 1L; private static final Result[] all = new Result[] {SUCCESS,UNSTABLE,FAILURE,ABORTED}; -- GitLab