Giter VIP home page Giter VIP logo

Comments (17)

yogurtearl avatar yogurtearl commented on July 20, 2024 8

I prefer @gson over @nogson

from auto-value-gson.

gabrielittner avatar gabrielittner commented on July 20, 2024 1

I've just found another reason for this. The gson and moshi extensions don't support generics in AutoValue classes and generate code that won't compile for those. So without opt-in/out you either can't use generics or can't use the gson/moshi extension.

from auto-value-gson.

rharter avatar rharter commented on July 20, 2024 1

So I've been thinking about how to do this cleanly, and I think it aligns with something I've been wanting to add for a while.

The approach would basically be to require the annotated class to include a public static method returning TypeAdapter. If that exists, then we generate the TypeAdapter, otherwise we don't.

This means to include the class in the gson extension you'd need something like this:

@AutoValue public abstract class Foo {
  public abstract String a();
  public abstract String b();

  public static TypeAdapter typeAdapter() {
    return new AutoValue_Foo.TypeAdapter();
  }
}

This will also open up the opportunity to generate a single TypeAdapterFactory that will return the appropriate TypeAdapter for any auto-value-gson generated class.

I'm curious to hear any feedback on this idea.

from auto-value-gson.

lenguyenthanh avatar lenguyenthanh commented on July 20, 2024

@rharter do you have any idea for this feature? is it good? I'm happy to make a PR. But wanna discuss with you first. Cheers!

from auto-value-gson.

rharter avatar rharter commented on July 20, 2024

I do think this is a good idea, I'm just trying to come up with a good way to handle it. We need to play nice with other extensions, so I'd love it if there was a way to include/exclude classes without the need for another extension, but I'm not sure there is.

Are you suggesting an explicit @Gson annotation for inclusion?

@AutoValue @Gson public abstract class Foo {

}

from auto-value-gson.

lenguyenthanh avatar lenguyenthanh commented on July 20, 2024

Yes, it is exactly what I propose. I'd like to add a runtime Module for @Gson annotation only. On main extension library, it should start generate code only if a class has @Gson annotation.

from auto-value-gson.

gabrielittner avatar gabrielittner commented on July 20, 2024

I think there are two completely different usage scenarios. You either want it to run on all and exclude few or the opposite. Adding @Gson to all your classes can get very annoying and using the static TypeAdapterFactory method as opt-in signal won't work with auto-registration.

What do you think of having two separate artifacts? One works like the current one and has a @NoGson annotation to opt-out. The other one requires explicit opt-in by adding the static TypeAdapterFactory method or adding @Gson.

from auto-value-gson.

rharter avatar rharter commented on July 20, 2024

First of all, there's no need for a runtime module, as even if there was a @Gson annotation, it would only need source retention and not be needed at runtime.

That being said, @gabrielittner's points are exactly my concerns. In your case, you want to exclude most classed, but explicitly include certain ones. Most of my cases are the opposite, in which most of my classes, benefit from having TypeAdapters. The challenge is that making things easier for one case makes them harder for the other.

As for the idea of having multiple artifacts, that sounds like a management headache and would be confusing to users, so I'm not a big fan of that idea.

from auto-value-gson.

lenguyenthanh avatar lenguyenthanh commented on July 20, 2024

I think two atifacts for two scenarios is quite strange. And it's hard to maintain two different codebases.

I still prefer @gson annotation option because it makes our code clearer, we have to explicit what classes are support Gson. Option 2, with @nogson anntation, adds magic to our code si nce it automatically adds suppoting Gson. And we can forget to add @gson easily when we adding new no gson classes.

from auto-value-gson.

gabrielittner avatar gabrielittner commented on July 20, 2024

I don't think managing would be a problem, one shared abstract base class and the two implementations would just have to implement applicable(). But you're right that it's probably too confusing.

from auto-value-gson.

lenguyenthanh avatar lenguyenthanh commented on July 20, 2024

There is only one problem with no runtime module is Android Studio cannot understand @Gson annotation when we use it with apt plugin. So we have to include the library with apt and provided. Something like this:

// Do not compile AutoValue dependencies to the app.
apt libraries.autoValue
// Make AutoValue annotation visible to the compiler.
provided libraries.autoValue

But I think it's IDE bug, so it may not effect to our decision.

from auto-value-gson.

JakeWharton avatar JakeWharton commented on July 20, 2024

It is not an IDE bug. Annotations should be separate from their compilers.

On Wed, Mar 30, 2016 at 11:23 PM Thanh Le [email protected] wrote:

There is only one problem with no runtime module is Android Studio cannot
understand @gson annotation when we use it with apt plugin. So we have to
include the library with apt and provided. Something like this:

// Do not compile AutoValue dependencies to the app.
apt libraries.autoValue
// Make AutoValue annotation visible to the compiler.
provided libraries.autoValue

But I think it's IDE bug, so it may not effect to our decision.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#17 (comment)

from auto-value-gson.

lenguyenthanh avatar lenguyenthanh commented on July 20, 2024

Thanks @JakeWharton for correting my mistake.

from auto-value-gson.

artem-zinnatullin avatar artem-zinnatullin commented on July 20, 2024

Hm, idk but I'd like to explicitly mark something with @Gson/etc to generate gson mapping. We have a lot of immutable classes and only some part of them needs json serialization.

Imagine you're reading a class and not even realizing that it's going to be jsonable.

from auto-value-gson.

dimsuz avatar dimsuz commented on July 20, 2024

Would also love to see @Moshi :)

from auto-value-gson.

JakeWharton avatar JakeWharton commented on July 20, 2024

File a bug on https://github.com/rharter/auto-value-moshi/

from auto-value-gson.

lenguyenthanh avatar lenguyenthanh commented on July 20, 2024

I think it is a good idea. It explicit Gson type without adding another annotation.

from auto-value-gson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.