Skip to content

Commit

Permalink
Add Json Parameter Converter using Gson BNN-366 #resolve (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoppinho authored and sergiofbsilva committed Oct 31, 2018
1 parent c7d1497 commit 3ab3adf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import org.fenixedu.bennu.core.util.CoreConfiguration;
import org.fenixedu.bennu.core.util.CoreConfiguration.ConfigurationProperties;
import org.fenixedu.bennu.spring.converters.DomainObjectConverter;
import org.fenixedu.bennu.spring.converters.JsonElementConverter;
import org.fenixedu.bennu.spring.converters.LocalizedStringConverter;
import org.fenixedu.bennu.spring.converters.UserFromUsernameConverter;
import org.fenixedu.bennu.spring.portal.PortalHandlerInterceptor;
Expand Down Expand Up @@ -172,6 +174,7 @@ public ConversionService conversionService(GenericConversionService service) {
service.addConverter(new DomainObjectConverter());
service.addConverter(new LocalizedStringConverter());
service.addConverter(new UserFromUsernameConverter());
service.addConverter(new JsonElementConverter());
return service;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* 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;
package org.fenixedu.bennu.spring.converters;

import java.util.Collections;
import java.util.Set;
Expand Down
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);
}
}

0 comments on commit 3ab3adf

Please sign in to comment.