Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated parsing libraries #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.application'
apply plugin: 'com.getkeepsafe.dexcount'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 27
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.ilyaeremin.jsonparserscomparation"
minSdkVersion 15
targetSdkVersion 25
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -25,15 +25,16 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.jakewharton:butterknife:7.0.0'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

compile 'com.google.code.gson:gson:2.8.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.moshi:moshi:1.4.0'
compile 'com.fasterxml.jackson.core:jackson-databind:2.0.1'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
compile('com.afollestad:ason:1.1.0') {
exclude group: 'org.json', module: 'json'
}

compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:appcompat-v7:27.1.0'
testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
* Created by ereminilya on 5/3/17.
*/

public class GcTracker {
class GcTracker {
@Nullable private Thread mThread;

/**
* Starts listening for GC_FOR_ALLOC events
*/
public void startListening() {
void startListening() {
if (mThread != null) {
return;
}
Expand Down Expand Up @@ -54,7 +54,7 @@ public void run() {
/**
* Stops listening for GC_FOR_ALLOC events
*/
public void stopListening() {
void stopListening() {
if (mThread != null) {
mThread.interrupt();
mThread = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@

public class InitialActivity extends AppCompatActivity {

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_initial);
ButterKnife.bind(this);
}

@OnClick(R.id.small_json) void onSmallJsonClick() {
@OnClick(R.id.small_json)
public void onSmallJsonClick() {
startActivity(new Intent(this, ParseNTimesSmallJson.class));
}

@OnClick(R.id.big_json) void onBigJsonClick() {
@OnClick(R.id.big_json)
public void onBigJsonClick() {
startActivity(new Intent(this, ParseNTimesBigJsonActivity.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void onCreate(Bundle savedInstanceState) {
try {
long before = System.currentTimeMillis();
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker()
mapper.setVisibility(mapper.getVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@

public class ParseNTimesBigJsonActivity extends AppCompatActivity {

private static final int TIMES = 20;
private static final int TIMES = 1;

private final Type HUMAN_LIST_GSON = new TypeToken<ArrayList<Human>>() {
}.getType();

private String jsonString;
private Gson gson;
private ObjectMapper mapper;
private Moshi moshi;
private JsonAdapter<List<Human>> moshiAdapter;
private GcTracker gcTracker;

Expand All @@ -50,12 +49,12 @@ public class ParseNTimesBigJsonActivity extends AppCompatActivity {
jsonString = Utils.readJsonAsStringFromDisk(this, R.raw.big);
gson = new Gson();
mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker()
mapper.setVisibility(mapper.getVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
moshi = new Moshi.Builder().build();
Moshi moshi = new Moshi.Builder().build();
moshiAdapter = moshi.adapter(Types.newParameterizedType(List.class, Human.class));

gcTracker = new GcTracker();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,53 @@

public class ParseNTimesSmallJson extends AppCompatActivity {

private static final int TIMES = 1000;
private static final int TIMES = 1;

private final Type HUMAN_LIST_GSON = new TypeToken<ArrayList<SmallObject>>() {
}.getType();

private String jsonString;
private Gson gson;
private ObjectMapper mapper;
private Moshi moshi;
private String jsonString;
private Gson gson;
private ObjectMapper mapper;
private JsonAdapter<List<SmallObject>> moshiAdapter;
private GcTracker gcTracker;


@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
jsonString = Utils.readJsonAsStringFromDisk(this, R.raw.small);
gson = new Gson();
mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
moshi = new Moshi.Builder().build();
mapper.setVisibility(mapper.getVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
Moshi moshi = new Moshi.Builder().build();
moshiAdapter = moshi.adapter(Types.newParameterizedType(List.class, Human.class));
gcTracker = new GcTracker();
gcTracker.startListening();
}


@OnClick(R.id.gson) void onGsonClick() {
@OnClick(R.id.gson)
void onGsonClick() {
printToast(Utils.doNTimes(TIMES, new Runnable() {
@Override public void run() {
@Override
public void run() {
List<Human> humanList = gson.fromJson(jsonString, HUMAN_LIST_GSON);
}
}));
}

@OnClick(R.id.jackson) void onJacksonClick() {
@OnClick(R.id.jackson)
void onJacksonClick() {
printToast(Utils.doNTimes(TIMES, new Runnable() {
@Override public void run() {
@Override
public void run() {
try {
List<SmallObject> humanList = Arrays.asList(mapper.readValue(jsonString, SmallObject[].class));
} catch (IOException e) {
Expand All @@ -84,9 +88,11 @@ public class ParseNTimesSmallJson extends AppCompatActivity {
}


@OnClick(R.id.moshi) void onMoshiClick() {
@OnClick(R.id.moshi)
void onMoshiClick() {
printToast(Utils.doNTimes(TIMES, new Runnable() {
@Override public void run() {
@Override
public void run() {
try {
List<SmallObject> humans = moshiAdapter.fromJson(jsonString);
} catch (IOException e) {
Expand All @@ -96,9 +102,11 @@ public class ParseNTimesSmallJson extends AppCompatActivity {
}));
}

@OnClick(R.id.ason) void onAsonClick() {
@OnClick(R.id.ason)
void onAsonClick() {
printToast(Utils.doNTimes(TIMES, new Runnable() {
@Override public void run() {
@Override
public void run() {
List<Human> humans = Ason.deserializeList(jsonString, Human.class);
}
}));
Expand All @@ -109,7 +117,8 @@ private void printToast(long millis) {
System.out.println(millis);
}

@Override protected void onDestroy() {
@Override
protected void onDestroy() {
gcTracker.stopListening();
super.onDestroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* Created by ereminilya on 22/2/17.
*/

public class Utils {
public static String readJsonAsStringFromDisk(@NonNull Context context, @RawRes int rawRes) {
class Utils {
static String readJsonAsStringFromDisk(@NonNull Context context, @RawRes int rawRes) {
InputStream inputStream = context.getResources().openRawResource(rawRes);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

Expand All @@ -37,7 +37,7 @@ public static String readJsonAsStringFromDisk(@NonNull Context context, @RawRes
* @param task
* @return times in millis how much time requered to do work
*/
public static long doNTimes(int times, Runnable task) {
static long doNTimes(int times, Runnable task) {
long before = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
task.run();
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/res/layout/screen_initial.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
android:orientation="vertical">

<Button
android:id="@+id/small_json"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parse small json 100 times"
/>
android:text="Parse small json 1000 times" />

<Button
android:id="@+id/big_json"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Big json 50 times"
/>
android:text="Big json 50 times" />

</LinearLayout>
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.2'
}
}

allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Mon Mar 05 11:37:32 IST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip