incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
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 GMT
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<String> propertyNamesToIgnore;
+    
+    private static DateFormat calendarFormat;
+    
+    private final Set<String> 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:
+     * <table>
+     *   <tr><th>JSR Property Type</th><th>JSON Value Type</th></tr>
+     *   <tr><td>BINARY</td><td>size of binary value as long<sup>1</sup></td></tr>
+     *   <tr><td>DATE</td><td>converted date string as defined by
ECMA</td></tr>
+     *   <tr><td>BOOLEAN</td><td>boolean</td></tr>
+     *   <tr><td>LONG</td><td>long</td></tr>
+     *   <tr><td>DOUBLE</td><td>double</td></tr>
+     *   <tr><td><i>all other</li></td><td>string</td></tr>
+     * </table>
+     * <sup>1</sup> 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());
     }
 }



Mime
View raw message