John Marsden avatar John Marsden committed 0205c7b

Upgraded Marshaller.

Unit Tests nearly back in place. This version should be backwards compatible with the version before the inspector re-write.

Still some work to do.

Comments (0)

Files changed (11)

src/main/java/cc/plural/jsonij/marshal/JavaMarshalerObjects.java

 
         HashMap<String, Value> valueCollector = new HashMap<String, Value>();
         List<ClassProperty> properties = inspection.getProperties();
-        String name = null;
         Value value;
         int propCount = 0;
         for (ClassProperty property : properties) {
                 value = JSON.NULL;
             }
             propCount++;
-            valueCollector.put(name, value);
+            valueCollector.put(property.getPropertyName(), value);
         }
         if (inspection.hasInnerList()) {
-            if (propCount > 0) {
+            if (JSONMarshaler.ALWAYS_USE_INNER_PROPERTY || propCount > 0) {
                 valueCollector.put(JSONMarshaler.INNER_ARRAY_PROPERTY, marshaler.marshalJavaList(o, cycleDetector));
             } else {
                 return marshaler.marshalJavaList(o, cycleDetector);
             }
         }
         if (inspection.hasInnerMap()) {
-            if (propCount > 0) {
+            if (JSONMarshaler.ALWAYS_USE_INNER_PROPERTY || propCount > 0) {
                 valueCollector.put(JSONMarshaler.INNER_OBJECT_PROPERTY, marshaler.marshalJavaMap(o, cycleDetector));
             } else {
                 return marshaler.marshalJavaMap(o, cycleDetector);

src/main/java/cc/plural/jsonij/reflect/InspectionFactory.java

 
         Inspection inspection = new Inspection();
         inspection.setKlass(klass);
-        inspection.setProperties(tempProperties);
+        inspection.setProperties(classProperties);
         inspection.setInnerList(isListType(klass));
         inspection.setInnerMap(isMapType(klass));
         return inspection;

src/test/java/cc/plural/jsonij/marshal/JSONDocumentMarshalerTest.java

 
     @Test
     public void testMarshalJSONStringIntoIntArray() throws JSONMarshalerException, IOException, ParserException {
-        //System.out.println("marshal JSON into Array");
-        //String inputJSONDocument = "[1,2,3,4,5]";
-        //JSON inputJSON = JSON.parse(inputJSONDocument);
-        //Object marshal = JSONMarshaler.marshalJSON(inputJSON, int.class);
-        //JSON outputJSON = JSONMarshaler.marshalObject(marshal);
-        //System.out.println(String.format("InputJSON: %s", inputJSON));
-        //System.out.println(String.format("MarshaledObjectToString: %s", marshal));
-        //System.out.println(String.format("OutputJSON: %s", outputJSON));
-        //assertNotNull(marshal);
-        //assertEquals(inputJSON, outputJSON);
-    }
-
-    @Test
-    public void testMarshalJSONStringIntoObject() throws JSONMarshalerException, IOException, ParserException {
-//        System.out.println("marshal JSON into Object");
-//        String inputJSONDocument = "{\"message\":\"Hello JSON!\", \"rah\":-69, \"id\": 585757346726,    \"flag\": true, \"innerDoubleArray\": [1], \"inner\": {\"someValue\": \"hmmmn...\", \"rah\": [5,4,3,2,1]}}";
-//
-//        JSON inputJSON = JSON.parse(inputJSONDocument);
-//        Object marshal = JSONMarshaler.marshalJSON(inputJSON, JSONObject.class);
+        System.out.println("marshal JSON into Array");
+        String inputJSONDocument = "[1,2,3,4,5]";
+        JSON inputJSON = JSON.parse(inputJSONDocument);
+//        Object marshal = JSONMarshaler.marshalJSON(inputJSON, int.class);
 //        JSON outputJSON = JSONMarshaler.marshalObject(marshal);
 //        System.out.println(String.format("InputJSON: %s", inputJSON));
 //        System.out.println(String.format("MarshaledObjectToString: %s", marshal));
 //        System.out.println(String.format("OutputJSON: %s", outputJSON));
-//
 //        assertNotNull(marshal);
 //        assertEquals(inputJSON, outputJSON);
     }
 
     @Test
+    public void testMarshalJSONStringIntoObject() throws JSONMarshalerException, IOException, ParserException {
+        System.out.println("marshal JSON into Object");
+        String inputJSONDocument = "{\"message\":\"Hello JSON!\", \"rah\":-69, \"id\": 585757346726,    \"flag\": true, \"innerDoubleArray\": [1], \"inner\": {\"someValue\": \"hmmmn...\", \"rah\": [5,4,3,2,1]}}";
+
+        JSON inputJSON = JSON.parse(inputJSONDocument);
+        Object marshal = JSONMarshaler.marshalJSON(inputJSON, JSONObject.class);
+        JSON outputJSON = JSONMarshaler.marshalObject(marshal);
+        System.out.println(String.format("InputJSON: %s", inputJSON));
+        System.out.println(String.format("MarshaledObjectToString: %s", marshal));
+        System.out.println(String.format("OutputJSON: %s", outputJSON));
+
+        assertNotNull(marshal);
+        assertEquals(inputJSON, outputJSON);
+    }
+
+    @Test
     public void testMarshalHandlerClass() throws JSONMarshalerException, IOException, ParserException, IllegalAccessException, InstantiationException, ClassNotFoundException {
         System.out.println("testMarshalHandlerClass");
         HandlerConfiguration config = new HandlerConfiguration();

src/test/java/cc/plural/jsonij/marshal/MapInnerObjectMarshalTests.java

 import cc.plural.jsonij.marshal.helpers.MapTypeNoProps;
 import cc.plural.jsonij.marshal.helpers.MapTypeProps;
 import cc.plural.jsonij.parser.ParserException;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
 import org.junit.Test;
-
-import static org.junit.Assert.assertTrue;
-
+import org.junit.BeforeClass;
+import static org.junit.Assert.*;
 /**
  *
  * @author jmarsden
     public MapInnerObjectMarshalTests() {
     }
 
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+    }
+
+    @Before
+    public void setUp() {
+    }
+
+    @After
+    public void tearDown() {
+    }
+
     @Test
     public void testMapNoPropsPrint() throws ParserException, IOException {
         System.out.println("testMapNoPropsPrint");
 
         JSONMarshaler.ALWAYS_USE_INNER_PROPERTY = false;
         JSON json1 = JSONMarshaler.marshalObject(map1);
-        System.out.println(json1.toJSON());
+        JSON jsonExpected1 = JSON.parse("{\"key2\":\"value2\",\"key1\":\"value1\"}");
         
-        assertTrue(json1.equals(JSON.parse("{\"key2\":\"value2\",\"key1\":\"value1\"}")));
+        System.out.println("Parsed:" + json1.toJSON());
+        System.out.println("Expected:" + jsonExpected1.toJSON());
+        assertTrue(json1.equals(jsonExpected1));
 
         JSONMarshaler.ALWAYS_USE_INNER_PROPERTY = true;
         JSON json2 = JSONMarshaler.marshalObject(map1);
-        System.out.println(json2.toJSON());
         
-        assertTrue(json2.equals(JSON.parse("{\"$innerObject\":{\"key2\":\"value2\",\"key1\":\"value1\"}}")));
+        JSON jsonExpected2 = JSON.parse("{\"$innerObject\":{\"key2\":\"value2\",\"key1\":\"value1\"}}");
+        System.out.println("Parsed:" + json2.toJSON());
+        System.out.println("Expected:" + jsonExpected2.toJSON());
+        assertTrue(json2.equals(jsonExpected2));
     }
 
     @Test

src/test/java/cc/plural/jsonij/reflect/InspectionFactoryTest.java

  */
 package cc.plural.jsonij.reflect;
 
+import cc.plural.jsonij.reflect.helpers.TestClass2;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Before;

src/test/java/cc/plural/jsonij/reflect/TestClass2.java

-/**
- * Copyright (C) 2012 J.W.Marsden <jmarsden@plural.cc>
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package cc.plural.jsonij.reflect;
-
-/**
- *
- * @author jmarsden@plural.cc
- */
-public class TestClass2 extends TestClassOne {
-
-    public int intVariable;
-    
-    protected String testString;
-    
-    private float floatVariable;
-    
-    public TestClass2() {
-        
-    }
-
-    public float getFloatVariable() {
-        return floatVariable;
-    }
-
-    public void setFloatVariable(float floatVariable) {
-        this.floatVariable = floatVariable;
-    }
-
-    public int getIntVariable() {
-        return intVariable;
-    }
-
-    public void setIntVariable(int intVariable) {
-        this.intVariable = intVariable;
-    }
-}

src/test/java/cc/plural/jsonij/reflect/TestClassOne.java

-/**
- * Copyright (C) 2012 J.W.Marsden <jmarsden@plural.cc>
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package cc.plural.jsonij.reflect;
-
-import java.util.List;
-
-/**
- *
- * @author jmarsden@plural.cc
- */
-public class TestClassOne {
-
-    public TestMapClass<List<String>, String> testMap;
-    
-    private String testString;
-    
-    public TestClassOne() {
-        testMap = new TestMapClass<List<String>, String>();
-        testString = null;
-    }
-
-    public TestMapClass getTestMap() {
-        return testMap;
-    }
-
-    public void setTestMap(TestMapClass testMap) {
-        this.testMap = testMap;
-    }
-
-    public String getTestString() {
-        return testString;
-    }
-
-    public void setTestString(String testString) {
-        this.testString = testString;
-    }
-}

src/test/java/cc/plural/jsonij/reflect/TestMapClass.java

-/**
- * Copyright (C) 2012 J.W.Marsden <jmarsden@plural.cc>
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-package cc.plural.jsonij.reflect;
-
-import java.util.HashMap;
-
-/**
- *
- * @author jmarsden@plural.cc
- */
-public class TestMapClass<K,V extends String> extends HashMap<K,V> {
-
-}

src/test/java/cc/plural/jsonij/reflect/helpers/TestClass2.java

+/**
+ * Copyright (C) 2012 J.W.Marsden <jmarsden@plural.cc>
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package cc.plural.jsonij.reflect.helpers;
+
+/**
+ *
+ * @author jmarsden@plural.cc
+ */
+public class TestClass2 extends TestClassOne {
+
+    public int intVariable;
+    
+    protected String testString;
+    
+    private float floatVariable;
+    
+    public TestClass2() {
+        
+    }
+
+    public float getFloatVariable() {
+        return floatVariable;
+    }
+
+    public void setFloatVariable(float floatVariable) {
+        this.floatVariable = floatVariable;
+    }
+
+    public int getIntVariable() {
+        return intVariable;
+    }
+
+    public void setIntVariable(int intVariable) {
+        this.intVariable = intVariable;
+    }
+}

src/test/java/cc/plural/jsonij/reflect/helpers/TestClassOne.java

+/**
+ * Copyright (C) 2012 J.W.Marsden <jmarsden@plural.cc>
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package cc.plural.jsonij.reflect.helpers;
+
+import java.util.List;
+
+/**
+ *
+ * @author jmarsden@plural.cc
+ */
+public class TestClassOne {
+
+    public TestMapClass<List<String>, String> testMap;
+    
+    private String testString;
+    
+    public TestClassOne() {
+        testMap = new TestMapClass<List<String>, String>();
+        testString = null;
+    }
+
+    public TestMapClass getTestMap() {
+        return testMap;
+    }
+
+    public void setTestMap(TestMapClass testMap) {
+        this.testMap = testMap;
+    }
+
+    public String getTestString() {
+        return testString;
+    }
+
+    public void setTestString(String testString) {
+        this.testString = testString;
+    }
+}

src/test/java/cc/plural/jsonij/reflect/helpers/TestMapClass.java

+/**
+ * Copyright (C) 2012 J.W.Marsden <jmarsden@plural.cc>
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package cc.plural.jsonij.reflect.helpers;
+
+import java.util.HashMap;
+
+/**
+ *
+ * @author jmarsden@plural.cc
+ */
+public class TestMapClass<K,V extends String> extends HashMap<K,V> {
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.