-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Json Parameter Converter using Gson BNN-366 #resolve (#329)
- Loading branch information
1 parent
c7d1497
commit 3ab3adf
Showing
3 changed files
with
55 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
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
51 changes: 51 additions & 0 deletions
51
bennu-spring/src/main/java/org/fenixedu/bennu/spring/converters/JsonElementConverter.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,51 @@ | ||
/** | ||
* Copyright © 2018 Instituto Superior Técnico | ||
* | ||
* This file is part of Bennu Spring. | ||
* | ||
* Bennu Spring is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Bennu Spring is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Bennu Spring. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.fenixedu.bennu.spring.converters; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import com.google.gson.JsonElement; | ||
|
||
import com.google.gson.JsonParser; | ||
import org.springframework.core.convert.TypeDescriptor; | ||
import org.springframework.core.convert.converter.ConditionalGenericConverter; | ||
|
||
/** | ||
* Converts a String to a JsonElement. | ||
* | ||
* @author Tiago Pinho ([email protected]) | ||
*/ | ||
public class JsonElementConverter implements ConditionalGenericConverter { | ||
|
||
@Override | ||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { | ||
return String.class.equals(sourceType.getType()) && JsonElement.class.equals(targetType.getType()); | ||
} | ||
|
||
@Override | ||
public Set<ConvertiblePair> getConvertibleTypes(){ | ||
return Collections.singleton(new ConvertiblePair(String.class, JsonElement.class)); | ||
} | ||
|
||
@Override | ||
public JsonElement convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { | ||
return new JsonParser().parse((String) source); | ||
} | ||
} |