GithubHelp home page GithubHelp logo

Comments (3)

johncarl81 avatar johncarl81 commented on May 8, 2024

@lexs I like the idea. We'd probably have to introduce a configuration parameter to tell Parceler to use the constructor or fields instead of the default methods. In addition, the constructor parameters may have to be aligned with the associated getters if the same type is used more than once.

Would you like to take a crack at this?

from parceler.

lexs avatar lexs commented on May 8, 2024

@johncarl81 I think it could default to constructor style, if it can't find a constructor it will fall back to the other path.

I'll have a lot more free time in two weeks, might take a crack at it then.

from parceler.

johncarl81 avatar johncarl81 commented on May 8, 2024

@lexs The more I think about this the more I want to move to a Gson style of serialization (via field) as default and allow the configuration of Constructor/Method serialization if desired. If you've used Simple XML, for the most part this is the way it works. I think this is more inline with Android and consistent with other serialization frameworks. Also, as you mentioned, this supports an immutable pattern as well.

I see a couple hurdles in getting there:

  1. Should we do anything with private fields if in Field serialization mode? We could, but it would incur a reflection performance penalty.
  2. Disambiguating constructor parameters to method parameters would be a challenge if multiple properties of the same type were used. Perhaps we should assume a type to type mapping, then only fail if multiple properties of the same type were used.
  3. What if multiple constructors are given, should we assume the greediest constructor (longest) or require a single constructor to be annotated?
  4. If classes are not under the control of the developer (@ParcelClass), should we give the option of specifying a SerializationMode?

Here's the use cases:

Field mode:

@Parcel
public class FieldMode{
    String value;
    public String getValue() { return value;}
}

Constructor / Method (immutable and not ambiguous):

@Parcel(SerializationMode.METHOD)
public class MethodMode{
    private final String value;
    public MethodMode(String value){
        this.value = value;
    }
    public String getValue() { return value;}
}

Constructor / Method (ambiguous):

@Parcel(SerializationMode.METHOD)
public class AmbiguousMode{
    private String one;
    private String two;
    public AmbiguousMode(){}
    @ParcelConstructor
    public AmbiguousMode(@ParcelElement("one") String oneInput, @ParcelElement("two") String twoInput){
        this.one = oneInput;
        this.two = twoInput;
    }
    @ParcelElement("one") //not necessary because getter property is named "one"
    public String getOne(){ return one; }
    @ParcelElement("two") //not necessary because getter property is named "two"
    public String getTwo(){ return two; }
}

Method only:

@Parcel(SerializationMode.METHOD)
public class MethodMode{
    private String value;
    public String getValue(){ return value; }
    public void setValue(String value){ this.value = value; }
}

Thoughts?

from parceler.

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.