com.squareup.moshi.Moshi类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(147)

本文整理了Java中com.squareup.moshi.Moshi类的一些代码示例,展示了Moshi类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Moshi类的具体详情如下:
包路径:com.squareup.moshi.Moshi
类名称:Moshi

Moshi介绍

[英]Coordinates binding between JSON values and Java objects.
[中]协调JSON值和Java对象之间的绑定。

代码示例

代码示例来源:origin: square/okhttp

/** See https://api.slack.com/methods/rtm.start. */
public RtmStartResponse rtmStart(String accessToken) throws IOException {
 HttpUrl url = baseUrl.newBuilder("rtm.start")
   .addQueryParameter("token", accessToken)
   .build();
 Request request = new Request.Builder()
   .url(url)
   .build();
 Call call = httpClient.newCall(request);
 try (Response response = call.execute()) {
  JsonAdapter<RtmStartResponse> jsonAdapter = moshi.adapter(RtmStartResponse.class);
  return jsonAdapter.fromJson(response.body().source());
 }
}

代码示例来源:origin: square/moshi

@Override public void bind(Moshi moshi, JsonAdapter.Factory factory) {
 super.bind(moshi, factory);
 delegate = Types.equals(parameterTypes[0], returnType)
   && qualifierAnnotations.equals(returnTypeAnnotations)
   ? moshi.nextAdapter(factory, returnType, returnTypeAnnotations)
   : moshi.adapter(returnType, returnTypeAnnotations);
}

代码示例来源:origin: square/moshi

public void run() throws Exception {
 // For some reason our JSON has date and time as separate fields. We will clean that up during
 // parsing: Moshi will first parse the JSON directly to an EventJson and from that the
 // EventJsonAdapter will create the actual Event.
 String json = ""
   + "{\n"
   + "  \"title\": \"Blackjack tournament\",\n"
   + "  \"begin_date\": \"20151010\",\n"
   + "  \"begin_time\": \"17:04\"\n"
   + "}\n";
 Moshi moshi = new Moshi.Builder().add(new EventJsonAdapter()).build();
 JsonAdapter<Event> jsonAdapter = moshi.adapter(Event.class);
 Event event = jsonAdapter.fromJson(json);
 System.out.println(event);
 System.out.println(jsonAdapter.toJson(event));
}

代码示例来源:origin: square/moshi

@Override public void toJson(JsonWriter writer, Object value) throws IOException {
 Class<?> valueClass = value.getClass();
 if (valueClass == Object.class) {
  // Don't recurse infinitely when the runtime type is also Object.class.
  writer.beginObject();
  writer.endObject();
 } else {
  moshi.adapter(toJsonType(valueClass), Util.NO_ANNOTATIONS).toJson(writer, value);
 }
}

代码示例来源:origin: square/moshi

ObjectJsonAdapter(Moshi moshi) {
 this.moshi = moshi;
 this.listJsonAdapter = moshi.adapter(List.class);
 this.mapAdapter = moshi.adapter(Map.class);
 this.stringAdapter = moshi.adapter(String.class);
 this.doubleAdapter = moshi.adapter(Double.class);
 this.booleanAdapter = moshi.adapter(Boolean.class);
}

代码示例来源:origin: influxdata/influxdb-java

public static InfluxDBException buildExceptionForErrorState(final String errorBody) {
 try {
  Moshi moshi = new Moshi.Builder().build();
  JsonAdapter<ErrorMessage> adapter = moshi.adapter(ErrorMessage.class).lenient();
  ErrorMessage errorMessage = adapter.fromJson(errorBody);
  return InfluxDBException.buildExceptionFromErrorMessage(errorMessage.error);
 } catch (Exception e) {
  return new InfluxDBException(errorBody);
 }
}

代码示例来源:origin: square/retrofit

@Override public Converter<?, RequestBody> requestBodyConverter(Type type,
  Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
 JsonAdapter<?> adapter = moshi.adapter(type, jsonAnnotations(parameterAnnotations));
 if (lenient) {
  adapter = adapter.lenient();
 }
 if (failOnUnknown) {
  adapter = adapter.failOnUnknown();
 }
 if (serializeNulls) {
  adapter = adapter.serializeNulls();
 }
 return new MoshiRequestBodyConverter<>(adapter);
}

代码示例来源:origin: serj-lotutovici/moshi-lazy-adapters

@Override public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations,
   Moshi moshi) {
  Set<? extends Annotation> nextAnnotations =
    Types.nextAnnotations(annotations, SerializeNulls.class);
  if (nextAnnotations == null) return null;
  return moshi.adapter(type, nextAnnotations).serializeNulls();
 }
};

代码示例来源:origin: square/moshi

@Override public @Nullable JsonAdapter<?> create(
   Type requestedType, Set<? extends Annotation> annotations, Moshi moshi) {
  if (type != requestedType) return null;
  JsonAdapter<T> delegate = moshi.nextAdapter(this, type, annotations);
  return new DefaultOnDataMismatchAdapter<>(delegate, defaultValue);
 }
};

代码示例来源:origin: square/moshi

public void run() throws Exception {
 Moshi moshi = new Moshi.Builder()
   .add(new MultipleFormatsCardAdapter())
   .add(new CardStringAdapter())
   .build();
 JsonAdapter<Card> cardAdapter = moshi.adapter(Card.class);
 // Decode cards from one format or the other.
 System.out.println(cardAdapter.fromJson("\"5D\""));
 System.out.println(cardAdapter.fromJson("{\"suit\": \"SPADES\", \"rank\": 5}"));
 // Cards are always encoded as strings.
 System.out.println(cardAdapter.toJson(new Card('5', Suit.CLUBS)));
}

代码示例来源:origin: square/moshi

public void run() throws Exception {
 Moshi moshi = new Moshi.Builder()
   .add(new SortedSetAdapterFactory())
   .build();
 JsonAdapter<SortedSet<String>> jsonAdapter = moshi.adapter(
   Types.newParameterizedType(SortedSet.class, String.class));
 TreeSet<String> model = new TreeSet<>();
 model.add("a");
 model.add("b");
 model.add("c");
 String json = jsonAdapter.toJson(model);
 System.out.println(json);
}

代码示例来源:origin: square/moshi

MapJsonAdapter(Moshi moshi, Type keyType, Type valueType) {
 this.keyAdapter = moshi.adapter(keyType);
 this.valueAdapter = moshi.adapter(valueType);
}

代码示例来源:origin: lukas-krecan/JsonUnit

@Override
protected Node readValue(String source, String label, boolean lenient) {
  try {
    JsonAdapter<Object> adapter = moshi.adapter(Object.class);
    if (lenient) {
      adapter = adapter.lenient();
    }
    return newNode(adapter.fromJson(source));
  } catch (IOException e) {
    throw new IllegalArgumentException("Can not parse " + label + " value.", e);
  }
}

代码示例来源:origin: square/retrofit

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
  Retrofit retrofit) {
 JsonAdapter<?> adapter = moshi.adapter(type, jsonAnnotations(annotations));
 if (lenient) {
  adapter = adapter.lenient();
 }
 if (failOnUnknown) {
  adapter = adapter.failOnUnknown();
 }
 if (serializeNulls) {
  adapter = adapter.serializeNulls();
 }
 return new MoshiResponseBodyConverter<>(adapter);
}

代码示例来源:origin: square/moshi

@Override public @Nullable JsonAdapter<?> create(
   Type type, Set<? extends Annotation> annotations, Moshi moshi) {
  Set<? extends Annotation> delegateAnnotations =
    Types.nextAnnotations(annotations, Enveloped.class);
  if (delegateAnnotations == null) {
   return null;
  }
  Type envelope =
    Types.newParameterizedTypeWithOwner(EnvelopeJsonAdapter.class, Envelope.class, type);
  JsonAdapter<Envelope<?>> delegate = moshi.nextAdapter(this, envelope, delegateAnnotations);
  return new EnvelopeJsonAdapter(delegate);
 }
};

代码示例来源:origin: square/okhttp

/** See https://api.slack.com/methods/oauth.access. */
public OAuthSession exchangeCode(String code, HttpUrl redirectUrl) throws IOException {
 HttpUrl url = baseUrl.newBuilder("oauth.access")
   .addQueryParameter("client_id", clientId)
   .addQueryParameter("client_secret", clientSecret)
   .addQueryParameter("code", code)
   .addQueryParameter("redirect_uri", redirectUrl.toString())
   .build();
 Request request = new Request.Builder()
   .url(url)
   .build();
 Call call = httpClient.newCall(request);
 try (Response response = call.execute()) {
  JsonAdapter<OAuthSession> jsonAdapter = moshi.adapter(OAuthSession.class);
  return jsonAdapter.fromJson(response.body().source());
 }
}

代码示例来源:origin: square/moshi

public void run() throws Exception {
 Moshi moshi = new Moshi.Builder()
   .add(Date.class, new Rfc3339DateJsonAdapter())
   .build();
 JsonAdapter<Tournament> jsonAdapter = moshi.adapter(Tournament.class);
 // The RFC3339 JSON adapter can read dates with a timezone offset like '-05:00'.
 String lastTournament = ""
   + "{"
   + "  \"location\":\"Chainsaw\","
   + "  \"name\":\"21 for 21\","
   + "  \"start\":\"2015-09-01T20:00:00-05:00\""
   + "}";
 System.out.println("Last tournament: " + jsonAdapter.fromJson(lastTournament));
 // The RFC3339 JSON adapter always writes dates with UTC, using a 'Z' suffix.
 Tournament nextTournament = new Tournament(
   "Waterloo Classic", "Bauer Kitchen", newDate(2015, 10, 1, 20, -5));
 System.out.println("Next tournament JSON: " + jsonAdapter.toJson(nextTournament));
}

代码示例来源:origin: square/moshi

public void run() throws Exception {
 BlackjackHand blackjackHand = new BlackjackHand(
   new Card('6', SPADES),
   Arrays.asList(new Card('4', CLUBS), new Card('A', HEARTS)));
 Moshi moshi = new Moshi.Builder().build();
 JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);
 String json = jsonAdapter.toJson(blackjackHand);
 System.out.println(json);
}

代码示例来源:origin: square/moshi

@CheckReturnValue
public <T> JsonAdapter<T> adapter(Type type, Set<? extends Annotation> annotations) {
 return adapter(type, annotations, null);
}

代码示例来源:origin: net.javacrumbs.json-unit/json-unit-core

@Override
protected Node readValue(String source, String label, boolean lenient) {
  try {
    JsonAdapter<Object> adapter = moshi.adapter(Object.class);
    if (lenient) {
      adapter = adapter.lenient();
    }
    return newNode(adapter.fromJson(source));
  } catch (IOException e) {
    throw new IllegalArgumentException("Can not parse " + label + " value.", e);
  }
}

相关文章