Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.jens.myapplication.util; //ww w . j a va2s.c o m import com.example.jens.myapplication.apimanager.JSONParsingTask; import com.example.jens.myapplication.apimanager.PostRequestTask; import com.example.jens.myapplication.apimanager.SimpleRequestTask; import com.example.jens.myapplication.apimanager.manager.LoginManager; import com.example.jens.myapplication.domain.User; import com.example.jens.myapplication.sam.JoetzApplication; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; import java.io.IOException; import java.util.Iterator; import java.util.LinkedList; /** * Created by Sam on 21/11/2014. */ public class JsonUtils { /** * returns a filterprovider with a single filter * @param filterName * @param fields * @return */ public static FilterProvider createSimpleFilter(String filterName, String... fields){ return new SimpleFilterProvider().addFilter(filterName, SimpleBeanPropertyFilter.filterOutAllExcept(fields)); } /** * use this to add multiple filters * @param filters * @return */ public static FilterProvider createSimpleFilter(Filters filters){ SimpleFilterProvider filter = new SimpleFilterProvider(); filters.attachFilters(filter); return filter; } /** * A class that keeps a list of filters for json object filtering for serialization */ public static class Filters{ private LinkedList<String> namesList; private LinkedList<String[]> fieldsList; public Filters(){ namesList = new LinkedList<String>(); fieldsList = new LinkedList<String[]>(); } /** * Add a filter to the filters * @param filterName The name of the filter (corresponds with the JsonFilter(value) value) * @param fields The fields that should be included * @return */ public Filters addFilter(String filterName, String... fields){ this.namesList.add(filterName); this.fieldsList.add(fields); return this; } /** * Attach the filters to the filterprovider * @param filterProvider the provider to which the filters should be added */ public void attachFilters(SimpleFilterProvider filterProvider){ Iterator<String> itNames = namesList.iterator(); Iterator<String[]> itFields = fieldsList.iterator(); while(itNames.hasNext()){ String filterName = itNames.next(); String[] fields = itFields.next(); filterProvider.addFilter(filterName, SimpleBeanPropertyFilter.filterOutAllExcept(fields)); } } } /** * * @return a task that maps the json string to a user object */ public static JSONParsingTask<User> createSimpleUserTask(){ return new JSONParsingTask<User>() { @Override public User handleJSON(String json) throws IOException { return new ObjectMapper().readValue(json, User.class); } }; } /** * Creates a task that will set the new user as the currently logged in user * @param afterTask The task that should be called after this object's task is completed * (can be null) * @return the task */ public static PostRequestTask<User> createSimpleRefreshUserTask(final SimpleRequestTask afterTask){ return new PostRequestTask<User>() { @Override public void doTask(User user, int statusCode) { LoginManager manager = JoetzApplication.getContext().getLoginManager(); if(!manager.isLoggedIn()) { throw new IllegalArgumentException("User has to be logged in to refresh"); } manager.setUser(user); if(afterTask != null){ afterTask.doTask(statusCode); } } }; } /** * Creates a simple task that only executes the afterTask if it is not null * @param afterTask The task to be executed * @return the task */ public static PostRequestTask createSimpleDoAfterTask(final SimpleRequestTask afterTask){ return new PostRequestTask() { @Override public void doTask(Object obj, int statusCode) { if(afterTask != null){ afterTask.doTask(statusCode); } } }; } }