-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* replace jackson json parsing with gson * validate required fields exists when parsing json
- Loading branch information
1 parent
0a233c2
commit 6b6fa1f
Showing
19 changed files
with
291 additions
and
146 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
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
37 changes: 37 additions & 0 deletions
37
auth0/src/main/java/com/auth0/authentication/result/JsonRequired.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,37 @@ | ||
/* | ||
* JsonRequired.java | ||
* | ||
* Copyright (c) 2016 Auth0 (http://auth0.com) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.auth0.authentication.result; | ||
|
||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface JsonRequired { | ||
} |
47 changes: 47 additions & 0 deletions
47
auth0/src/main/java/com/auth0/authentication/result/JsonRequiredTypeAdapterFactory.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,47 @@ | ||
package com.auth0.authentication.result; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
|
||
|
||
public class JsonRequiredTypeAdapterFactory implements TypeAdapterFactory { | ||
|
||
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) { | ||
|
||
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); | ||
|
||
return new TypeAdapter<T>() { | ||
|
||
public void write(JsonWriter out, T value) throws IOException { | ||
delegate.write(out, value); | ||
} | ||
|
||
public T read(JsonReader in) throws IOException { | ||
T pojo = delegate.read(in); | ||
|
||
Field[] fields = pojo.getClass().getDeclaredFields(); | ||
for (Field f : fields) { | ||
if (f.getAnnotation(JsonRequired.class) != null) { | ||
try { | ||
f.setAccessible(true); | ||
if (f.get(pojo) == null) { | ||
throw new JsonParseException("Missing field in JSON: " + f.getName()); | ||
} | ||
} catch (IllegalAccessException ex) { | ||
throw new JsonParseException("Missing field in JSON: " + f.getName(), ex); | ||
} | ||
} | ||
} | ||
return pojo; | ||
} | ||
}.nullSafe(); | ||
} | ||
} |
Oops, something went wrong.