Commits

b_lim  committed 62529bc

True typed array support added for formatter

  • Participants
  • Parent commits 85de282
  • Branches jsonintegrationadapter

Comments (0)

Files changed (3)

File src/nexj/core/integration/format/json/JSONMessageFormatter.java

 
    protected void beginWriteRootTyped(TransferObject tobj, Message message, Output out) throws IntegrationException, IOException
    {
+      MessagePart part;
+      String valueName = "";
+      Object toValue = null;
       CompositeMessagePart root = message.getRoot();
+      Iterator itr = root.getPartIterator();
 
-      // Root must contain only one message
-      if (root.getPartCount() != 1)
+      while (itr.hasNext())
       {
-         throw new IntegrationException("err.nexj.integration.multipleParts", new Object[]{root.getDeclarator()});
+         m_writer.beginObject();
+
+         part = (MessagePart)itr.next();
+         valueName = part.getName();
+
+         if (part instanceof CompositeMessagePartInstance)
+         {
+            toValue = getTransferObjectValue(tobj, valueName);
+            writeCompositeInstancePart(out, (CompositeMessagePartInstance)part, toValue, true);
+         }
+         else if (part instanceof PrimitiveMessagePart)
+         {
+            toValue = getTransferObjectValue(tobj, valueName);
+            writePrimitivePart(out, (PrimitiveMessagePart)part, toValue, true);
+         }
+
+         m_writer.endObject();
+         valueSeparator(itr.hasNext());
       }
 
-      CompositeMessagePartInstance typedArray = (CompositeMessagePartInstance)root.getPart(0);
-
-      String messageName = typedArray.getName();
-      Object messageNode = getTransferObjectValue(tobj, messageName);
-      if (! (messageNode instanceof List || messageNode instanceof TransferObject))
-      {
-         throw new IntegrationException("err.nexj.integration.messageTypeMismatch", new Object[]{root.getDeclarator(), new TransferObject(), messageNode.getClass()});
-      }
-
-      if (messageNode instanceof List)
-      {
-         Iterator itr = ((List) messageNode).iterator();
-         Object value;
-         while (itr.hasNext())
-         {
-            value = itr.next();
-            writeTypedMessage(typedArray, message, out, (TransferObject)value);
-            valueSeparator(itr.hasNext());
-         }
-      }
-      else
-      {
-         writeTypedMessage(typedArray, message, out, (TransferObject)messageNode);
-      }
-
-
    }
 
-   protected void writeTypedMessage(CompositeMessagePartInstance part, Message message, Output out, TransferObject value) throws IntegrationException, IOException
+   protected void writeTypedMessage(CompositeMessagePart part, Message message, Output out, TransferObject value) throws IntegrationException, IOException
    {
       if (value instanceof List)
       {

File src/nexj/core/scripting/JSONParser.java

File contents unchanged.

File test/nexj/core/integration/format/json/JSONMessageFormatterTest.java

                            "\"stringname\":\"stringval\"" +
                         "}" +
                      "]";
+      m_literal = Repository.getMetadata().getMessage("JSON_TestTypedArrayNew1");
+      TransferObject root = new TransferObject();
+      ((JSONMessagePartMapping)m_literal.getRoot().getMapping()).setIsTypedArray(true);
+
+      root.setValue("stringname", "stringval");
+
+      m_formatter.format(root, m_literal, new WriterOutput(m_writer));
+      assertEquals(case1, m_writer.toString());
 
       String case2 = "[" +
                         "{" +
                            "\"stringname\":\"stringval\"" +
                         "}," +
-                        "[" +
-                           "{" +
-                              "\"stringname2\":\"stringval2\"" +
-                           "}," +
-                           "{" +
-                              "\"stringname2\":\"stringval3\"" +
-                           "}" +
-                        "]" +
+                        "{" +
+                           "\"stringname2\":[" +
+                              "\"stringval1\"," +
+                              "\"stringval2\"," +
+                              "\"stringval3\"" +
+                           "]" +
+                        "}" +
                      "]";
+      m_literal = Repository.getMetadata().getMessage("JSON_TestTypedArrayNew2");
+
+      clear();
+      root = new TransferObject();
+      root.setValue("stringname", "stringval");
+      List list = new ArrayList();
+      list.add("stringval1");
+      list.add("stringval2");
+      list.add("stringval3");
+      root.setValue("stringname2", list);
+
+      ((JSONMessagePartMapping)m_literal.getRoot().getMapping()).setIsTypedArray(true);
+      m_formatter.format(root, m_literal, new WriterOutput(m_writer));
+      assertEquals(case2, m_writer.toString());
 
       String case3 = "[" +
                          "{" +
                            "\"value\":1" +
                          "}," +
-                         "[" +
-                            "{" +
-                               "\"value2\":2" +
-                            "}," +
-                            "{" +
-                               "\"value2\":3" +
-                            "}" +
-                         "]," +
+                         "{" +
+                            "\"value2\":[" +
+                               "2,3" +
+                            "]" +
+                         "}," +
                          "{" +
                             "\"message\":" +
                                "{" +
                          "}" +
                      "]";
 
+      clear();
+      root = new TransferObject();
+      root.setValue("value", new Integer(1));
+      list = new ArrayList();
+      list.add(new Integer(2));
+      list.add(new Integer(3));
+      root.setValue("value2", list);
+
+      TransferObject to = new TransferObject();
+      to.setValue("value3", new Integer(4));
+      root.setValue("message", to);
+
+      TransferObject message1_1 = new TransferObject();
+      TransferObject message1_2 = new TransferObject();
+      message1_1.setValue("value4", new Integer(5));
+      message1_2.setValue("value4", new Integer(6));
+      List list2 = new ArrayList();
+      list2.add(message1_1);
+      list2.add(message1_2);
+      root.setValue("message1", list2);
+
+      m_literal = Repository.getMetadata().getMessage("JSON_TestTypedArrayNew3");
+      ((JSONMessagePartMapping)m_literal.getRoot().getMapping()).setIsTypedArray(true);
+      m_formatter.format(root, m_literal, new WriterOutput(m_writer));
+      assertEquals(case3, m_writer.toString());
+
    }
 
    public void testCyclicReference() throws Exception