List of usage examples for com.google.gson GsonBuilder GsonBuilder
public GsonBuilder()
From source file:ca.ualberta.cs.drivr.UserManager.java
License:Apache License
private static void loadRequests() { try {//from ww w .j av a2s. co m if (instance == null) instance = new UserManager(); FileInputStream fis = App.getContext().openFileInput(SAVE_FILENAME_REQUESTS); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); Gson gson = new GsonBuilder().registerTypeAdapter(Uri.class, new UriSerializer()).create(); instance.requestsList = gson.fromJson(in, RequestsList.class); if (instance == null) throw new FileNotFoundException(); } catch (FileNotFoundException e) { instance.requestsList = new RequestsList(); } }
From source file:ca.ualberta.cs.drivr.UserManager.java
License:Apache License
private static void saveUser() { try {//from ww w. j av a 2 s .co m FileOutputStream fos = App.getContext().openFileOutput(SAVE_FILENAME_USER, 0); OutputStreamWriter writer = new OutputStreamWriter(fos); Gson gson = new GsonBuilder().registerTypeAdapter(Uri.class, new UriSerializer()).create(); gson.toJson(instance.user, writer); writer.flush(); } catch (IOException e) { // Do nothing } }
From source file:ca.ualberta.cs.drivr.UserManager.java
License:Apache License
private static void saveRequests() { try {//from w w w . j a v a 2s . co m FileOutputStream fos = App.getContext().openFileOutput(SAVE_FILENAME_REQUESTS, 0); OutputStreamWriter writer = new OutputStreamWriter(fos); Gson gson = new GsonBuilder().registerTypeAdapter(Uri.class, new UriSerializer()).create(); gson.toJson(instance.requestsList, writer); writer.flush(); } catch (IOException e) { // Do nothing } }
From source file:ca.ualberta.cs.scandaloutraveltracker.mappers.UserMapper.java
License:Apache License
/** * Saves the user data, one field at a time. The field it saves * is associated with the key./*w w w .j av a 2 s . c o m*/ * @param userId * @param key * @param data */ public void saveUserData(int userId, String key, Object data) { SharedPreferences userFile = this.context.getSharedPreferences(getUserFileName(userId), 0); Editor editor = userFile.edit(); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer()); gsonBuilder.registerTypeAdapter(Location.class, new LocationSerializer()); Gson gson = gsonBuilder.create(); if (key.equals("id")) { editor.putInt("id", (Integer) data); } else if (key.equals("name")) { editor.putString(key, (String) data); } else if (key.equals("location")) { String locationsJson = gson.toJson((Location) data); editor.putString(key, locationsJson); } editor.commit(); }
From source file:ca.ualberta.cs.scandaloutraveltracker.mappers.UserMapper.java
License:Apache License
/** * Loads user data one field at a time. The field loaded is associated * with the key./*from w w w .j a va2 s . c o m*/ * @param userId * @param key * @return User associated with userId */ public Object loadUserData(int userId, String key) { Object data = 0; SharedPreferences userFile = this.context.getSharedPreferences(getUserFileName(userId), 0); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer()); gsonBuilder.registerTypeAdapter(Location.class, new LocationSerializer()); Gson gson = gsonBuilder.create(); if (key.equals("id")) { data = userFile.getInt(key, -1); } else if (key.equals("name")) { data = userFile.getString(key, ""); } else if (key.equals("location")) { String locationsJson = userFile.getString(key, ""); Type type = new TypeToken<Location>() { }.getType(); data = gson.fromJson(locationsJson, type); } return data; }
From source file:ca.ualberta.cs.unter.util.RequestUtil.java
License:Apache License
public static String serializer(Request request) { Gson gson = new GsonBuilder().registerTypeAdapter(GeoPoint.class, new GeoPointConverter()).create(); return gson.toJson(request); }
From source file:ca.ualberta.cs.unter.util.RequestUtil.java
License:Apache License
public static Request deserializer(String string) { Gson gson = new GsonBuilder().registerTypeAdapter(GeoPoint.class, new GeoPointConverter()).create(); return gson.fromJson(string, NormalRequest.class); }
From source file:ca.ualberta.cs.unter.util.RequestUtil.java
License:Apache License
public static Gson customGsonBuilder() { return new GsonBuilder().registerTypeAdapter(GeoPoint.class, new GeoPointConverter()).create(); }
From source file:ca.udes.android_projectweather.network.ForecastClient.java
License:Apache License
/** * Configure the gson.// w ww. j a va2 s.com * * @return builder.create() */ private static Gson createGson() { final long MILLIS = 1000; GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong() * MILLIS); } }); return builder.create(); }
From source file:ca.uhn.fhir.parser.json.GsonStructure.java
License:Apache License
@Override public void load(Reader theReader, boolean allowArray) throws DataFormatException { PushbackReader pbr = new PushbackReader(theReader); int nextInt;/* www . ja va2 s . c o m*/ try { while (true) { nextInt = pbr.read(); if (nextInt == -1) { throw new DataFormatException("Did not find any content to parse"); } if (nextInt == '{') { pbr.unread(nextInt); break; } if (Character.isWhitespace(nextInt)) { continue; } if (allowArray) { if (nextInt == '[') { pbr.unread(nextInt); break; } throw new DataFormatException( "Content does not appear to be FHIR JSON, first non-whitespace character was: '" + (char) nextInt + "' (must be '{' or '[')"); } throw new DataFormatException( "Content does not appear to be FHIR JSON, first non-whitespace character was: '" + (char) nextInt + "' (must be '{')"); } Gson gson = new GsonBuilder().disableHtmlEscaping().create(); if (nextInt == '{') { JsonObject root = gson.fromJson(pbr, JsonObject.class); setNativeObject(root); } else if (nextInt == '[') { JsonArray root = gson.fromJson(pbr, JsonArray.class); setNativeArray(root); } } catch (JsonSyntaxException e) { if (e.getMessage().startsWith("Unexpected char 39")) { throw new DataFormatException("Failed to parse JSON encoded FHIR content: " + e.getMessage() + " - This may indicate that single quotes are being used as JSON escapes where double quotes are required", e); } throw new DataFormatException("Failed to parse JSON encoded FHIR content: " + e.getMessage(), e); } catch (Exception e) { throw new DataFormatException("Failed to parse JSON content, error was: " + e.getMessage(), e); } }