On Android, should / can I use one plain Gson object instead of calling new Gson () everywhere?

I am wondering if I should contain one oneton instance of a Gson object to parse json in my application.

Are there any problems with this approach? Is this premature optimization?

+4
source share
1 answer

I think both options are fine, but if you create new instances Gsontoo often, this is not very good due to

  • your application consumes more memory
  • Creating a new facility is not a cheap operation
  • you could potentially have compatibility issues when creating instances Gsonwith custom parameters in different places

, Gson , Gson Singleton DI ( ). , Gson , API/API, . -.

, , , , Gson . . , .

, Json.

:

public class Foo {

    private final Date date;

    public Foo(Date date) {
        this.date = date;
    }
}

Foo:

public class FooSerializer implements JsonSerializer<Foo> {

    private static SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");

    @Override
    public JsonElement serialize(Foo src, Type typeOfSrc, JsonSerializationContext context) {
        final JsonObject object = new JsonObject();
        object.addProperty("date", format.format(src.date));
        return object;
    }
}

, SimpleDateFormat Data String.

: SimpleDateFormat , typeAdapters, Gson, .

, Gson

new GsonBuilder()
    .registerTypeAdapter(Foo.class, new FooSerializer())
    .create();

, , , : {"date":"Thu, 0015 Oct 2015 21:32:0040 +0600"}

:

public class SimpleDateFormatThreadSafe extends SimpleDateFormat {

    private static final long serialVersionUID = 5448371898056188202L;
    ThreadLocal<SimpleDateFormat> localSimpleDateFormat;

    public SimpleDateFormatThreadSafe() {
        super();
        localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
            @Override
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat();
            }
        };
    }

    public SimpleDateFormatThreadSafe(final String pattern) {
        super(pattern);
        localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
            @Override
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat(pattern);
            }
        };
    }

    public SimpleDateFormatThreadSafe(final String pattern, final DateFormatSymbols formatSymbols) {
        super(pattern, formatSymbols);
        localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
            @Override
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat(pattern, formatSymbols);
            }
        };
    }

    public SimpleDateFormatThreadSafe(final String pattern, final Locale locale) {
        super(pattern, locale);
        localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
            @Override
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat(pattern, locale);
            }
        };
    }

    @Override
    public Object parseObject(String source) throws ParseException {
        return localSimpleDateFormat.get().parseObject(source);
    }

    @Override
    public String toString() {
        return localSimpleDateFormat.get().toString();
    }

    @Override
    public Date parse(String source) throws ParseException {
        return localSimpleDateFormat.get().parse(source);
    }

    // ... here are other overridden methods ...

    @Override
    public Object clone() {
        return localSimpleDateFormat.get().clone();
    }

    @Override
    public int hashCode() {
        return localSimpleDateFormat.get().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return localSimpleDateFormat.get().equals(obj);
    }

}
+7

Source: https://habr.com/ru/post/1609954/


All Articles