Back to project page uber-android.
The source code is released under:
MIT License
If you think the Android project uber-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * UberAPILib//from w w w. j ava 2 s . com * * This file was automatically generated by APIMATIC BETA v2.0 on 08/22/2014 */ package io.apimatic.uberapilib; import java.io.IOException; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class APIHelper { /*used for deserialization of json data*/ private static ObjectMapper mapper = new ObjectMapper(); /** * JSON Serialization of a given object. * @param obj The object to serialize into JSON * @return The serialized Json string representation of the given object */ public static String jsonSerialize(Object obj) throws JsonProcessingException { if(null == obj) return null; return mapper.writeValueAsString(obj); } /** * JSON Deserialization of the given json string. * @param json The json string to deserialize * @param <T> The type of the object to deserialize into * @return The deserialized object */ public static <T extends Object> T jsonDeserialize(String json, TypeReference<T> typeReference) throws IOException { if (isNullOrWhiteSpace(json)) return null; return mapper.readValue(json, typeReference); } /** * Replaces template parameters in the given url * @param queryBuilder The query string builder to replace the template parameters * @param parameters The parameters to replace in the url */ public static void appendUrlWithTemplateParameters(StringBuilder queryBuilder, Map<String, Object> parameters) { //perform parameter validation if (null == queryBuilder) throw new IllegalArgumentException("Given value for parameter \"queryBuilder\" is invalid." ); if (null == parameters) return; //iterate and append parameters for (Map.Entry<String, Object> pair : parameters.entrySet()) { String replaceValue = ""; //load parameter value if (null != pair.getValue()) replaceValue = pair.getValue().toString(); //find the template parameter and replace it with its value replaceAll(queryBuilder, "{" + pair.getKey() + "}", replaceValue); } } /** * Appends the given set of parameters to the given query string * @param queryBuilder The query url string to append the parameters * @param parameters The parameters to append * @return The modified query url string */ public static void appendUrlWithQueryParameters(StringBuilder queryBuilder, Map<String, Object> parameters) { //perform parameter validation if (null == queryBuilder) throw new IllegalArgumentException("Given value for parameter \"queryBuilder\" is invalid." ); if (null == parameters) return; //does the query string already has parameters boolean hasParams = (queryBuilder.indexOf("?") > 0); //iterate and append parameters for (Map.Entry<String, Object> pair : parameters.entrySet()) { //ignore null values if (null == pair.getValue()) continue; //if already has parameters, use the & to append new parameters char separator = (hasParams) ? '&' : '?'; queryBuilder.append(separator + pair.getKey() + "=" + pair.getValue()); //indicate that now the query has some params hasParams = true; } } /** * Validates if the string is null, empty or whitespace * @param s The string to validate * @return The result of validation */ public static boolean isNullOrWhiteSpace(String s) { if(s == null) return true; int length = s.length(); if (length > 0) { for (int start = 0, middle = length / 2, end = length - 1; start <= middle; start++, end--) { if (s.charAt(start) > ' ' || s.charAt(end) > ' ') { return false; } } return true; } return false; } /** * Replaces all occurrences of the given string in the string builder * @param stringBuilder The string builder to update with replaced strings * @param toReplace The string to replace in the string builder * @param replaceWith The string to replace with*/ public static void replaceAll(StringBuilder stringBuilder, String toReplace, String replaceWith) { int index = stringBuilder.indexOf(toReplace); while (index != -1) { stringBuilder.replace(index, index + toReplace.length(), replaceWith); index += replaceWith.length(); // Move to the end of the replacement index = stringBuilder.indexOf(toReplace, index); } } /** * Validates and processes the given Url * @param url The given Url to process * @return Pre-process Url as string */ public static String cleanUrl(StringBuilder url) { //ensure that the urls are absolute Pattern pattern = Pattern.compile("^(https?://[^/]+)"); Matcher matcher = pattern.matcher(url); if (!matcher.find()) throw new IllegalArgumentException("Invalid Url format."); //get the http protocol match String protocol = matcher.group(1); //remove redundant forward slashes String query = url.substring(protocol.length()); query = query.replaceAll("//+", "/"); //return process url return protocol.concat(query); } }