com.squareup.moshi.FromJson Java Examples

The following examples show how to use com.squareup.moshi.FromJson. 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: MoshiConverterFactoryTest.java    From jus with Apache License 2.0 6 votes vote down vote up
@FromJson
public AnInterface read(JsonReader jsonReader) throws IOException {
    jsonReader.beginObject();

    String name = null;
    while (jsonReader.hasNext()) {
        switch (jsonReader.nextName()) {
            case "name":
                name = jsonReader.nextString();
                break;
        }
    }

    jsonReader.endObject();
    return new AnImplementation(name);
}
 
Example #2
Source File: ComplexAdapterUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@FromJson
public ZonedDateTime fromJson(JsonDateTime input) {
    LocalDate date = LocalDate.parse(input.getDate());
    LocalTime time = LocalTime.parse(input.getTime());
    ZoneId timezone = ZoneId.of(input.getTimezone());
    return ZonedDateTime.of(date, time, timezone);
}
 
Example #3
Source File: CardAdapter.java    From moshi with Apache License 2.0 5 votes vote down vote up
@FromJson Card fromJson(String card) {
  if (card.length() != 2) throw new JsonDataException("Unknown card: " + card);

  char rank = card.charAt(0);
  switch (card.charAt(1)) {
    case 'C': return new Card(rank, Suit.CLUBS);
    case 'D': return new Card(rank, Suit.DIAMONDS);
    case 'H': return new Card(rank, Suit.HEARTS);
    case 'S': return new Card(rank, Suit.SPADES);
    default: throw new JsonDataException("unknown suit: " + card);
  }
}
 
Example #4
Source File: CustomAdapterWithDelegate.java    From moshi with Apache License 2.0 5 votes vote down vote up
@FromJson
@Nullable
Stage fromJson(JsonReader jsonReader, JsonAdapter<Stage> delegate) throws IOException {
  String value = jsonReader.nextString();

  Stage stage;
  if (value.startsWith("in-progress")) {
    stage = Stage.IN_PROGRESS;
  } else {
    stage = delegate.fromJsonValue(value);
  }
  return stage;
}
 
Example #5
Source File: AttributeType.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
@FromJson
AttributeType fromJson(String name) {
    AttributeType attrType = AttributeType.searchByName(name);
    if (null == attrType && name.equalsIgnoreCase("series"))
        attrType = SERIE; // Fix the issue with v1.6.5
    return attrType;
}
 
Example #6
Source File: MultipleFormats.java    From moshi with Apache License 2.0 5 votes vote down vote up
@FromJson @CardString Card fromJson(String card) {
  if (card.length() != 2) throw new JsonDataException("Unknown card: " + card);

  char rank = card.charAt(0);
  switch (card.charAt(1)) {
    case 'C': return new Card(rank, Suit.CLUBS);
    case 'D': return new Card(rank, Suit.DIAMONDS);
    case 'H': return new Card(rank, Suit.HEARTS);
    case 'S': return new Card(rank, Suit.SPADES);
    default: throw new JsonDataException("unknown suit: " + card);
  }
}
 
Example #7
Source File: MultipleFormats.java    From moshi with Apache License 2.0 5 votes vote down vote up
@FromJson Card fromJson(JsonReader reader, @CardString JsonAdapter<Card> stringAdapter,
    JsonAdapter<Card> defaultAdapter) throws IOException {
  if (reader.peek() == JsonReader.Token.STRING) {
    return stringAdapter.fromJson(reader);
  } else {
    return defaultAdapter.fromJson(reader);
  }
}
 
Example #8
Source File: AlternativeAdapterUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@FromJson
@EpochMillis
public Instant fromJson(Long input) {
    return Instant.ofEpochMilli(input);
}
 
Example #9
Source File: ColorAdapter.java    From NaviBee with GNU General Public License v3.0 4 votes vote down vote up
@FromJson
@HexColor int fromJson(String color) {
    return Color.parseColor(color);
}
 
Example #10
Source File: SimpleAdapterUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@FromJson
public Author fromJson(String author) {
    Matcher matcher = pattern.matcher(author);
    return matcher.find() ? new Author(matcher.group(1), matcher.group(2)) : null;
}
 
Example #11
Source File: InstantAdapter.java    From u2020 with Apache License 2.0 4 votes vote down vote up
@FromJson public Instant fromJson(String value) {
  return Instant.parse(value);
}
 
Example #12
Source File: InstantAdapter.java    From u2020-mvp with Apache License 2.0 4 votes vote down vote up
@FromJson public Instant fromJson(String value) {
  return Instant.parse(value);
}
 
Example #13
Source File: CustomQualifier.java    From moshi with Apache License 2.0 4 votes vote down vote up
@FromJson @HexColor int fromJson(String rgb) {
  return Integer.parseInt(rgb.substring(1), 16);
}
 
Example #14
Source File: FromJsonWithoutStrings.java    From moshi with Apache License 2.0 4 votes vote down vote up
@FromJson Event eventFromJson(EventJson eventJson) {
  Event event = new Event();
  event.title = eventJson.title;
  event.beginDateAndTime = eventJson.begin_date + " " + eventJson.begin_time;
  return event;
}
 
Example #15
Source File: BigDecimalAdapter.java    From rides-java-sdk with MIT License 4 votes vote down vote up
@FromJson
public BigDecimal bigDecimalFromString(@Nullable Float value) {
    return value != null ? new BigDecimal(value.toString()) : null;
}
 
Example #16
Source File: OAuthScopesAdapter.java    From rides-java-sdk with MIT License 4 votes vote down vote up
@FromJson
@OAuthScopes
Set<Scope> fromJson(String scopes) {
    return Scope.parseScopes(scopes);
}
 
Example #17
Source File: WrappedJsonAdapterTest.java    From moshi-lazy-adapters with Apache License 2.0 4 votes vote down vote up
@FromJson Throws fromJson(String str) throws IOException {
  throw new IOException("ThrowingAdapter.fromJson");
}
 
Example #18
Source File: TestUtil.java    From moshi-jsonapi with MIT License 4 votes vote down vote up
@FromJson @Color int fromJson(String rgb) {
    return Integer.parseInt(rgb.substring(1), 16);
}
 
Example #19
Source File: IclusionResponseAdapter.java    From hmftools with GNU General Public License v3.0 4 votes vote down vote up
@FromJson
@NotNull
List<IclusionObjectStudy> studies(@NotNull Map<String, IclusionObjectStudy> json) {
    return Lists.newArrayList(json.values());
}
 
Example #20
Source File: IclusionResponseAdapter.java    From hmftools with GNU General Public License v3.0 4 votes vote down vote up
@FromJson
@NotNull
List<IclusionObjectVariant> variants(@NotNull Map<String, IclusionObjectVariant> json) {
    return Lists.newArrayList(json.values());
}
 
Example #21
Source File: IclusionResponseAdapter.java    From hmftools with GNU General Public License v3.0 4 votes vote down vote up
@FromJson
@NotNull
List<IclusionObjectGene> genes(@NotNull Map<String, IclusionObjectGene> json) {
    return Lists.newArrayList(json.values());
}
 
Example #22
Source File: IclusionResponseAdapter.java    From hmftools with GNU General Public License v3.0 4 votes vote down vote up
@FromJson
@NotNull
List<IclusionObjectIndication> indications(@NotNull Map<String, IclusionObjectIndication> json) {
    return Lists.newArrayList(json.values());
}
 
Example #23
Source File: LocalDateTimeAdapter.java    From droidconat-2016 with Apache License 2.0 4 votes vote down vote up
@FromJson
public LocalDateTime fromText(String text) {
    return LocalDateTime.parse(text, formatter);
}
 
Example #24
Source File: DateAdapter.java    From secrets-proxy with Apache License 2.0 4 votes vote down vote up
@FromJson
LocalDateTime fromJson(String date) {
  return LocalDateTime.from(formatter.parse(date));
}