Skip to content

Commit e9bb393

Browse files
committed
Create JSONArray from multiple objects
1 parent 2210525 commit e9bb393

14 files changed

Lines changed: 247 additions & 150 deletions

File tree

.classpath

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="output" path="target/classes"/>
27+
</classpath>

.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>json.mapper</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#Tue Oct 29 15:02:41 CDT 2013
2+
encoding//src/test/java=ISO-8859-1
3+
encoding/<project>=UTF-8
4+
eclipse.preferences.version=1
5+
encoding//src/main/java=ISO-8859-1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Tue Oct 29 15:02:41 CDT 2013
2+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
4+
eclipse.preferences.version=1
5+
org.eclipse.jdt.core.compiler.source=1.5
6+
org.eclipse.jdt.core.compiler.compliance=1.5
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#Tue Oct 29 15:02:39 CDT 2013
2+
resolveWorkspaceProjects=true
3+
version=1
4+
activeProfiles=
5+
eclipse.preferences.version=1

pom.xml

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
44

5-
<groupId>dummiesmind</groupId>
6-
<artifactId>json.mapper</artifactId>
7-
<version>0.0.1-SNAPSHOT</version>
8-
<packaging>jar</packaging>
5+
<groupId>dummiesmind</groupId>
6+
<artifactId>json.mapper</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
99

10-
<name>json.mapper</name>
11-
<url>http://maven.apache.org</url>
10+
<name>json.mapper</name>
11+
<url>http://maven.apache.org</url>
1212

13-
<properties>
14-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
</properties>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>2.3.1</version>
23+
<configuration>
24+
<source>1.5</source>
25+
<target>1.5</target>
26+
<encoding>ISO-8859-1</encoding>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</build>
1631

17-
1832
<dependencies>
1933
<dependency>
2034
<groupId>junit</groupId>
@@ -27,6 +41,12 @@
2741
<artifactId>ognl</artifactId>
2842
<version>3.0.6</version>
2943
</dependency>
44+
<dependency>
45+
<groupId>javassist</groupId>
46+
<artifactId>javassist</artifactId>
47+
<version>3.11.0.GA</version>
48+
</dependency>
49+
3050
<dependency>
3151
<groupId>org.mockito</groupId>
3252
<artifactId>mockito-all</artifactId>
@@ -40,7 +60,8 @@
4060
<dependency>
4161
<groupId>org.json</groupId>
4262
<artifactId>json</artifactId>
43-
<version>20090211</version>
63+
<version>20080701</version>
4464
</dependency>
65+
4566
</dependencies>
4667
</project>
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.dummiesmind.json;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54
import java.util.Map;
65
import java.util.Map.Entry;
@@ -12,42 +11,39 @@
1211
import org.json.JSONException;
1312
import org.json.JSONObject;
1413

15-
import com.dummiesmind.beans.Task;
16-
1714
public class JSONProducer {
1815

19-
public JSONObject createJSONObjectForProperty(String expression, Object context) throws JSONException, OgnlException {
16+
public JSONObject createJSONObjectForProperty(List<String> expressions,
17+
Object context) throws JSONException, OgnlException {
2018
JSONObject jsonObject = new JSONObject();
21-
jsonObject.put(expression, evaluateExpressionUsingOGNL(expression, context));
19+
20+
for (String expression : expressions) {
21+
22+
Object value = evaluateExpressionUsingOGNL(expression, context);
23+
if (value != null) {
24+
jsonObject.put(expression, value);
25+
}
26+
}
2227
return jsonObject;
2328
}
2429

30+
public <T extends Object> JSONArray createJSONArrayForMultipleObjects(
31+
Map<T, List<String>> testData) throws JSONException, OgnlException {
32+
JSONArray response = new JSONArray();
2533

26-
public List<JSONArray> createJSONArrayListForMultipleObjectsProperties(Map<Task, List<String>> testData) throws JSONException, OgnlException {
27-
List<JSONArray> response = new ArrayList<JSONArray>();
28-
29-
for(Entry<Task, List<String>> entry : testData.entrySet()){
30-
JSONArray array = createJSONArrayForProperties(entry.getValue(), entry.getKey());
31-
response.add(array);
34+
for (Entry<T, List<String>> entry : testData.entrySet()) {
35+
JSONObject object = createJSONObjectForProperty(entry.getValue(),
36+
entry.getKey());
37+
if (object != null) {
38+
response.put(object);
39+
}
3240
}
33-
3441

3542
return response;
3643
}
3744

38-
39-
public JSONArray createJSONArrayForProperties(List<String> expressions, Object context) throws JSONException, OgnlException {
40-
JSONArray array = new JSONArray();
41-
42-
for(String expression : expressions){
43-
array.put(createJSONObjectForProperty(expression, context));
44-
}
45-
46-
return array;
47-
}
48-
49-
50-
private Object evaluateExpressionUsingOGNL(String expression, Object root) throws OgnlException {
45+
private Object evaluateExpressionUsingOGNL(String expression, Object root)
46+
throws OgnlException {
5147
return Ognl.getValue(expression, root);
5248
}
5349
}

src/test/java/com/dummiesmind/json/JSONProducerTest.java

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.util.List;
1010
import java.util.Map;
1111

12+
1213
import org.json.JSONArray;
14+
import org.json.JSONException;
1315
import org.json.JSONObject;
1416
import org.junit.Before;
1517
import org.junit.Test;
@@ -29,73 +31,72 @@ public void setup() {
2931

3032
@Test
3133
public void createsJSONObjectForProperty_ReturnsJSONObject() throws Exception {
32-
JSONObject results = jsonProducer.createJSONObjectForProperty("description", new Task(null,
34+
JSONObject results = jsonProducer.createJSONObjectForProperty(asList("description"), new Task(null,
3335
null, null));
3436

3537
assertThat(results, instanceOf(JSONObject.class));
3638
}
3739

40+
3841
@Test
3942
public void createsJSONObjectForProperty_GivenTask_ReturnsJSONObject_WithPropertyNameAsKeyAndPropertyValueAsValue()
4043
throws Exception {
4144
Task task = new Task(null, null, "My first task");
4245

43-
JSONObject results = jsonProducer.createJSONObjectForProperty("description", task);
46+
JSONObject results = jsonProducer.createJSONObjectForProperty(asList("description"), task);
4447

4548
assertThat(results.getString("description"), is("My first task"));
4649
}
4750

4851

4952

5053
@Test
51-
public void createsJSONObjectForProperty_GivenNote_ReturnsJSONObject_WithPropertyNameAsKeyAndPropertyValueAsValue()
52-
throws Exception {
53-
Note note = new Note("My first note", null);
54-
55-
JSONObject results = jsonProducer.createJSONObjectForProperty("text", note);
56-
57-
assertThat(results.getString("text"), is("My first note"));
58-
}
59-
60-
@Test
61-
public void createsJSONObjectForProperty_GivenNote_ReturnsJSONObject_WithDeepRelation()
54+
public void createsJSONObjectForProperty_GivenNote_ReturnsJSONObject_WithDeepRelation_usingOGNL ()
6255
throws Exception {
63-
Note note = new Note("My first note", new Owner("Pawan", "pawanspace@gmail.com"));
56+
Note note = new Note("My first note", new Owner("Pawan", "pawan@gmail.com"));
6457

65-
JSONObject results = jsonProducer.createJSONObjectForProperty("writer.name", note);
58+
JSONObject results = jsonProducer.createJSONObjectForProperty(asList("writer.name"), note);
6659

6760
assertThat(results.getString("writer.name"), is("Pawan"));
6861
}
6962

7063

64+
7165
@Test
66+
@SuppressWarnings("unchecked")
7267
public void createsListOfJSONArrayReturns_MultipleValuesMappedInArray_ForMultipleObjects() throws Exception {
7368
Map<Task, List<String>> testData = createMapForTasksTestData();
7469

75-
List<JSONArray> results = jsonProducer.createJSONArrayListForMultipleObjectsProperties(testData);
70+
JSONArray results = jsonProducer.createJSONArrayForMultipleObjects(testData);
7671

77-
assertThat(results.size(), is(3));
72+
assertThat(results.length(), is(3));
73+
JSONObject jsonObject = getTask2(results);
74+
75+
assertThat(jsonObject.getString("description"), is("Task 2"));
76+
assertThat(jsonObject.get("notes"), instanceOf(List.class));
77+
assertThat(((List<Note>)jsonObject.get("notes")).get(0).getText(), is("My first note"));
78+
assertThat(jsonObject.getString("assignee.name"), is("Varun"));
79+
assertThat(jsonObject.getString("assignee.email"), is("[email protected]"));
80+
7881
}
79-
80-
@Test
81-
public void createsJSONArrayReturns_MultiplePropertiesForOneObjectInJSONArray() throws Exception {
82-
Owner writer = new Owner("Pawan", "[email protected]");
83-
Note note = new Note("My first note", writer);
84-
Task task = new Task(writer, asList(note), "Task 1");
8582

86-
JSONArray array = jsonProducer.createJSONArrayForProperties(asList("description", "notes", "assignee.name", "assignee.email"), task);
87-
88-
assertThat(array.getJSONObject(0).getString("description"), is("Task 1"));
89-
assertThat(array.getJSONObject(1).get("notes"), instanceOf(List.class));
90-
assertThat(array.getJSONObject(2).getString("assignee.name"), is("Pawan"));
91-
assertThat(array.getJSONObject(3).getString("assignee.email"), is("[email protected]"));
83+
private JSONObject getTask2(JSONArray results) throws JSONException {
84+
JSONObject jsonObject = null;
85+
86+
for (int i = 0; i < results.length(); i++) {
87+
JSONObject obj = (JSONObject)results.get(i);
88+
if(obj.getString("description").equals("Task 2")){
89+
jsonObject = obj;
90+
}
91+
}
92+
return jsonObject;
9293
}
9394

9495

9596
private Map<Task, List<String>> createMapForTasksTestData(){
9697
Map<Task, List<String>> testData = new HashMap<Task, List<String>>();
97-
Owner writer1 = new Owner("Pawan", "pawanspace@gmail.com");
98-
Owner writer2 = new Owner("Varun", "varunspace@gmail.com");
98+
Owner writer1 = new Owner("Pawan", "pawan@gmail.com");
99+
Owner writer2 = new Owner("Varun", "varun@gmail.com");
99100

100101
Note note1 = new Note("My first note", writer1);
101102
Note note2 = new Note("My second note", writer2);
@@ -105,7 +106,7 @@ private Map<Task, List<String>> createMapForTasksTestData(){
105106
Task task3 = new Task(writer1, null, "Task 3");
106107

107108
testData.put(task1, asList("description", "notes"));
108-
testData.put(task2, asList("description", "notes", "assignee.name"));
109+
testData.put(task2, asList("description", "notes", "assignee.name", "assignee.email"));
109110
testData.put(task3, asList("description", "assignee.name"));
110111

111112

-320 Bytes
Binary file not shown.
-800 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)