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

JSON Import API #489

Merged
merged 40 commits into from
Dec 19, 2014
Merged

JSON Import API #489

merged 40 commits into from
Dec 19, 2014

Conversation

cmelchior
Copy link
Contributor

API methods :

Realm.createObjectFromJson(Class realmObject, JSONObject json)
Realm.createAllFromJson(Class realmObject, JSONArray json)

Realm.createObjectFromJson(Class realmObject, String json)
Realm.createAllFromJson(Class realmObject, String json)

// Uses JsonReader for stream parsing. Only for API 11+
Realm.createObjectFromJson(Class realmObject, InputStream json);
Realm.createAllFromJson(Class realmObject, InputStream json);

A few comments:

  • Import using InputStream only works for API 11+. Methods are annotated as such.
  • JSON property names need to match java variabel names.
  • Import API does not work in standard Java. JSON is only supported from Java 7, and with a different API.
  • If import fails, everything up to that point is put in the database (if Realm.commitTransaction() is called). We need a Realm.rollback() to change this behavior.
  • functions like createOrUpdateFromJson() would be natural extensions but awaits primary key support.

@@ -3,7 +3,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
classpath 'com.android.tools.build:gradle:0.13.+'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good change but this should be another PR

@cmelchior
Copy link
Contributor Author

Updated with fixes for comments + added file with notes about API.

@cmelchior
Copy link
Contributor Author

There is a unit test for it testing all data types with a null value, it defaults to the default value for that datatype. I have updated the JavaDoc for the Json methods so this is clarified.

}

dependencies {
compile 'io.realm:realm-android:0.75.1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update to 0.76.0

protected void setUp() throws Exception {
Realm.deleteRealmFile(getContext());
testRealm = Realm.getInstance(getContext());
testRealm.beginTransaction();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to delete objects when the file has been deleted.

}

public void testImportStream_DateAsLong() throws IOException {
InputStream in = loadJsonFromAssets("date_as_long.json");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 5 lines are repeated through out the next many tests. Make a local method for that.

emanuelez added a commit that referenced this pull request Dec 19, 2014
@emanuelez emanuelez merged commit f58c607 into realm:master Dec 19, 2014
@Hazer Hazer mentioned this pull request Dec 23, 2014
2 tasks
@Zhuinden Zhuinden mentioned this pull request Oct 12, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.