You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by sa...@apache.org on 2012/02/13 11:28:39 UTC

svn commit: r1243469 - in /axis/axis2/java/core/trunk/modules/json: src/org/apache/axis2/json/AbstractJSONOMBuilder.java test/org/apache/axis2/json/JSONOMBuilderTest.java

Author: sagara
Date: Mon Feb 13 10:28:39 2012
New Revision: 1243469

URL: http://svn.apache.org/viewvc?rev=1243469&view=rev
Log:
Applied the patch for AXIS2-5125.

Modified:
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java
    axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONOMBuilderTest.java

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java?rev=1243469&r1=1243468&r2=1243469&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java Mon Feb 13 10:28:39 2012
@@ -114,17 +114,29 @@ public abstract class AbstractJSONOMBuil
         if there is a prefix, message starts like {"prefix:foo":
          */
         try {
-            //read the stream until we find a : symbol
-            char temp = (char)reader.read();
+            //read the stream until we find a : symbol, break the loop if it encounter end of json String
+            char temp = (char) reader.read();
+            int bracketCounter = 0;        // counter increase by 1 when get a '{' and decrease by 1 when get a '}'
             while (temp != ':') {
-                if (temp != ' ' && temp != '{' && temp != '\n' && temp != '\r' && temp != '\t') {
+                if (temp == '{') {
+                    bracketCounter++;
+                } else if (temp == '}') {
+                    bracketCounter--;
+                } else if (temp != ' ' && temp != '\n' && temp != '\r' && temp != '\t') {
                     localName += temp;
                 }
-                temp = (char)reader.read();
+
+
+                if (bracketCounter > 0) {
+                    temp = (char) reader.read();
+                } else {
+                    break;
+                }
             }
 
             //if the part we read ends with ", there is no prefix, otherwise it has a prefix
-            if (localName.charAt(0) == '"') {
+
+            if (localName.length() > 0 && localName.charAt(0) == '"') {
                 if (localName.charAt(localName.length() - 1) == '"') {
                     localName = localName.substring(1, localName.length() - 1);
                 } else {
@@ -144,8 +156,12 @@ public abstract class AbstractJSONOMBuil
         } catch (IOException e) {
             throw AxisFault.makeFault(e);
         }
-        AbstractJSONDataSource jsonDataSource = getDataSource(reader, prefix, localName);
-        return factory.createOMElement(jsonDataSource, localName, ns);
+        if (localName != null && !localName.isEmpty()) {
+            AbstractJSONDataSource jsonDataSource = getDataSource(reader, prefix, localName);
+            return factory.createOMElement(jsonDataSource, localName, ns);
+        }else{
+            return null;
+        }
     }
 
     protected abstract AbstractJSONDataSource getDataSource(Reader

Modified: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONOMBuilderTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONOMBuilderTest.java?rev=1243469&r1=1243468&r2=1243469&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONOMBuilderTest.java (original)
+++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONOMBuilderTest.java Mon Feb 13 10:28:39 2012
@@ -18,7 +18,7 @@
  */
 
 
-/*package org.apache.axis2.json;
+package org.apache.axis2.json;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -32,6 +32,7 @@ import junit.framework.TestCase;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axis2.AxisFault;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.transport.TransportUtils;
 import org.apache.axis2.transport.http.SOAPMessageFormatter;
@@ -45,15 +46,15 @@ public class JSONOMBuilderTest extends T
         String jsonString = getBadgerfishJSONString();
         ByteArrayInputStream inStream = new ByteArrayInputStream(jsonString.getBytes());
 
-        JSONOMBuilder omBuilder = new JSONBadgerfishOMBuilder();
+        MessageContext msgCtx = new MessageContext();
+        JSONOMBuilder omBuilder = new JSONOMBuilder();
         OMElement elem = omBuilder.processDocument(inStream,
-                JSONTestConstants.CONTENT_TYPE_BADGERFISH, null);
+                JSONTestConstants.CONTENT_TYPE_BADGERFISH, msgCtx);
 
         elem.toString();
 
         SOAPEnvelope envelope = TransportUtils.createSOAPEnvelope(elem);
 
-        MessageContext msgCtx = new MessageContext();
         msgCtx.setEnvelope(envelope);
 
         OMOutputFormat outputFormat = new OMOutputFormat();
@@ -72,10 +73,11 @@ public class JSONOMBuilderTest extends T
             IOException, ParserConfigurationException, SAXException {
         String jsonString = getBadgerfishJSONString();
         ByteArrayInputStream inStream = new ByteArrayInputStream(jsonString.getBytes());
+        MessageContext msgCtx = new MessageContext();
 
-        JSONOMBuilder omBuilder = new JSONBadgerfishOMBuilder();
+        JSONOMBuilder omBuilder = new JSONOMBuilder();
         OMElement elem = omBuilder.processDocument(inStream,
-                JSONTestConstants.CONTENT_TYPE_BADGERFISH, null);
+                JSONTestConstants.CONTENT_TYPE_BADGERFISH, msgCtx);
 
         elem.toString();
 
@@ -86,9 +88,19 @@ public class JSONOMBuilderTest extends T
 
     }
 
+    public void testEmptyJsonString() throws AxisFault {
+        String emptyJson = "{}";
+        ByteArrayInputStream inStream = new ByteArrayInputStream(emptyJson.getBytes());
+        MessageContext messageContext = new MessageContext();
+
+        JSONOMBuilder omBuilder = new JSONOMBuilder();
+        OMElement elem = omBuilder.processDocument(inStream,
+                JSONTestConstants.CONTENT_TYPE_BADGERFISH, messageContext);
+        assertEquals(null ,elem);
+    }
+
     private String getBadgerfishJSONString() {
         return "{\"p\":{\"@xmlns\":{\"bb\":\"http://other.nsb\",\"aa\":\"http://other.ns\",\"$\":\"http://def.ns\"},\"sam\":{\"$\":\"555\", \"@att\":\"lets\"}}}";
     }
 
 }
-*/
\ No newline at end of file