androidx.room.TypeConverter Java Examples

The following examples show how to use androidx.room.TypeConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResponseDeltasTypeConverter.java    From ground-android with Apache License 2.0 6 votes vote down vote up
@TypeConverter
@Nullable
public static String toString(@NonNull ImmutableList<ResponseDelta> responseDeltas) {
  JSONObject json = new JSONObject();
  for (ResponseDelta delta : responseDeltas) {
    try {
      json.put(
          delta.getFieldId(),
          delta
              .getNewResponse()
              .map(ResponseJsonConverter::toJsonObject)
              .orElse(JSONObject.NULL));
    } catch (JSONException e) {
      Timber.e(e, "Error building JSON");
    }
  }
  return json.toString();
}
 
Example #2
Source File: TypeTransmogrifier.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@TypeConverter
public static String fromStringSet(Set<String> strings) {
  if (strings==null) {
    return(null);
  }

  StringWriter result=new StringWriter();
  JsonWriter json=new JsonWriter(result);

  try {
    json.beginArray();

    for (String s : strings) {
      json.value(s);
    }

    json.endArray();
    json.close();
  }
  catch (IOException e) {
    Log.e(TAG, "Exception creating JSON", e);
  }

  return(result.toString());
}
 
Example #3
Source File: TypeTransmogrifier.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@TypeConverter
public static Set<String> toStringSet(String strings) {
  if (strings==null) {
    return(null);
  }

  StringReader reader=new StringReader(strings);
  JsonReader json=new JsonReader(reader);
  HashSet<String> result=new HashSet<>();

  try {
    json.beginArray();

    while (json.hasNext()) {
      result.add(json.nextString());
    }

    json.endArray();
  }
  catch (IOException e) {
    Log.e(TAG, "Exception parsing JSON", e);
  }

  return(result);
}
 
Example #4
Source File: DB.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
@TypeConverter
public static Address[] decodeAddresses(String json) {
    if (json == null)
        return null;

    List<Address> result = new ArrayList<>();
    try {
        JSONArray jroot = new JSONArray(json);
        for (int i = 0; i < jroot.length(); i++) {
            Object item = jroot.get(i);
            if (jroot.get(i) instanceof JSONArray)
                for (int j = 0; j < ((JSONArray) item).length(); j++)
                    result.add(InternetAddressJson.from((JSONObject) ((JSONArray) item).get(j)));
            else
                result.add(InternetAddressJson.from((JSONObject) item));
        }
    } catch (Throwable ex) {
        // Compose can store invalid addresses
        Log.w(ex);
    }
    return result.toArray(new Address[0]);
}
 
Example #5
Source File: RoomTypeConverters.java    From Gander with Apache License 2.0 6 votes vote down vote up
@TypeConverter
public static String fromHeaderListToString(List<HttpHeader> value) {
    if (value == null || value.size() == 0) {
        return null;
    }
    StringBuilder stringBuilder = new StringBuilder();
    boolean isFirst = true;
    for (HttpHeader header : value) {
        if (!isFirst) {
            stringBuilder.append(LIST_SEPARATOR);
        }
        stringBuilder
                .append(header.getName())
                .append(NAME_VALUE_SEPARATOR)
                .append(header.getValue());
        isFirst = false;
    }
    return stringBuilder.toString();
}
 
Example #6
Source File: RoomTypeConverters.java    From Gander with Apache License 2.0 6 votes vote down vote up
@TypeConverter
public static List<HttpHeader> fromStringToHeaderList(String value) {
    if (value == null || TextUtil.isNullOrWhiteSpace(value)) {
        return new ArrayList<>();
    }

    String[] nameValuePairArray = value.split(LIST_SEPARATOR);
    List<HttpHeader> list = new ArrayList<>(nameValuePairArray.length);

    for (String nameValuePair : nameValuePairArray) {
        String[] nameValue = nameValuePair.split(NAME_VALUE_SEPARATOR);
        if (nameValue.length == 2) {
            list.add(new HttpHeader(nameValue[0], nameValue[1]));
        } else if (nameValue.length == 1) {
            list.add(new HttpHeader(nameValue[0], ""));
        }
    }
    return list;
}
 
Example #7
Source File: ResponseMapTypeConverter.java    From ground-android with Apache License 2.0 6 votes vote down vote up
@TypeConverter
@NonNull
public static ResponseMap fromString(@Nullable String jsonString) {
  ResponseMap.Builder map = ResponseMap.builder();
  if (jsonString == null) {
    return map.build();
  }
  try {
    JSONObject jsonObject = new JSONObject(jsonString);
    Iterator<String> keys = jsonObject.keys();
    while (keys.hasNext()) {
      String fieldId = keys.next();
      ResponseJsonConverter.toResponse(jsonObject.get(fieldId))
          .ifPresent(response -> map.putResponse(fieldId, response));
    }
  } catch (JSONException e) {
    Timber.e(e, "Error parsing JSON string");
  }
  return map.build();
}
 
Example #8
Source File: ResponseMapTypeConverter.java    From ground-android with Apache License 2.0 6 votes vote down vote up
@TypeConverter
@Nullable
public static String toString(@NonNull ResponseMap responseDeltas) {
  JSONObject json = new JSONObject();
  for (String fieldId : responseDeltas.fieldIds()) {
    try {
      json.put(
          fieldId,
          responseDeltas
              .getResponse(fieldId)
              .map(ResponseJsonConverter::toJsonObject)
              .orElse(null));
    } catch (JSONException e) {
      Timber.e(e, "Error building JSON");
    }
  }
  return json.toString();
}
 
Example #9
Source File: ResponseDeltasTypeConverter.java    From ground-android with Apache License 2.0 6 votes vote down vote up
@TypeConverter
@NonNull
public static ImmutableList<ResponseDelta> fromString(@Nullable String jsonString) {
  ImmutableList.Builder<ResponseDelta> deltas = ImmutableList.builder();
  if (jsonString == null) {
    return deltas.build();
  }
  try {
    JSONObject jsonObject = new JSONObject(jsonString);
    Iterator<String> keys = jsonObject.keys();
    while (keys.hasNext()) {
      String fieldId = keys.next();
      deltas.add(
          ResponseDelta.builder()
              .setFieldId(fieldId)
              .setNewResponse(ResponseJsonConverter.toResponse(jsonObject.get(fieldId)))
              .build());
    }
  } catch (JSONException e) {
    Timber.e(e, "Error parsing JSON string");
  }
  return deltas.build();
}
 
Example #10
Source File: Converters.java    From PopularMovies with MIT License 5 votes vote down vote up
@TypeConverter
public static List<Genre> toGenresList(String genres) {
    if (genres == null) {
        return Collections.emptyList();
    }

    Type listType = new TypeToken<List<Genre>>() {}.getType();

    return gson.fromJson(genres, listType);
}
 
Example #11
Source File: TypeTransmogrifier.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@TypeConverter
public static String fromLocation(Location location) {
  if (location==null) {
    return(null);
  }

  return(String.format(Locale.US, "%f,%f", location.getLatitude(),
    location.getLongitude()));
}
 
Example #12
Source File: PrivacyProfileConverter.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@TypeConverter
public static final String storeToDatabase(PrivacyProfile privacyProfile) {
    if (privacyProfile == null) return "";
    try {
        return GsonUtils.INSTANCE.toJson(privacyProfile);
    } catch (Throwable tr) {
        tr.printStackTrace();
    }
    return "";
}
 
Example #13
Source File: PrivacyProfileConverter.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@TypeConverter
public static final PrivacyProfile restoreToOrigin(String json) {
    if (json == null || json.isEmpty()) return new PrivacyProfile();
    try {
        return GsonUtils.INSTANCE.fromJson(json, PrivacyProfile.class);
    } catch (Throwable tr) {
        tr.printStackTrace();
    }
    return new PrivacyProfile();
}
 
Example #14
Source File: PagesConverter.java    From QuranyApp with Apache License 2.0 5 votes vote down vote up
@TypeConverter
public String fromPagesToString(ArraySet<Integer> pages){
    if (pages == null){
        return null;
    }
    // create json
    Gson gson = new Gson();
    Type type = new TypeToken<ArraySet<Integer>>(){}.getType();
    return gson.toJson(pages,type);
}
 
Example #15
Source File: TypeTransmogrifier.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@TypeConverter
public static Location toLocation(String latlon) {
  if (latlon==null) {
    return(null);
  }

  String[] pieces=latlon.split(",");
  Location result=new Location("");

  result.setLatitude(Double.parseDouble(pieces[0]));
  result.setLongitude(Double.parseDouble(pieces[1]));

  return(result);
}
 
Example #16
Source File: TypeTransmogrifier.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@TypeConverter
public static Date toDate(Long millisSinceEpoch) {
  if (millisSinceEpoch==null) {
    return(null);
  }

  return(new Date(millisSinceEpoch));
}
 
Example #17
Source File: PagesConverter.java    From QuranyApp with Apache License 2.0 5 votes vote down vote up
@TypeConverter
public ArraySet<Integer> toPagesList(String pages){
    if (pages == null){
        return null ;
    }
    Gson gson = new Gson();
    Type type = new TypeToken<ArraySet<Integer>>(){}.getType();
    return gson.fromJson(pages,type);

}
 
Example #18
Source File: DB.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@TypeConverter
public static String[] toStringArray(String value) {
    if (value == null)
        return new String[0];
    else {
        String[] result = TextUtils.split(value, " ");
        for (int i = 0; i < result.length; i++)
            result[i] = Uri.decode(result[i]);
        return result;
    }
}
 
Example #19
Source File: DB.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@TypeConverter
public static String fromStringArray(String[] value) {
    if (value == null || value.length == 0)
        return null;
    else {
        String[] copy = new String[value.length];
        System.arraycopy(value, 0, copy, 0, value.length);
        for (int i = 0; i < copy.length; i++)
            copy[i] = Uri.encode(copy[i]);
        return TextUtils.join(" ", copy);
    }
}
 
Example #20
Source File: Converters.java    From memorize with MIT License 5 votes vote down vote up
@TypeConverter
public static List<String> fromTimestamp(String value) {
    Type listType = new TypeToken<List<String>>() {
    }.getType();
    return new Gson().fromJson(value, listType);
    // return value == null ? null : new Date(value);
}
 
Example #21
Source File: Converters.java    From memorize with MIT License 5 votes vote down vote up
@TypeConverter
public static String arraylistToString(List<String> list) {
    Gson gson = new Gson();
    String json = gson.toJson(list);

    return json;
    // return date == null ? null : date.getTime();
}
 
Example #22
Source File: TypeTransmogrifier.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@TypeConverter
public static Long fromDate(Date date) {
  if (date==null) {
    return(null);
  }

  return(date.getTime());
}
 
Example #23
Source File: MeshTypeConverters.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TypeConverter
public Map<Integer, Element> fromJsonToElements(final String elementsJson) {
    final Type elements = new TypeToken<Map<Integer, Element>>() {
    }.getType();
    return new GsonBuilder().
            excludeFieldsWithoutExposeAnnotation().
            registerTypeAdapter(Element.class, new ElementDbMigrator()).
            registerTypeAdapter(MeshModel.class, new InternalMeshModelDeserializer()).
            create().fromJson(elementsJson, elements);
}
 
Example #24
Source File: StyleTypeConverter.java    From ground-android with Apache License 2.0 4 votes vote down vote up
@TypeConverter
@Nullable
public static Style fromString(@Nullable String color) {
  return color == null ? null : Style.builder().setColor(color).build();
}
 
Example #25
Source File: MoodleAssignmentCourse.java    From ETSMobile-Android2 with Apache License 2.0 4 votes vote down vote up
@TypeConverter
public static List<MoodleAssignment> stringToAssignmentCourses(String json) {
    Gson gson = new Gson();
    Type type = new TypeToken<List<MoodleAssignment>>() {}.getType();
    return gson.fromJson(json, type);
}
 
Example #26
Source File: Converters.java    From call_manage with MIT License 4 votes vote down vote up
@TypeConverter
public static List<String> stringToList(String str) {
    String[] arr = str.split(";");
    return Arrays.asList(arr);
}
 
Example #27
Source File: MoodleAssignmentSubmission.java    From ETSMobile-Android2 with Apache License 2.0 4 votes vote down vote up
@TypeConverter
public static MoodleAssignmentLastAttempt stringToLastAttempt(String json) {
    Gson gson = new Gson();
    Type type = new TypeToken<MoodleAssignmentLastAttempt>() {}.getType();
    return gson.fromJson(json, type);
}
 
Example #28
Source File: Converters.java    From Beedio with GNU General Public License v2.0 4 votes vote down vote up
@TypeConverter
public static String fromList(List<String> list) {
    return gson.toJson(list);
}
 
Example #29
Source File: MoodleAssignmentSubmission.java    From ETSMobile-Android2 with Apache License 2.0 4 votes vote down vote up
@TypeConverter
public static MoodleAssignmentFeedback stringToFeedback(String json) {
    Gson gson = new Gson();
    Type type = new TypeToken<MoodleAssignmentFeedback>() {}.getType();
    return gson.fromJson(json, type);
}
 
Example #30
Source File: Converters.java    From Beedio with GNU General Public License v2.0 4 votes vote down vote up
@TypeConverter
public static List<String> fromString(String value) {
    Type listType = new TypeToken<List<String>>() {
    }.getType();
    return gson.fromJson(value, listType);
}