-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
skygear/src/androidTest/java/io/skygear/skygear/UnknownValueSerializerUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
skygear/src/main/java/io/skygear/skygear/UnknownValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
skygear/src/main/java/io/skygear/skygear/UnknownValueSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|