Skip to content

Commit

Permalink
Implement UnknownValue class
Browse files Browse the repository at this point in the history
  • Loading branch information
cheungpat authored and rickmak committed Nov 23, 2016
1 parent 099fae9 commit bddd537
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void testRecordSerializationNormalFlow() throws Exception {
data.put("abc", 12.345);
data.put("publish_date", new DateTime(2016, 6, 15, 7, 55, 34, 342, DateTimeZone.UTC).toDate());
data.put("comment", new Reference(aComment));
data.put("money", new UnknownValue("money"));
data.put("loc", location);
data.put("attachment", new Asset(
"928739f5-e4f4-4c1c-9377-a0184dac66eb-hello.txt",
Expand Down Expand Up @@ -293,6 +294,11 @@ public void testRecordDeserializationNormalFlow() throws Exception {
commentReferenceObject.put("$id", "Comment/" + referenceRecordId);
jsonObject.put("comment", commentReferenceObject);

JSONObject unknownValueObject = new JSONObject();
unknownValueObject.put("$type", "unknown");
unknownValueObject.put("$underlying_type", "money");
jsonObject.put("money", unknownValueObject);

JSONObject locationObject = new JSONObject();
locationObject.put("$type", "geo");
locationObject.put("$lat", 22.3360901);
Expand Down Expand Up @@ -341,6 +347,10 @@ public void testRecordDeserializationNormalFlow() throws Exception {
assertEquals("Comment", commentRef.getType());
assertEquals(referenceRecordId, commentRef.getId());

// assert unknown value field
UnknownValue moneyValue = (UnknownValue) record.get("money");
assertEquals("money", moneyValue.getUnderlyingType());

// assert location field
Location loc = (Location) record.get("loc");
assertEquals(22.3360901, loc.getLatitude());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.skygear.skygear;

import android.support.test.runner.AndroidJUnit4;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;

@RunWith(AndroidJUnit4.class)
public class UnknownValueSerializerUnitTest {
@Test
public void testUnknownValueSerializationWithUnderlyingType() throws Exception {
UnknownValue value = new UnknownValue("money");
JSONObject jsonObject = UnknownValueSerializer.serialize(value);

assertEquals("unknown", jsonObject.getString("$type"));
assertEquals("money", jsonObject.getString("$underlying_type"));
}

@Test(expected=JSONException.class)
public void testUnknownValueSerializationWithNullUnderlyingType() throws Exception {
UnknownValue value = new UnknownValue(null);
JSONObject jsonObject = UnknownValueSerializer.serialize(value);

jsonObject.getString("$underlying_type");
}

@Test
public void testUnknownValueDeserializationWithUnderlyingType() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("$type", "unknown");
jsonObject.put("$underlying_type", "money");

UnknownValue ref = UnknownValueSerializer.deserialize(jsonObject);
assertEquals("money", ref.getUnderlyingType());
}

@Test
public void testUnknownValueDeserializationWithNoUnderlyingType() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("$type", "unknown");

UnknownValue ref = UnknownValueSerializer.deserialize(jsonObject);
assertNull(null, ref.getUnderlyingType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class RecordSerializer {
Date.class,
Asset.class,
Location.class,
Reference.class
Reference.class,
UnknownValue.class
));

/**
Expand Down Expand Up @@ -137,6 +138,8 @@ static JSONObject serialize(Record record) {
recordData.put(perKey, LocationSerializer.serialize((Location) perValue));
} else if (perValue instanceof Reference) {
recordData.put(perKey, ReferenceSerializer.serialize((Reference) perValue));
} else if (perValue instanceof UnknownValue) {
recordData.put(perKey, UnknownValueSerializer.serialize((UnknownValue) perValue));
}
}

Expand Down Expand Up @@ -257,6 +260,8 @@ static Record deserialize(JSONObject jsonObject) throws JSONException {
record.set(nextKey, LocationSerializer.deserialize((JSONObject) nextValue));
} else if (ReferenceSerializer.isReferenceFormat(nextValue)) {
record.set(nextKey, ReferenceSerializer.deserialize((JSONObject) nextValue));
} else if (UnknownValueSerializer.isUnknownValueFormat(nextValue)) {
record.set(nextKey, UnknownValueSerializer.deserialize((JSONObject) nextValue));
} else {
record.set(nextKey, nextValue);
}
Expand Down
58 changes: 58 additions & 0 deletions skygear/src/main/java/io/skygear/skygear/UnknownValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.skygear.skygear;

import org.json.JSONException;
import org.json.JSONObject;

/**
* UnknownValue indicates that the value is of unknown type to Skygear.
*
* This usually occurs when the database contains data that is not managed
* by Skygear.
*
* You should not instantiate an instance of this class.
*/
public class UnknownValue {
/**
* The name of the underlying data type.
*/
String underlyingType;

/**
* Instantiates a new unknown value.
*
* @param underlyingType the name of the underlying data type
*/
public UnknownValue(String underlyingType) {
super();
this.underlyingType = underlyingType;
}

/**
* Gets underlying data type.
*
* @return the underlying data type
*/
public String getUnderlyingType() {
return underlyingType;
}

/**
* Serialize the unknown value.
*
* @return the JSON object
*/
public JSONObject toJson() {
return UnknownValueSerializer.serialize(this);
}

/**
* Deserializes the unknown value.
*
* @param jsonObject the JSON object
* @return the unknown value
* @throws JSONException the json exception
*/
public static UnknownValue fromJson(JSONObject jsonObject) throws JSONException {
return UnknownValueSerializer.deserialize(jsonObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.skygear.skygear;

import org.json.JSONException;
import org.json.JSONObject;

/**
* The Skygear UnknownValue Data Type Object Serializer.
*/
public class UnknownValueSerializer {
/**
* Serialize a UnknownValue.
*
* @param unknownValue the unknown value
* @return the json object
*/
public static JSONObject serialize(UnknownValue unknownValue) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("$type", "unknown");
if (unknownValue.underlyingType != null) {
jsonObject.put("$underlying_type", unknownValue.underlyingType);
}

return jsonObject;
} catch (JSONException e) {
return null;
}
}

/**
* Deserialize a UnknownValue from JSON object.
*
* @param jsonObject the JSON object
* @return the UnknownValue
* @throws JSONException the JSON exception
*/
public static UnknownValue deserialize(JSONObject jsonObject) throws JSONException {
String typeValue = jsonObject.getString("$type");
if (typeValue.equals("unknown")) {
String underlyingType = null;
if (jsonObject.has("$underlying_type")) {
underlyingType = jsonObject.getString("$underlying_type");
}

UnknownValue unknownValue = new UnknownValue(underlyingType);
return unknownValue;
}

throw new JSONException("Invalid $type value: " + typeValue);
}

/**
* Determines whether an object is a JSON object in Skygear defined unknown
* value format.
*
* @param object the object
* @return the boolean
*/
public static boolean isUnknownValueFormat(Object object) {
try {
JSONObject jsonObject = (JSONObject) object;
return jsonObject.getString("$type").equals("unknown");
} catch (ClassCastException e) {
return false;
} catch (JSONException e) {
return false;
}
}
}

0 comments on commit bddd537

Please sign in to comment.