I am making an Android application which reads several JSON files and inserts the read information into tables in my database. For instance, there are three files:

  1. countries.json

    {
        "countries": [
            {
                "name": "United States",
                "org_ids": [
                    {
                        "id": 1
                    },
                    {
                        "id": 2
                    },
                    {
                        "id": 3
                    }
                ]
            },
            {
                "name": "China",
                "org_ids": [
                    {
                        "id": 1
                    },
                    {
                        "id": 2
                    }
                ]
            },
            {
                "name": "Hong Kong",
                "org_ids": [
                    {
                        "id": 3
                    },
                    {
                        "id": 4
                    },
                    {
                        "id": 5
                    }
                ]
            }
        ]
    }
    
  2. orgs.json

    {
        "orgs": [
            {
                "id": 1,
                "name": "UNO"
            },
            {
                "id": 2,
                "name": "Organization of American States"
            },
            {
                "id": 3,
                "name": "INTERPOL"
            },
            {
                "id": 4,
                "name": "European Union"
            },
            {
                "id": 5,
                "name": "The Commonwealth of Nations"
            }
        ]
    }
    
  3. continents.json

    {
        "continents": [
            {
                "id": 1,
                "name": "Africa"
            },
            {
                "id": 2,
                "name": "North America"
            },
            {
                "id": 3,
                "name": "South America"
            },
            {
                "id": 4,
                "name": "Asia"
            },
            {
                "id": 5,
                "name": "Europe"
            },
            {
                "id": 6,
                "name": "Australia"
            }
        ]
    }
    

Please don't pay attention to the contents of my JSONs, they don't contain real members of the listed international organizations. It's just an example.

I see two ways.

THE FORMER WAY.

Store each country with its data to a class instance and define a SparseArray contains a country ID as a key and a CountryInfo object as a value.

/** Each item contains country's ID (key) and respective data (value) */
    private SparseArray<CountryInfo> mCountriesInfo = new SparseArray<JsonParser.CountryInfo>();

After reading everything from the files, the data from the SparseArray will be inserted into the database.

    private class CountryInfo {

    String mCountryName;
    int continentId;
    String mContinentName;
    /** Contains IDs (keys) and names (values) of organizations */
    SparseArray<String> mOrgNames = new SparseArray<String>();

}

Drawbacks.

  • The more JSONS (or the more complex JSONs) the more complex the class for storing.
  • Data duplication.

Benefits

  • Reading and inserting does not depend on each other.

THE LATTER WAY.

Read and insert without preliminary saving.

Drawbacks.

  • Database logic and reading logic are mixed.

Benefits.

  • No any additional classes.
  • No data duplication.

Which way is a better practice?

  • Can you elaborate why you copy the file contents to a database and how often this happens? – Bart van Ingen Schenau May 14 '13 at 9:24
  • @BartvanIngenSchenau, I copy the file contents to the database because I will use ListView to display the data and it's a very bad practice to read from JSON periodically in getView. As regards to my question, I chose the latter way because it is simpler. – Maksim Dmitriev May 14 '13 at 11:24
  • If you perform the copy multiple times (e.g. each time the application starts), then it is just as bad a practice (misuse of a database) and you might be better off to let the ListView serve the items from memory. OTOH, if it is just a one-time data conversion, the technique as outlined by DocBrown is best. – Bart van Ingen Schenau May 14 '13 at 14:09
up vote 3 down vote accepted

(This is a Java question, I guess?)

I am sure you can write a JSON reader which takes an arbitrary processing function and applies that function to every record. The "processing function" may be passed in a functional way, or if you have only older Java versions available, in form of an interface. This gives you reading logic without any database logic. Then, you provide a specific processing function / interface implementation containing the "database insert" logic.

As a result, you get separated database and reading logic without the drawbacks from your first approach.

This is also known as Visitor pattern.

  • thank you. I'm learning the visitor pattern now. But I've got a question. They use CarElement car = new Car(); on the wiki page. Is there any difference between CarElement car = new Car(); and Car car = new Car();? – Maksim Dmitriev May 14 '13 at 12:38
  • @RedPlanet: In the context of the main function in that specific example? No, I don't think this will make a difference there. – Doc Brown May 14 '13 at 13:31
  • ,thank you for your answer. I'll vote up after getting 15 or more reputation. If you have a little time, could you look through this? It's another question about the visitor design pattern. – Maksim Dmitriev May 15 '13 at 14:17

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.