• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mizosoft / methanol / #585

22 Aug 2025 05:42PM UTC coverage: 89.013% (-0.9%) from 89.944%
#585

push

github

web-flow
Properly setup coveralls (#132)

* Move buildSrc to gradle/src

* Setup coveralls with another Gradle plugin

 - Previously "com.github.kt3k.coveralls" was used which was removed in #130 due to issues with Gradle 9.0.0 (https://github.com/kt3k/coveralls-gradle-plugin/issues/119)

* Add CI job for coverallsJacoco

* Setup covered & test-contributing projects properly

2324 of 2796 branches covered (83.12%)

Branch coverage included in aggregate %.

7633 of 8390 relevant lines covered (90.98%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/methanol-gson/src/main/java/com/github/mizosoft/methanol/adapter/gson/GsonAdapter.java
1
/*
2
 * Copyright (c) 2025 Moataz Hussein
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to deal
6
 * in the Software without restriction, including without limitation the rights
7
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 * copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all
12
 * copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
 * SOFTWARE.
21
 */
22

23
package com.github.mizosoft.methanol.adapter.gson;
24

25
import static java.util.Objects.requireNonNull;
26

27
import com.github.mizosoft.methanol.MediaType;
28
import com.github.mizosoft.methanol.MoreBodySubscribers;
29
import com.github.mizosoft.methanol.TypeRef;
30
import com.github.mizosoft.methanol.adapter.AbstractBodyAdapter;
31
import com.google.gson.Gson;
32
import com.google.gson.reflect.TypeToken;
33
import java.net.http.HttpRequest.BodyPublisher;
34
import java.net.http.HttpRequest.BodyPublishers;
35
import java.net.http.HttpResponse.BodySubscriber;
36
import java.net.http.HttpResponse.BodySubscribers;
37
import java.util.function.Supplier;
38

39
abstract class GsonAdapter extends AbstractBodyAdapter {
40
  final Gson gson;
41

42
  GsonAdapter(Gson gson) {
43
    super(MediaType.APPLICATION_JSON);
1✔
44
    this.gson = requireNonNull(gson);
1✔
45
  }
1✔
46

47
  @Override
48
  public boolean supportsType(TypeRef<?> typeRef) {
49
    try {
50
      @SuppressWarnings("unused")
51
      var unused = gson.getAdapter(TypeToken.get(typeRef.type()));
1✔
52
      return true;
1✔
53
    } catch (IllegalArgumentException ignored) {
×
54
      // Gson::getAdapter throws IAE if it can't de/serialize the type.
55
      return false;
×
56
    }
57
  }
58

59
  static final class Encoder extends GsonAdapter implements BaseEncoder {
60
    Encoder(Gson gson) {
61
      super(gson);
1✔
62
    }
1✔
63

64
    @Override
65
    public <T> BodyPublisher toBody(T value, TypeRef<T> typeRef, Hints hints) {
66
      requireSupport(typeRef, hints);
1✔
67
      return attachMediaType(
1✔
68
          BodyPublishers.ofString(
1✔
69
              gson.toJson(value, typeRef.type()), hints.mediaTypeOrAny().charsetOrUtf8()),
1✔
70
          hints.mediaTypeOrAny());
1✔
71
    }
72
  }
73

74
  static final class Decoder extends GsonAdapter implements BaseDecoder {
75
    Decoder(Gson gson) {
76
      super(gson);
1✔
77
    }
1✔
78

79
    @Override
80
    public <T> BodySubscriber<T> toObject(TypeRef<T> typeRef, Hints hints) {
81
      requireSupport(typeRef, hints);
1✔
82
      return BodySubscribers.mapping(
1✔
83
          BodySubscribers.ofString(hints.mediaTypeOrAny().charsetOrUtf8()),
1✔
84
          json -> gson.fromJson(json, typeRef.type()));
1✔
85
    }
86

87
    @Override
88
    public <T> BodySubscriber<Supplier<T>> toDeferredObject(TypeRef<T> typeRef, Hints hints) {
89
      requireSupport(typeRef, hints);
1✔
90
      return BodySubscribers.mapping(
1✔
91
          MoreBodySubscribers.ofReader(hints.mediaTypeOrAny().charsetOrUtf8()),
1✔
92
          reader -> () -> gson.fromJson(reader, typeRef.type()));
1✔
93
    }
94
  }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc