Paradigm shift: json Objects only accept json strings as constructors' arguments...
authorMarco Zanon <info@marcozanon.com>
Wed, 1 Feb 2012 18:28:03 +0000 (18:28 +0000)
committerMarco Zanon <info@marcozanon.com>
Wed, 1 Feb 2012 18:28:03 +0000 (18:28 +0000)
src/java/com/marcozanon/macaco/json/MJsonArray.java
src/java/com/marcozanon/macaco/json/MJsonBoolean.java
src/java/com/marcozanon/macaco/json/MJsonNull.java
src/java/com/marcozanon/macaco/json/MJsonNumber.java
src/java/com/marcozanon/macaco/json/MJsonObject.java
src/java/com/marcozanon/macaco/json/MJsonString.java
src/java/com/marcozanon/macaco/json/MJsonValue.java
src/java/com/marcozanon/macaco/web/ui/MWebApplicationServlet.java
src/java/com/marcozanon/macaco/web/ui/MWebDownloader.java
src/java/com/marcozanon/macaco/web/ui/MWebMessage.java

index 57aef0bc9e1d6d0164b6ed222d3a6d9b22ed9f6d..030d499d2dc3d5cf798a163d1fc8786497adb9c4 100644 (file)
@@ -53,7 +53,7 @@ public class MJsonArray extends MJsonValue {
     public MJsonArray clone() {
         MJsonArray tmpMJsonArray = null;
         try {
-            tmpMJsonArray = new MJsonArray(this.getFormattedValue());
+            tmpMJsonArray = new MJsonArray(this.getJsonValue());
         }
         catch (MInvalidValueJsonException exception) { // cannot happen
         }
@@ -210,14 +210,14 @@ public class MJsonArray extends MJsonValue {
 
     /* Formatter */
 
-    public String getFormattedValue() {
+    public String getJsonValue() {
         StringBuilder s = new StringBuilder("");
         s.append("[");
         for (int i = 0; i < this.countValues(); i++) {
             if (i > 0) {
                 s.append(", ");
             }
-            s.append(this.getValuesReference().get(i).getFormattedValue());
+            s.append(this.getValuesReference().get(i).getJsonValue());
         }
         s.append("]");
         return s.toString();
index eb9aa5159380c1ecb83c951b24ffba7d9698ad9e..d02fa7de35e024b06ecece8a4de5f2ce03c5efd5 100644 (file)
@@ -31,8 +31,8 @@ public class MJsonBoolean extends MJsonValue {
 
     /* */
 
-    public MJsonBoolean(boolean x) throws MInvalidValueJsonException {
-        this((new Boolean(x)).toString());
+    public MJsonBoolean() throws MInvalidValueJsonException {
+        this("false");
     }
 
     public MJsonBoolean(String x) throws MInvalidValueJsonException {
@@ -44,7 +44,7 @@ public class MJsonBoolean extends MJsonValue {
     public MJsonBoolean clone() {
         MJsonBoolean tmpMJsonBoolean = null;
         try {
-            tmpMJsonBoolean = new MJsonBoolean(this.getFormattedValue());
+            tmpMJsonBoolean = new MJsonBoolean(this.getJsonValue());
         }
         catch (MInvalidValueJsonException exception) { // cannot happen
         }
@@ -94,7 +94,7 @@ public class MJsonBoolean extends MJsonValue {
 
     /* Formatter */
 
-    public String getFormattedValue() {
+    public String getJsonValue() {
         return this.getValue().toString();
     }
 
index 162efdb90e160d7ae52f95babaa9a9925622f7d4..7d10b20fea9d89af7fa0600f7fc48dcbce6b67b8 100644 (file)
@@ -42,19 +42,13 @@ public class MJsonNull extends MJsonValue {
     public MJsonNull clone() {
         MJsonNull tmpMJsonNull = null;
         try {
-            tmpMJsonNull = new MJsonNull(this.getFormattedValue());
+            tmpMJsonNull = new MJsonNull(this.getJsonValue());
         }
         catch (MInvalidValueJsonException exception) { // cannot happen
         }
         return tmpMJsonNull;
     }
 
-    /* Value handler */
-
-    public String getValue() {
-        return "null";
-    }
-
     /* Parsers */
 
     protected static int getTokenLength(String x) {
@@ -80,8 +74,8 @@ public class MJsonNull extends MJsonValue {
 
     /* Formatter */
 
-    public String getFormattedValue() {
-        return this.getValue();
+    public String getJsonValue() {
+        return "null";
     }
 
 }
index f4a49f585931735742faf8a2e4a9249a4bc71bf6..d4d23026ee7cbb06bfa2279f2a9eadfcec99f4c2 100644 (file)
@@ -33,28 +33,8 @@ public class MJsonNumber extends MJsonValue {
 
     /* */
 
-    public MJsonNumber(byte x) throws MInvalidValueJsonException {
-        this((new Byte(x)).toString());
-    }
-
-    public MJsonNumber(double x) throws MInvalidValueJsonException {
-        this((new Double(x)).toString());
-    }
-
-    public MJsonNumber(float x) throws MInvalidValueJsonException {
-        this((new Float(x)).toString());
-    }
-
-    public MJsonNumber(int x) throws MInvalidValueJsonException {
-        this((new Integer(x)).toString());
-    }
-
-    public MJsonNumber(long x) throws MInvalidValueJsonException {
-        this((new Long(x)).toString());
-    }
-
-    public MJsonNumber(short x) throws MInvalidValueJsonException {
-        this((new Short(x)).toString());
+    public MJsonNumber() throws MInvalidValueJsonException {
+        this("0");
     }
 
     public MJsonNumber(String x) throws MInvalidValueJsonException {
@@ -66,7 +46,7 @@ public class MJsonNumber extends MJsonValue {
     public MJsonNumber clone() {
         MJsonNumber tmpMJsonNumber = null;
         try {
-            tmpMJsonNumber = new MJsonNumber(this.getFormattedValue());
+            tmpMJsonNumber = new MJsonNumber(this.getJsonValue());
         }
         catch (MInvalidValueJsonException exception) { // cannot happen
         }
@@ -139,7 +119,7 @@ public class MJsonNumber extends MJsonValue {
 
     /* Formatter */
 
-    public String getFormattedValue() {
+    public String getJsonValue() {
         return this.getValueReference().toString();
     }
 
index 439e3203280c3e14e56d67287e65d23a73ca0f33..b52af6031ef8f54ef4812efe630b5f1994c9436f 100644 (file)
@@ -56,7 +56,7 @@ public class MJsonObject extends MJsonValue {
     public MJsonObject clone() {
         MJsonObject tmpMJsonObject = null;
         try {
-            tmpMJsonObject = new MJsonObject(this.getFormattedValue());
+            tmpMJsonObject = new MJsonObject(this.getJsonValue());
         }
         catch (MInvalidValueJsonException exception) { // cannot happen
         }
@@ -260,7 +260,7 @@ public class MJsonObject extends MJsonValue {
 
     /* Formatter */
 
-    public String getFormattedValue() {
+    public String getJsonValue() {
         StringBuilder s = new StringBuilder("");
         s.append("{");
         int k = 0;
@@ -270,7 +270,7 @@ public class MJsonObject extends MJsonValue {
             }
             s.append("\"" + MJsonString.getEscapedString(key, false) + "\"");
             s.append(": ");
-            s.append(this.getValuesReference().get(key).getFormattedValue());
+            s.append(this.getValuesReference().get(key).getJsonValue());
             k++;
         }
         s.append("}");
index 1071267ccfe0ebb2d1f27c18b5e38229603684ba..99762e8944dfa044022965b31bfcdc270cef7eb7 100644 (file)
@@ -51,7 +51,7 @@ public class MJsonString extends MJsonValue {
     public MJsonString clone() {
         MJsonString tmpMJsonString = null;
         try {
-            tmpMJsonString = new MJsonString(this.getFormattedValue());
+            tmpMJsonString = new MJsonString(this.getJsonValue());
         }
         catch (MInvalidValueJsonException exception) { // cannot happen
         }
@@ -302,7 +302,7 @@ public class MJsonString extends MJsonValue {
 
     /* Formatter */
 
-    public String getFormattedValue() {
+    public String getJsonValue() {
         return "\"" + MJsonString.getEscapedString(this.getValue(), this.getExtendedEscapeMode()) + "\"";
     }
 
index f2ed6859f2d39c75c564737dd560321f0820fe79..5823304a24482418156e7fe354c02349963d8937 100644 (file)
@@ -57,6 +57,6 @@ public abstract class MJsonValue extends MObject {
 
     /* Formatter */
 
-    public abstract String getFormattedValue();
+    public abstract String getJsonValue();
 
 }
index 9aeef9b7ffcc34adb51504bc7589e161a2612d97..c04a63ca6d59f68d7c729a67b804b2d469a02dbb 100644 (file)
@@ -204,14 +204,14 @@ public abstract class MWebApplicationServlet extends MHttpServlet {
             MJsonObject returnValue = null;
             try {
                 returnValue = new MJsonObject();
-                returnValue.setValue("errorMode", new MJsonNumber(MWebApplicationServlet.ErrorMode.CLEAN.ordinal()));
+                returnValue.setValue("errorMode", new MJsonNumber("" + MWebApplicationServlet.ErrorMode.CLEAN.ordinal()));
                 MJsonString jsonParameter = new MJsonString();
                 jsonParameter.setValue(new String(content, MInformation.TEXT_ENCODING));
                 returnValue.setValue("parameter", jsonParameter);
             }
             catch (MInvalidValueJsonException exception) { // cannot happen
             }
-            responseContent = returnValue.getFormattedValue().getBytes(MInformation.TEXT_ENCODING);
+            responseContent = returnValue.getJsonValue().getBytes(MInformation.TEXT_ENCODING);
         }
         else {
             responseContent = content;
@@ -244,7 +244,7 @@ public abstract class MWebApplicationServlet extends MHttpServlet {
                     MJsonObject returnValue = null;
                     try {
                         returnValue = new MJsonObject();
-                        returnValue.setValue("errorMode", new MJsonNumber(errorMode.ordinal()));
+                        returnValue.setValue("errorMode", new MJsonNumber("" + errorMode.ordinal()));
                         MJsonString jsonParameter = new MJsonString();
                         if (null != content.getMessage()) {
                             jsonParameter.setValue(content.getClass().getName() + ": " + content.getMessage());
@@ -256,7 +256,7 @@ public abstract class MWebApplicationServlet extends MHttpServlet {
                     }
                     catch (MInvalidValueJsonException exception) { // should not happen
                     }
-                    responseContent = returnValue.getFormattedValue().getBytes(MInformation.TEXT_ENCODING);
+                    responseContent = returnValue.getJsonValue().getBytes(MInformation.TEXT_ENCODING);
                 }
                 else {
                     if (null != content.getMessage()) {
index 5e26931b3aef67ffa6481179f24497728ecefddf..52ea5011e9ba51c60f42e595fe7664b944813f2c 100644 (file)
@@ -53,7 +53,7 @@ public class MWebDownloader extends MWebWidget {
         //
         MWebBrowserPage browserPage = this.getViewReference().getBrowserPageReference();
         String destinationUrl = this.getApplicationContextReference().getRequestReference().getRequestURL().toString();
-        String parameters = "securityId=" + browserPage.getSecurityId() + "&message=" + downloadMessage.getFormattedValue();
+        String parameters = "securityId=" + browserPage.getSecurityId() + "&message=" + downloadMessage.getJsonValue();
         //
         this.getApplicationContextReference().addPlainTextResponseContent(String.format("$('m_downloader').src = '%s?%s';", destinationUrl, parameters));
     }
index 7949d9a2e1b961835715ff426c5fecfd0a010245..7ad9bf75667ed2bb439fd91ee7c7990f6e29a2d1 100644 (file)
@@ -96,25 +96,25 @@ public class MWebMessage extends MObject {
     public MWebMessage clone() {
         try {
             MJsonString tmpString = null;
-            MJsonObject tmpMessage = new MJsonObject("{}");
+            MJsonObject tmpMessage = new MJsonObject();
             //
-            tmpString = new MJsonString("\"\"");
+            tmpString = new MJsonString();
             tmpString.setValue(this.getWidgetId());
             tmpMessage.setValue("widgetId", tmpString);
             //
-            tmpString = new MJsonString("\"\"");
+            tmpString = new MJsonString();
             tmpString.setValue(this.getEvent());
             tmpMessage.setValue("event", tmpString);
             //
-            MJsonObject tmpMessageParameters = new MJsonObject("{}");
+            MJsonObject tmpMessageParameters = new MJsonObject();
             for (String parameterKey: this.getParametersReference().keySet()) {
-                tmpString = new MJsonString("\"\"");
+                tmpString = new MJsonString();
                 tmpString.setValue(this.getParameters().get(parameterKey));
                 tmpMessageParameters.setValue(parameterKey, tmpString);
             }
             tmpMessage.setValue("parameters", tmpMessageParameters);
             //
-            return new MWebMessage(tmpMessage.getFormattedValue());
+            return new MWebMessage(tmpMessage.getJsonValue());
         }
         catch (MInvalidValueJsonException exception) { // cannot happen
             return null; // necessary to avoid Java compilation errors