> I was thinking about annotating the constructors with property names
> (because the parameter names aren't retained at runtime) and using a
> custom InstanceCreator. For example a Person that always needs a valid
> firstName and a lastName would have the constructor:
>
> @ConstructorProperties({"firstName", "lastName"})
> public Person(String firstName, String lastName);
>
> Then when a Person needed to be created, properties firstName and
> lastName would be passed into the constructor.
This leads to a slippery slope of incorporating all of Guice
functionality into Gson. For example, what if the Constructor
parameters are rich Java types themselves? The code is not all that
hard to write (especially since Guice already does it) but probably
has much more unintended consequences than what we have. We dont want
Gson to turn into a mini-Guice.
What you are asking for can actually be done fairly simply by
registering your own custom deserializer that does the needed work
itself and calls Context.deserialize() for the remaining stuff. We
are also planning to provide a Context.serializeInternal() method (see
http://code.google.com/p/google-gson/issues/detail?id=43) that can be
used as to create a Deserializer that in-effect is a post or pre
create invocation.
I understand that for JPA entities, you do not want to write a custom
deserializer for each entity. You probably would prefer just
annotating the classes themselves with extra annotations. However,
that will pollute them with an external represenation and is probably
harder to maintain.
> However after looking into this a bit more, I don't think it's
> currently possible, since InstanceCreator#createInstance(Type type)
> doesn't have access to the properties "in advance". Would this be easy
> to fix, or does it break the whole philosophy?
If Gson is doing all the work then we can do anything we want.
However, if its a user-written instance creator then we have to see
how we can accomplish this while maintaining backwards compatibility.
I am not sure why you would not just write a deserializer instead of
an InstanceCreator in this case. Deserializers have full access to the
properties. InstanceCreators do not (that was an oversight while
designing our API, we should have provided Json tree as a parameter).
But InstanceCreators are not really necessary for most cases. They are
just an optimization when all you want to do is to provide an ability
to create a default instance.
> As for the reuse of JPA entities: I think if you could build detached
> entities using Gson like this, you could use EntityManager#merge() to
> update persistent ones directly.
>
> But this is my fallback plan. Currently, I'm working on something
> (imo) a bit cooler. If you're interested, see:
>
>
http://stackoverflow.com/questions/731989/partial-bean-serialization-...
Cool. IMHO it is useful to introduce additional facades for Web layer
and not expose the database entities directly. If you decide to do so,
Gson will be much easier to use.
Thanks
Inder