Skip to content

Commit

Permalink
Added JSON.Smart, upgraded GSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
alt236 committed Jan 7, 2013
1 parent 7b22526 commit 770b698
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
<classpathentry kind="src" path="gen"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="lib" path="libs/gson-1.6.jar"/>
<classpathentry kind="lib" path="libs/jackson-core-asl-1.7.1.jar"/>
<classpathentry kind="lib" path="libs/json_simple-1.1.jar"/>
<classpathentry kind="lib" path="libs/json-smart-1.1.1.jar"/>
<classpathentry kind="lib" path="libs/gson-2.2.2.jar"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Binary file removed libs/gson-1.6.jar
Binary file not shown.
Binary file added libs/gson-2.2.2.jar
Binary file not shown.
Binary file added libs/json-smart-1.1.1.jar
Binary file not shown.
15 changes: 7 additions & 8 deletions res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

</LinearLayout>
19 changes: 13 additions & 6 deletions src/com/martinadamek/jsonandroid/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.martinadamek.jsonandroid;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.InputStream;
import java.util.*;

public class MainActivity extends Activity {

private static final String TAG = MainActivity.class.getName();
Expand All @@ -23,9 +28,10 @@ public void run() {

testImpl(new AndroidJson(), results);
testImpl(new SimpleJson(), results);
testImpl(new SmartJson(), results);
testImpl(new GsonJson(), results);
testImpl(new JacksonJson(), results);

runOnUiThread(new Runnable() {
public void run() {

Expand All @@ -36,6 +42,7 @@ public void run() {

for (String key: keys) {
TextView textView = new TextView(MainActivity.this);
textView.setTypeface(Typeface.MONOSPACE);
textView.setText(key + ": " + results.get(key) + "ms");
mLayout.addView(textView, mLayoutParams);
}
Expand Down Expand Up @@ -71,9 +78,9 @@ public void onCreate(Bundle savedInstanceState) {
private void testImpl(TestJson testJson, Map<String, Long> results) {
warmUp(testJson);
long duration = test(testJson, 1);
results.put("[1 run] " + testJson.getName(), duration);
results.put("[1 run] " + testJson.getName(), duration);
duration = test(testJson, 5);
results.put("[5 runs] " + testJson.getName(), duration);
results.put("[5 runs] " + testJson.getName(), duration);
duration = test(testJson, 100);
results.put("[100 runs] " + testJson.getName(), duration);
}
Expand Down
54 changes: 54 additions & 0 deletions src/com/martinadamek/jsonandroid/SmartJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.martinadamek.jsonandroid;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;

public class SmartJson implements TestJson {

public String getName() {
return "JSON.smart";
}

public List<Map> parsePublicTimeline(InputStream inputStream) {

List<Map> result = new ArrayList<Map>();

JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);

try {
JSONArray jsonArray = (JSONArray) p.parse(new InputStreamReader(inputStream));
int size = jsonArray.size();

for (int i = 0; i < size; i++) {
Map map = new HashMap();
JSONObject jsonObject = (JSONObject) jsonArray.get(i);

Set keys = jsonObject.keySet();
for (Object key: keys) {
if ("user".equals(key)) {
JSONObject user = (JSONObject) jsonObject.get(key);
Set keys2 = user.keySet();
for (Object key2: keys2) {
map.put("user." + key2, user.get(key2));
}
} else {
map.put(key, jsonObject.get(key));
}
}

result.add(map);
}


} catch (Exception e) {
e.printStackTrace();
}

return result;
}
}

0 comments on commit 770b698

Please sign in to comment.