@Override public org.springframework.security.core.authority.SimpleGrantedAuthority deserialize( // final JsonParser jsonParser, // final DeserializationContext deserializationContext // ) throws IOException { final org.springframework.security.core.authority.SimpleGrantedAuthority grantedAuthority; final JsonToken currentToken = jsonParser.getCurrentToken(); if (JsonToken.VALUE_STRING.equals(currentToken)) { try { final String grantedAuthorityString = jsonParser.getText().trim(); grantedAuthority = new org.springframework.security.core.authority.SimpleGrantedAuthority( // grantedAuthorityString); return grantedAuthority; } catch (final Exception translated) { throw new Jackson2Utils.RuntimeJsonProcessingException("couldn't parse GrantedAuthority", translated); } } else { throw new IllegalStateException(); } } }
public static <T> Function<String, T> fromJson( // final ObjectMapper objectMapper, // final TypeReference<T> typeReference // ) { return string -> { try { return objectMapper.readValue(string, typeReference); } catch (final IOException wrapped) { throw new RuntimeJsonProcessingException("error read from JSON.", wrapped); } }; }
/** * to JSON. * * @param objectMapper objectMapper * @param <T> type * @return JSON */ public static <T> Function<T, String> toJson(final ObjectMapper objectMapper) { return object -> { try { if (object != null) { return objectMapper.writeValueAsString(object); } else { return ""; } } catch (final JsonProcessingException wrapped) { throw new RuntimeJsonProcessingException("error serialize to JSON.", wrapped); } }; }
/** * parse JSON. * * @param objectMapper objectMapper * @param type type * @param <T> type * @return object */ public static <T> Function<String, T> fromJson( // final ObjectMapper objectMapper, // final Class<T> type // ) { return string -> { try { return objectMapper.readValue(string, type); } catch (final IOException wrapped) { throw new RuntimeJsonProcessingException("error read from JSON.", wrapped); } }; }