Read Large JSON Files Using Gson

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

JSON exists everywhere and used mainly in REST APIs, There are many ways to read JSON files but here I will cover the two ways that are supported by Gson library.

  • Object Model

Load the whole file in memory and translate each JavaScript object to a Java object containing all the properties, array objects, etc. It is similar to the DOM way to read XML files.

  • Stream Model

Operate on a JSON document as a sequence of tokens that are traversed in depth-first order. Because stream mode reads one token at a time it requires less memory. It is similar to Pull Parsers way to read XML files.

When to use each model depends on your needs, If your JSON file is small then using object model to load the whole file into memory at once will be best choice, But if your JSON file it too large then using stream model will be the best.

I was having a large JSON file (≈ 30 MB) that I wanted to read. I couldn’t read the whole file at once using object model because loading all this data into memory made the system crash, So here I will describe how to use GSON library to read this file using stream model.

Firstly, Let’s take a look how the JSON file formed

cities_json

City and Coord model classes

city_model

coord_model

Now we will read the JSON file from assets folder using stream model

read_stream_model

With this approach we are loading objects one by one, and each time we convert the object that has been read to City model object.


References

What Is Android Intent ?

I was reading about Android Intents and I found this explanation very simple.

​An Intent is an “intention” to perform an action; in other words,

a messaging object you can use to request an action from another app component

An Intent is basically a message to say you did or want something to happen. Depending on the intent, apps or the OS might be listening for it and will react accordingly. Think of it as a blast email to a bunch of friends, in which you tell your friend John to do something, or to friends who can do X (“intent filters”), to do X. The other folks will ignore the email, but John (or friends who can do X) will react to it.

To listen for an broadcast intent (like the phone ringing, or an SMS is received), you implement a broadcast receiver, which will be passed the intent. To declare that you can handle another’s app intent like “take picture”, you declare an intent filter in your app’s manifest file.

If you want to fire off an intent to do something, like pop up the dialer, you fire off an intent saying you will.


References

Android Developer Path

Udacity has launched an Android Developer Path Guide. If you are newbie or have some experience with Android, take a look to that map to know the skills that you lack.

There are 3 levels in Udacity for Android developers:

  1. Beginner
    1. Android Basics: User Interface
    2. User Input
    3. Multi-Screen Apps
    4. Networking
    5. Data Storage
  2. Intermediate
    1. Developing Android Apps
  3. Advanced
    1. Advanced Android App Development

References

android-developer-path

Share Android App Internal Storage Data

Two weeks ago I faced a problem, I was storing a pdf file in internal storage and I wanted to share it via another apps like Gmail, or any other sharing app. When I tried to attach it to Gmail for example I got this error “Permission denied for the attachment“.
The problem is that data saved in internal storage isn’t accessible by other applications.

After a while I figured it out, that the solution is in Content Providers.

Content Providers have a lot of uses one of them is providing a way to share data with other apps.

In manifest we declare the content provider :

manifest-provider

name attribute is my custom content provider class, authorities attribute is URI that identify data offered by the content provider, exported is true means that the content provider is available for other applications to use.

After that we create our CustomContentProvider :

custom-content-provider

Finally, We will use our custom content provider to share our pdf file :

sharing

CONTENT_URI means I am going to use content provider “content://” + provider’s_authority + “/” + absolute file path.

That will allow our app to share the pdf file.

Chapter 4

In this chapter we talk about :-

This chapter is all about making sure that your software works in a real-world context.
Analysis: is figuring out potential problems, and then solving those problems before you release your application
out into real world.

In this chapter he talk about some notes in Class Diagram let’s see it … 🙂

Notes:-
1) Delegation helps our applications stay loosely coupled.That means that your objects are independent
of each other.


2) The nouns in a use case are usually the classes you need to write.
3) The verbs in your use case are usually the methods of the objects.


4) Think about how the classes you do have can support the behavior your use case descripe.
5) Pay attention to the nouns in your use case, even when they aren’t classes in your system.
6) Write your use cases in a way that makes sense to you, your boss, and your customers.


7) Association: means that one class is associated with another class by refrence,extension,inheritance,…etc
8) There are some important key words:-

Finally the source code for Modified Dog Door System that he talk about it in this chapter you can find it Here. I implement it in C#.

Chapter 3

In this chapter we talk about :-

In the real world requirements are always changing, and it’s up to you to roll with these changes and keep your customer satisfied.

When your customer has new need, it’s up to you to change your application to meet those new needs because

The one constant in software analysis and design is

Requirements always change. If you have got good use cases, though you can usually change your software quickly to adjust to those new requirements.

Notes:-
1- The Main Path should be what you want to have happen most of the time.
2- The Alternate Path is one or more steps that a use case has that are optional, or provide
alternate ways to work through the use case.
3- A complete path through a use case from the first step to the last, is called a scenario.

4- Any time you change your use case, you need to go back and check your requirements.
5- Duplicate code is a bad idea.
6- The last note is

Now we will add some new tools that we learn it in this chapter:-

And the OO Principles that we learn:-

Finally the source code for Modified Dog Door System that he talk about it in this chapter you can find it Here. I implement it in C#.