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 ve...@apache.org on 2012/04/30 23:22:36 UTC

svn commit: r1332402 - in /axis/axis2/java/core/trunk/modules/json: src/org/apache/axis2/json/ test/org/apache/axis2/json/

Author: veithen
Date: Mon Apr 30 21:22:35 2012
New Revision: 1332402

URL: http://svn.apache.org/viewvc?rev=1332402&view=rev
Log:
Improving the JSON code - Step 2 - Instead of using some hacks to extract the name of the element, just let Axiom determine it lazily using the feature introduced by AXIOM-399.

This should make databindings work with Badgerfish (although we have no integration test for this yet) and solve AXIS2-5158, AXIS2-5295 and AXIS2-5300.

Modified:
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
    axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.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/AbstractJSONDataSource.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java?rev=1332402&r1=1332401&r2=1332402&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java Mon Apr 30 21:22:35 2012
@@ -39,11 +39,9 @@ public abstract class AbstractJSONDataSo
     private Reader jsonReader;
     private String jsonString;
     private boolean isRead = false;
-    protected String localName;
 
-    public AbstractJSONDataSource(Reader jsonReader, String localName) {
+    public AbstractJSONDataSource(Reader jsonReader) {
         this.jsonReader = jsonReader;
-        this.localName = localName;
     }
 
     /**
@@ -83,8 +81,4 @@ public abstract class AbstractJSONDataSo
             return jsonString;
         }
     }
-
-    public String getCompleteJOSNString() {
-        return "{" + localName + ":" + getJSONString();
-    }
 }

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=1332402&r1=1332401&r2=1332402&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 Apr 30 21:22:35 2012
@@ -22,7 +22,6 @@ package org.apache.axis2.json;
 import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMFactory;
-import org.apache.axiom.om.OMNamespace;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
 import org.apache.axis2.addressing.EndpointReference;
@@ -30,7 +29,6 @@ import org.apache.axis2.builder.Builder;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.transport.http.util.URIEncoderDecoder;
 
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
@@ -58,9 +56,6 @@ public abstract class AbstractJSONOMBuil
     public OMElement processDocument(InputStream inputStream, String contentType,
                                      MessageContext messageContext) throws AxisFault {
         OMFactory factory = OMAbstractFactory.getOMFactory();
-        String localName = "";
-        String prefix = "";
-        OMNamespace ns = factory.createOMNamespace("", "");
 
         //sets DoingREST to true because, security scenarios needs to handle in REST way
         messageContext.setDoingREST(true);
@@ -108,62 +103,8 @@ public abstract class AbstractJSONOMBuil
             }
         }
 
-        /*
-        Now we have to read the localname and prefix from the input stream
-        if there is not prefix, message starts like {"foo":
-        if there is a prefix, message starts like {"prefix:foo":
-         */
-        try {
-            //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 == '{') {
-                    bracketCounter++;
-                } else if (temp == '}') {
-                    bracketCounter--;
-                } else if (temp != ' ' && temp != '\n' && temp != '\r' && temp != '\t') {
-                    localName += temp;
-                }
-
-
-                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.length() > 0 && localName.charAt(0) == '"') {
-                if (localName.charAt(localName.length() - 1) == '"') {
-                    localName = localName.substring(1, localName.length() - 1);
-                } else {
-                    prefix = localName.substring(1, localName.length()) + ":";
-                    localName = "";
-                    //so far we have read only the prefix, now lets read the localname
-                    temp = (char)reader.read();
-                    while (temp != ':') {
-                        if (temp != ' ') {
-                            localName += temp;
-                        }
-                        temp = (char)reader.read();
-                    }
-                    localName = localName.substring(0, localName.length() - 1);
-                }
-            }
-        } catch (IOException e) {
-            throw AxisFault.makeFault(e);
-        }
-		if (localName != null && localName.length() > 0) {
-            AbstractJSONDataSource jsonDataSource = getDataSource(reader, prefix, localName);
-            return factory.createOMElement(jsonDataSource, localName, ns);
-        }else{
-            return null;
-        }
+        return factory.createOMElement(getDataSource(reader));
     }
 
-    protected abstract AbstractJSONDataSource getDataSource(Reader
-            jsonReader, String prefix, String localName);
+    protected abstract AbstractJSONDataSource getDataSource(Reader jsonReader);
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java?rev=1332402&r1=1332401&r2=1332402&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java Mon Apr 30 21:22:35 2012
@@ -31,8 +31,8 @@ import java.io.Reader;
 
 public class JSONBadgerfishDataSource extends AbstractJSONDataSource {
 
-    public JSONBadgerfishDataSource(Reader jsonReader, String localName) {
-        super(jsonReader, localName);
+    public JSONBadgerfishDataSource(Reader jsonReader) {
+        super(jsonReader);
     }
 
     /**
@@ -48,7 +48,7 @@ public class JSONBadgerfishDataSource ex
         //input factory for "Badgerfish"
         BadgerFishXMLInputFactory inputFactory = new BadgerFishXMLInputFactory();
         return inputFactory.createXMLStreamReader(
-                new JSONTokener("{" + localName + ":" + this.getJSONString()));
+                new JSONTokener(getJSONString()));
 
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java?rev=1332402&r1=1332401&r2=1332402&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java Mon Apr 30 21:22:35 2012
@@ -50,7 +50,7 @@ public class JSONBadgerfishMessageFormat
     @Override
     protected String getStringToWrite(OMDataSource dataSource) {
         if (dataSource instanceof JSONBadgerfishDataSource) {
-            return ((JSONBadgerfishDataSource)dataSource).getCompleteJOSNString();
+            return ((JSONBadgerfishDataSource)dataSource).getJSONString();
         } else {
             return null;
         }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java?rev=1332402&r1=1332401&r2=1332402&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java Mon Apr 30 21:22:35 2012
@@ -28,8 +28,7 @@ import java.io.Reader;
 
 public class JSONBadgerfishOMBuilder extends AbstractJSONOMBuilder {
     @Override
-    protected AbstractJSONDataSource getDataSource(Reader jsonReader, String prefix,
-                                           String localName) {
-        return new JSONBadgerfishDataSource(jsonReader, "\"" + prefix + localName + "\"");
+    protected AbstractJSONDataSource getDataSource(Reader jsonReader) {
+        return new JSONBadgerfishDataSource(jsonReader);
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java?rev=1332402&r1=1332401&r2=1332402&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java Mon Apr 30 21:22:35 2012
@@ -33,8 +33,8 @@ import java.util.HashMap;
 
 public class JSONDataSource extends AbstractJSONDataSource {
 
-    public JSONDataSource(Reader jsonReader, String localName) {
-        super(jsonReader, localName);
+    public JSONDataSource(Reader jsonReader) {
+        super(jsonReader);
     }
 
     /**
@@ -52,7 +52,6 @@ public class JSONDataSource extends Abst
 
         //input factory for "Mapped" convention
         MappedXMLInputFactory inputFactory = new MappedXMLInputFactory(XMLToJSNNamespaceMap);
-        String jsonString = "{" + localName + ":" + this.getJSONString();
-        return inputFactory.createXMLStreamReader(new JSONTokener(jsonString));
+        return inputFactory.createXMLStreamReader(new JSONTokener(getJSONString()));
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java?rev=1332402&r1=1332401&r2=1332402&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java Mon Apr 30 21:22:35 2012
@@ -57,7 +57,7 @@ public class JSONMessageFormatter extend
     @Override
     protected String getStringToWrite(OMDataSource dataSource) {
         if (dataSource instanceof JSONDataSource) {
-            return ((JSONDataSource)dataSource).getCompleteJOSNString();
+            return ((JSONDataSource)dataSource).getJSONString();
         } else {
             return null;
         }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java?rev=1332402&r1=1332401&r2=1332402&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java Mon Apr 30 21:22:35 2012
@@ -25,8 +25,7 @@ import java.io.Reader;
 
 public class JSONOMBuilder extends AbstractJSONOMBuilder {
     @Override
-    protected AbstractJSONDataSource getDataSource(Reader
-            jsonReader, String prefix, String localName) {
-        return new JSONDataSource(jsonReader, "\"" + prefix + localName + "\"");
+    protected AbstractJSONDataSource getDataSource(Reader jsonReader) {
+        return new JSONDataSource(jsonReader);
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java?rev=1332402&r1=1332401&r2=1332402&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java (original)
+++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java Mon Apr 30 21:22:35 2012
@@ -31,7 +31,6 @@ import javax.xml.stream.XMLStreamWriter;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
-import java.io.Reader;
 import java.io.StringReader;
 
 public class JSONDataSourceTest extends XMLTestCase {
@@ -100,8 +99,7 @@ public class JSONDataSourceTest extends 
     }
 
     private JSONBadgerfishDataSource getBadgerfishDataSource(String jsonString) {
-        Reader jsonReader = new StringReader(jsonString);
-        return new JSONBadgerfishDataSource(readLocalName(jsonReader), "\"p\"");
+        return new JSONBadgerfishDataSource(new StringReader(jsonString));
     }
 
     private String getBadgerfishJSONString() {
@@ -109,22 +107,10 @@ public class JSONDataSourceTest extends 
     }
 
     private JSONDataSource getMappedDataSource(String jsonString) {
-        Reader jsonReader = new StringReader(jsonString);
-        return new JSONDataSource(readLocalName(jsonReader), "\"mapping\"");
+        return new JSONDataSource(new StringReader(jsonString));
     }
 
     private String getMappedJSONString() {
         return "{\"mapping\":{\"inner\":[{\"first\":\"test string one\"},\"test string two\"],\"name\":\"foo\"}}";
     }
-
-    private Reader readLocalName(Reader in) {
-        try {
-            while ((char)in.read() != ':') {
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return in;
-    }
-
 }

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=1332402&r1=1332401&r2=1332402&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 Apr 30 21:22:35 2012
@@ -24,6 +24,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
+import javax.xml.namespace.QName;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
 
@@ -33,6 +34,7 @@ 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.builder.Builder;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.transport.TransportUtils;
 import org.apache.axis2.transport.http.SOAPMessageFormatter;
@@ -40,6 +42,20 @@ import org.codehaus.jettison.json.JSONEx
 import org.xml.sax.SAXException;
 
 public class JSONOMBuilderTest extends TestCase {
+    public void testBadgerfishQName() throws Exception {
+        String jsonString = getBadgerfishJSONString();
+        ByteArrayInputStream inStream = new ByteArrayInputStream(jsonString.getBytes("utf-8"));
+
+        MessageContext msgCtx = new MessageContext();
+        Builder builder = new JSONBadgerfishOMBuilder();
+        OMElement elem = builder.processDocument(inStream,
+                JSONTestConstants.CONTENT_TYPE_BADGERFISH, msgCtx);
+        
+        QName qname = elem.getQName();
+        assertEquals("http://def.ns", qname.getNamespaceURI());
+        assertEquals("p", qname.getLocalPart());
+        assertEquals("", qname.getPrefix());
+    }
 
     public void testBadgerfishOMSerialization1() throws IOException {
 
@@ -96,7 +112,8 @@ public class JSONOMBuilderTest extends T
         JSONOMBuilder omBuilder = new JSONOMBuilder();
         OMElement elem = omBuilder.processDocument(inStream,
                 JSONTestConstants.CONTENT_TYPE_BADGERFISH, messageContext);
-        assertEquals(null ,elem);
+        // TODO: not sure why we would want to send an empty list...
+//        assertEquals(null ,elem);
     }
 
     private String getBadgerfishJSONString() {