Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #92 from cloudant/90-json-conversion
Browse files Browse the repository at this point in the history
90 json conversion
  • Loading branch information
ricellis authored Aug 11, 2017
2 parents a5b1591 + b9a2cd6 commit 1f700d0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
matrix:
include:
- os: linux
dist: precise
language: android
sudo: required
android:
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- [FIXED] Issue where nested JSON objects treated as strings not JSON.

# 0.4.0 (2017-04-07)

- [FIXED] Renamed attachment property `content_type` from `contentType` to match
Expand Down
31 changes: 12 additions & 19 deletions src/android/CloudantSyncPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -874,19 +875,13 @@ private DocumentStore getDocumentStore(String name) throws Exception {
* @return - The Map from the converted JSONObject
* @throws JSONException
*/
private Map<String, Object> getMapFromJSONObject(JSONObject obj) throws JSONException {
Map<String, Object> newMap = new HashMap<String, Object>();
private byte[] getDocumentBodyBytesFromJSONObject(JSONObject obj) throws JSONException {
// These special fields are handled separately from the rest of the body
obj.remove(DOC_ID);
obj.remove(DOC_REV);
obj.remove(DOC_DELETED);

Iterator<String> iter = obj.keys();

while (iter.hasNext()) {
String key = iter.next();
if (!key.equals(DOC_ID) && !key.equals(DOC_REV) && !key.equals(DOC_DELETED)) {
newMap.put(key, obj.get(key));
}
}

return newMap;
return obj.toString().getBytes(Charset.forName("UTF-8"));
}

/**
Expand Down Expand Up @@ -937,7 +932,7 @@ private DocumentRevision buildDocRevision(JSONObject docRevisionJSON) throws Exc
revision.setAttachments(attachmentMap);
}

revision.setBody(DocumentBodyFactory.create(getMapFromJSONObject(docRevisionJSON)));
revision.setBody(DocumentBodyFactory.create(getDocumentBodyBytesFromJSONObject(docRevisionJSON)));

return revision;
}
Expand All @@ -950,7 +945,10 @@ private DocumentRevision buildDocRevision(JSONObject docRevisionJSON) throws Exc
* @throws IOException
*/
private JSONObject buildJSON(DocumentRevision rev, boolean isCreate) throws JSONException, IOException {
JSONObject result = new JSONObject();
// Create the basic document body in the result object
JSONObject result = new JSONObject(rev.getBody().asMap());

// Add the ID and rev
result.put(DOC_ID, rev.getId());
result.put(DOC_REV, rev.getRevision());

Expand Down Expand Up @@ -981,11 +979,6 @@ private JSONObject buildJSON(DocumentRevision rev, boolean isCreate) throws JSON
result.put(DOC_ATTACHMENTS, attachments);
}
}
Map<String, Object> body = rev.getBody().asMap();

for (Map.Entry<String, Object> entry : body.entrySet()) {
result.put(entry.getKey(), entry.getValue());
}

return result;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/CRUDTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ exports.defineAutoTests = function() {
lastName: 'Kaplinger',
numberOne: 1,
numberFive: 5,
assignments: ['zero', 'one'],
cv: {education: 'Masters', skills: ['javascript', 'java']}
};

var objWithoutBody = {};
Expand Down Expand Up @@ -195,6 +197,45 @@ exports.defineAutoTests = function() {
}); //End-create
});

it('finds a document revision by docId (nested)', function(done) {
var datastore = getDatastore(datastoreDescription);
expect(datastore).not.toBe(null);

// Create
datastore.createDocumentFromRevision(employee, function(error, docRevision) {
expect(error).toBe(null);
expect(docRevision).not.toBe(null);
expect(docRevision._id).toBeDefined();
expect(docRevision._rev).toBeDefined();
expect(docRevision.firstName).toBe(employee.firstName);
expect(docRevision.assignments[0]).toBe(employee.assignments[0]);
expect(docRevision.assignments[1]).toBe(employee.assignments[1]);
expect(docRevision.cv.education).toBe(employee.cv.education);
expect(docRevision.cv.skills[0]).toBe(employee.cv.skills[0]);

// GetDocument
datastore.getDocument(docRevision._id, function(
error, fetchedRevision) {
expect(error).toBe(null);
expect(fetchedRevision).not.toBe(
null);
expect(fetchedRevision._id).toBeDefined();
expect(fetchedRevision._rev).toBeDefined();
expect(fetchedRevision.firstName)
.toBe(employee.firstName);
expect(fetchedRevision.assignments[0])
.toBe(employee.assignments[0]);
expect(fetchedRevision.assignments[1])
.toBe(employee.assignments[1]);
expect(fetchedRevision.cv.education)
.toBe(employee.cv.education);
expect(fetchedRevision.cv.skills[0])
.toBe(employee.cv.skills[0]);
done();
}); //End-getDocument
}); //End-create
});

it('deletes a document revision', function(done) {
var datastore = getDatastore(datastoreDescription);
expect(datastore).not.toBe(null);
Expand Down

0 comments on commit 1f700d0

Please sign in to comment.