-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Gruncan/v1.2a
V1.2.0
- Loading branch information
Showing
49 changed files
with
1,245 additions
and
77 deletions.
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
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
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 |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
package com.spotify.json; | ||
package com.json; | ||
|
||
|
||
class JSON { | ||
|
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
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
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
2 changes: 1 addition & 1 deletion
2
...ain/java/com/spotify/json/JSONString.java → src/main/java/com/json/JSONString.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
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
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
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 |
---|---|---|
@@ -1,13 +1,12 @@ | ||
package com.spotify; | ||
|
||
import com.spotify.requests.IRequest; | ||
import com.spotify.requests.RequestResponse; | ||
import com.spotify.requests.AbstractRequest; | ||
|
||
public interface SpotifyClient { | ||
|
||
|
||
// to be changed | ||
RequestResponse executeRequest(IRequest request); | ||
SpotifyResponse executeRequest(AbstractRequest request); | ||
|
||
|
||
} |
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
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,53 @@ | ||
package com.spotify; | ||
|
||
import com.json.JSONArray; | ||
import com.json.JSONObject; | ||
import com.spotify.objects.SpotifyObject; | ||
import com.spotify.objects.SpotifySerializer; | ||
import com.spotify.requests.RequestResponse; | ||
|
||
import java.lang.reflect.Array; | ||
|
||
public class SpotifyResponse extends SpotifySerializer { | ||
|
||
|
||
private final RequestResponse requestResponse; | ||
private final Class<? extends SpotifyObject> cls; | ||
private String index = null; | ||
|
||
|
||
public SpotifyResponse(RequestResponse response, Class<? extends SpotifyObject> cls) { | ||
this.requestResponse = response; | ||
this.cls = cls; | ||
} | ||
|
||
public SpotifyResponse(RequestResponse response, Class<? extends SpotifyObject> cls, String index) { | ||
this(response, cls); | ||
this.index = index; | ||
|
||
} | ||
|
||
public RequestResponse getRequestResponse() { | ||
return this.requestResponse; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <E extends SpotifyObject> E[] getSerialisedObjects() { | ||
if (this.index == null) return null; | ||
JSONArray jsonArray = this.requestResponse.ok().getJSONArray(this.index); | ||
int length = jsonArray.length(); | ||
E[] array = (E[]) Array.newInstance(this.cls, length); | ||
for (int i = 0; i < length; i++) { | ||
JSONObject json = jsonArray.getJSONObject(i); | ||
array[i] = (E) super.serializeObject(json, this.cls); | ||
} | ||
return array; | ||
} | ||
|
||
|
||
@SuppressWarnings("unchecked") | ||
public <E extends SpotifyObject> E getSerialisedObject() { | ||
return (E) super.serializeObject(this.requestResponse.ok(), cls); | ||
} | ||
|
||
} |
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,18 @@ | ||
package com.spotify.objects; | ||
|
||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SpotifyField { | ||
|
||
// Since default can't be null, setting it to " is illegal json, therefore won't ever occur | ||
String value() default "\""; | ||
|
||
String[] path() default {}; | ||
|
||
} |
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,9 @@ | ||
package com.spotify.objects; | ||
|
||
|
||
import java.io.Serializable; | ||
|
||
public interface SpotifyObject extends Serializable { | ||
|
||
|
||
} |
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,13 @@ | ||
package com.spotify.objects; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.FIELD, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SpotifyOptional { | ||
|
||
|
||
} |
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,13 @@ | ||
package com.spotify.objects; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SpotifySerialize { | ||
|
||
Class<? extends SpotifyObject> value(); | ||
|
||
boolean isArray() default false; | ||
|
||
} |
134 changes: 134 additions & 0 deletions
134
src/main/java/com/spotify/objects/SpotifySerializer.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,134 @@ | ||
package com.spotify.objects; | ||
|
||
import com.json.JSONArray; | ||
import com.json.JSONObject; | ||
import com.spotify.exceptions.SpotifySerializationException; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Array; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public abstract class SpotifySerializer { | ||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
private <E extends Serializable> E serializer(Class<E> cls, JSONObject json) { | ||
try { | ||
if (cls.isEnum()) { | ||
return (E) this.handleEnum((Class<? extends Enum>) cls, json.getString("value")); | ||
} | ||
Constructor<E> constructor = cls.getConstructor(); | ||
E e = constructor.newInstance(); | ||
SpotifyOptional notRequiredCls = cls.getAnnotation(SpotifyOptional.class); | ||
|
||
boolean clsRequired = notRequiredCls != null; | ||
|
||
|
||
for (Field field : cls.getDeclaredFields()) { | ||
SpotifyField spotifyField = field.getAnnotation(SpotifyField.class); | ||
// if not field not annotated ignore | ||
if (spotifyField == null) continue; | ||
|
||
SpotifyOptional spotifyNotRequired = field.getAnnotation(SpotifyOptional.class); | ||
|
||
boolean fieldRequired = spotifyNotRequired != null; | ||
|
||
|
||
boolean required = !(fieldRequired || clsRequired); | ||
|
||
Class<?> fieldRawType = field.getType(); | ||
Class<? extends Serializable> fieldType; | ||
|
||
|
||
if (fieldRawType.isPrimitive()) { | ||
if (fieldRawType.equals(int.class)) | ||
fieldRawType = Integer.class; | ||
else if (fieldRawType.equals(boolean.class)) | ||
fieldRawType = Boolean.class; | ||
else if (fieldRawType.equals(double.class)) | ||
fieldRawType = Double.class; | ||
else | ||
continue; | ||
} | ||
|
||
|
||
if (Serializable.class.isAssignableFrom(fieldRawType)) | ||
fieldType = (Class<? extends Serializable>) fieldRawType; | ||
else | ||
continue; | ||
|
||
String name = spotifyField.value(); | ||
if (name.equals("\"")) | ||
name = field.getName(); | ||
|
||
|
||
// Get jsonobject if deeper in hierarchy | ||
JSONObject jsonPath = json; | ||
for (String p : spotifyField.path()) | ||
jsonPath = jsonPath.getJSONObject(p); | ||
|
||
|
||
//if field is not present and is required throw error can't serialize | ||
if (!required && (jsonPath == null || jsonPath.isNull(name))) | ||
continue; | ||
else if (jsonPath == null || jsonPath.isNull(name)) | ||
throw new SpotifySerializationException(String.format("No mapping found for spotify required field: \"%s/%s\". " + | ||
"Java class %s field: %s", String.join("/", spotifyField.path()), spotifyField.value(), cls, field.getName())); | ||
|
||
|
||
field.setAccessible(true); | ||
|
||
if (fieldType.isArray()) { | ||
Class<?> componentRawType = fieldType.getComponentType(); | ||
Class<? extends Serializable> componentType = (Class<? extends Serializable>) componentRawType; | ||
|
||
|
||
JSONArray jsonArray = jsonPath.getJSONArray(name); | ||
field.set(e, this.createArray(componentType, jsonArray)); | ||
} else { | ||
field.set(e, this.serializeField(fieldType, jsonPath, name)); | ||
} | ||
field.setAccessible(false); | ||
} | ||
return e; | ||
|
||
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | | ||
InvocationTargetException | SpotifySerializationException a) { | ||
a.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
private <E extends Serializable> E serializeField(Class<E> componentType, JSONObject jsonPath, String name) { | ||
if (!SpotifyObject.class.isAssignableFrom(componentType)) | ||
return jsonPath.get(componentType, name); | ||
else { | ||
JSONObject jsonv = jsonPath.get(JSONObject.class, name); | ||
if (jsonv == null) { | ||
return this.serializer(componentType, jsonPath); | ||
} else { | ||
return this.serializer(componentType, jsonPath.get(JSONObject.class, name)); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <E extends Serializable> E[] createArray(Class<E> cls, JSONArray jsonArray) { | ||
E[] array = (E[]) Array.newInstance(cls, jsonArray.length()); | ||
for (int i = 0; i < array.length; i++) | ||
array[i] = this.serializeField(cls, jsonArray.getModifiedJSONObject(i), "value"); | ||
return array; | ||
} | ||
|
||
private <E extends Enum<E>> E handleEnum(Class<E> cls, String value) { | ||
return Enum.valueOf(cls, value); | ||
} | ||
|
||
protected <S extends SpotifyObject> S serializeObject(JSONObject json, Class<S> cls) { | ||
if (json == null) return null; | ||
return this.serializer(cls, json); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.