Return-Path: Delivered-To: apmail-incubator-sling-commits-archive@locus.apache.org Received: (qmail 82623 invoked from network); 4 Feb 2008 20:03:47 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Feb 2008 20:03:47 -0000 Received: (qmail 70762 invoked by uid 500); 4 Feb 2008 20:03:39 -0000 Delivered-To: apmail-incubator-sling-commits-archive@incubator.apache.org Received: (qmail 70731 invoked by uid 500); 4 Feb 2008 20:03:39 -0000 Mailing-List: contact sling-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: sling-dev@incubator.apache.org Delivered-To: mailing list sling-commits@incubator.apache.org Received: (qmail 70722 invoked by uid 99); 4 Feb 2008 20:03:39 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Feb 2008 12:03:39 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Feb 2008 20:03:06 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 639761A9832; Mon, 4 Feb 2008 12:03:13 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r618408 - /incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java Date: Mon, 04 Feb 2008 20:03:13 -0000 To: sling-commits@incubator.apache.org From: fmeschbe@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080204200313.639761A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: fmeschbe Date: Mon Feb 4 12:03:12 2008 New Revision: 618408 URL: http://svn.apache.org/viewvc?rev=618408&view=rev Log: SLING-219 Create typified JSON for some property types Modified: incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java Modified: incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java URL: http://svn.apache.org/viewvc/incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java?rev=618408&r1=618407&r2=618408&view=diff ============================================================================== --- incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java (original) +++ incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java Mon Feb 4 12:03:12 2008 @@ -19,6 +19,7 @@ import java.io.Writer; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Locale; import java.util.Set; @@ -38,7 +39,10 @@ * are threadsafe. */ public class JsonItemWriter { - final Set propertyNamesToIgnore; + + private static DateFormat calendarFormat; + + private final Set propertyNamesToIgnore; /** Create a JsonItemWriter * @param propertyNamesToIgnore if not null, a property having a name from this @@ -95,7 +99,7 @@ w.key(prop.getName()); w.array(); for(Value v : prop.getValues()) { - w.value(convertValue(v)); + dumpValue(w, v); } w.endArray(); } @@ -142,21 +146,62 @@ w.key(p.getName()); } - w.value(convertValue(p.getValue())); + dumpValue(w, p.getValue()); } - /** Convert a Value for JSON output */ - protected Object convertValue(Value v) throws ValueFormatException, IllegalStateException, RepositoryException { - if(v.getType() == PropertyType.BINARY) { - // TODO return the binary size - return new Integer(0); - - } else if(v.getType() == PropertyType.DATE) { - final DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.US); - return fmt.format(v.getDate().getTime()); - - } else { - return v.getString(); + /** + * Writes the given value to the JSON writer. currently the following + * conversions are done: + * + * + * + * + * + * + * + * + *
JSR Property TypeJSON Value Type
BINARYsize of binary value as long1
DATEconverted date string as defined by ECMA
BOOLEANboolean
LONGlong
DOUBLEdouble
all otherstring
+ * 1 Currently not implemented and uses 0 as default. + * + * @param w json writer + * @param v value to dump + */ + protected void dumpValue(JSONWriter w, Value v) + throws ValueFormatException, IllegalStateException, + RepositoryException, JSONException { + + switch (v.getType()) { + case PropertyType.BINARY: + // TODO return the binary size + w.value(0); + break; + + case PropertyType.DATE: + w.value(format(v.getDate())); + break; + + case PropertyType.BOOLEAN: + w.value(v.getBoolean()); + break; + + case PropertyType.LONG: + w.value(v.getLong()); + break; + + case PropertyType.DOUBLE: + w.value(v.getDouble()); + + break; + default: + w.value(v.getString()); + } + } + + public static synchronized String format(Calendar date) { + if (calendarFormat == null) { + calendarFormat = new SimpleDateFormat( + "EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.US); } + return calendarFormat.format(date.getTime()); } }