Browse Source

Removes use of internal gson class (#452)

Turns out an api was added a very long time ago to do what we do in gson
without use of internal apis.

See #449
pull/459/head release-9.3.1
Adrian Cole 8 years ago committed by GitHub
parent
commit
1e1bdb2e5d
  1. 5
      gson/README.md
  2. 17
      gson/src/main/java/feign/gson/DoubleToIntMapTypeAdapter.java

5
gson/README.md

@ -15,7 +15,6 @@ GitHub github = Feign.builder()
### Map<String, Object> and Numbers ### Map<String, Object> and Numbers
The default constructors of `GsonEncoder` and `GsonDecoder` decoder numbers in The default constructors of `GsonEncoder` and `GsonDecoder` decoder numbers in
`Map<String, Object>` as Integer type. This prevents reading `{"counter", "1"}` `Map<String, Object>` as Integer type. This prevents reading `{"counter", "1"}`
as `Map.of("counter", 1.0)`. This uses an internal class in gson. as `Map.of("counter", 1.0)`.
If you want the default behavior, or cannot use gson internal classes (ex in To change this, please use constructors that accept a Gson object.
OSGi), please use the constructors that accept a Gson object.

17
gson/src/main/java/feign/gson/DoubleToIntMapTypeAdapter.java

@ -16,31 +16,20 @@
package feign.gson; package feign.gson;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.InstanceCreator;
import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapter;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.bind.MapTypeAdapterFactory;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map; import java.util.Map;
/** /**
* Deals with scenario where Gson Object type treats all numbers as doubles. * Deals with scenario where Gson Object type treats all numbers as doubles.
*/ */
public class DoubleToIntMapTypeAdapter extends TypeAdapter<Map<String, Object>> { public class DoubleToIntMapTypeAdapter extends TypeAdapter<Map<String, Object>> {
private final TypeAdapter<Map<String, Object>> delegate =
final static TypeToken<Map<String, Object>> token = new TypeToken<Map<String, Object>>() { new Gson().getAdapter(new TypeToken<Map<String, Object>>() {
}; });
private final TypeAdapter<Map<String, Object>>
delegate =
new MapTypeAdapterFactory(new ConstructorConstructor(
Collections.<Type, InstanceCreator<?>>emptyMap()), false).create(new Gson(), token);
@Override @Override
public void write(JsonWriter out, Map<String, Object> value) throws IOException { public void write(JsonWriter out, Map<String, Object> value) throws IOException {

Loading…
Cancel
Save