From b5b36930bf7ad55fbdc509d3855b37a9921cea07 Mon Sep 17 00:00:00 2001 From: Rohan Pawar Date: Mon, 5 Mar 2018 12:50:01 +0530 Subject: [PATCH] Updated parsing libraries Updated SDK Version --- app/build.gradle | 15 +++--- .../jsonparserscomparation/GcTracker.java | 6 +-- .../InitialActivity.java | 9 ++-- .../InitializationAndParseActivity.java | 2 +- .../ParseNTimesBigJsonActivity.java | 7 ++- .../ParseNTimesSmallJson.java | 51 +++++++++++-------- .../jsonparserscomparation/Utils.java | 6 +-- app/src/main/res/layout/screen_initial.xml | 9 ++-- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 10 files changed, 62 insertions(+), 51 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a22ac41..cc886ba 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" @@ -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' } diff --git a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/GcTracker.java b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/GcTracker.java index 03a36b3..92b65d6 100644 --- a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/GcTracker.java +++ b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/GcTracker.java @@ -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; } @@ -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; diff --git a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/InitialActivity.java b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/InitialActivity.java index 9e9603e..6f04f4e 100644 --- a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/InitialActivity.java +++ b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/InitialActivity.java @@ -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)); } } diff --git a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/InitializationAndParseActivity.java b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/InitializationAndParseActivity.java index e3b7527..f4bbbd5 100644 --- a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/InitializationAndParseActivity.java +++ b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/InitializationAndParseActivity.java @@ -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) diff --git a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/ParseNTimesBigJsonActivity.java b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/ParseNTimesBigJsonActivity.java index c8078f7..5500132 100644 --- a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/ParseNTimesBigJsonActivity.java +++ b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/ParseNTimesBigJsonActivity.java @@ -30,7 +30,7 @@ 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>() { }.getType(); @@ -38,7 +38,6 @@ public class ParseNTimesBigJsonActivity extends AppCompatActivity { private String jsonString; private Gson gson; private ObjectMapper mapper; - private Moshi moshi; private JsonAdapter> moshiAdapter; private GcTracker gcTracker; @@ -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(); diff --git a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/ParseNTimesSmallJson.java b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/ParseNTimesSmallJson.java index fe385f7..0be12cc 100644 --- a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/ParseNTimesSmallJson.java +++ b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/ParseNTimesSmallJson.java @@ -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>() { }.getType(); - private String jsonString; - private Gson gson; - private ObjectMapper mapper; - private Moshi moshi; + private String jsonString; + private Gson gson; + private ObjectMapper mapper; private JsonAdapter> 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 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 humanList = Arrays.asList(mapper.readValue(jsonString, SmallObject[].class)); } catch (IOException e) { @@ -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 humans = moshiAdapter.fromJson(jsonString); } catch (IOException e) { @@ -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 humans = Ason.deserializeList(jsonString, Human.class); } })); @@ -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(); } diff --git a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/Utils.java b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/Utils.java index 0667335..acbcd84 100644 --- a/app/src/main/java/com/ilyaeremin/jsonparserscomparation/Utils.java +++ b/app/src/main/java/com/ilyaeremin/jsonparserscomparation/Utils.java @@ -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(); @@ -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(); diff --git a/app/src/main/res/layout/screen_initial.xml b/app/src/main/res/layout/screen_initial.xml index d31bd9c..ec27d92 100644 --- a/app/src/main/res/layout/screen_initial.xml +++ b/app/src/main/res/layout/screen_initial.xml @@ -1,23 +1,20 @@ + android:orientation="vertical">