discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

add more flexable ability to GsonBuilder for filtering property #122

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
before I know Gson I have a own annotation named @NotInJson,It's familar
with @Expose,and @NotInJson is exclude not include,I hope Gson can provide
a exclude annotation named @NotExpose or provide a mechanism let developers
process filtering,thanks.

Original issue reported on code.google.com by zhouyanm...@gmail.com on 6 May 2009 at 8:07

GoogleCodeExporter commented 9 years ago
attachment is patch 

add two methods for GsonBuilder

registerExclusionStrategy(ExclusionStrategy... exclusionStrategies)
excludeFieldsWithAnnotations(Class<?extends Annotation>...annotationClasses)

Original comment by zhouyanm...@gmail.com on 6 May 2009 at 9:28

Attachments:

GoogleCodeExporter commented 9 years ago
You can mark fields as "transient" and Gson will skip over them.  In Java, the
"transient" is used to mark a field that should not appear in the class's 
serialized
or persisted state.

Does this solution work for you?

Original comment by joel.leitch@gmail.com on 12 May 2009 at 4:24

GoogleCodeExporter commented 9 years ago
I know that,there are two problems
1.I hope that does not require source code changes for switching library
2.transient can effect other function like serialization

Original comment by zhouyanm...@gmail.com on 13 May 2009 at 1:26

GoogleCodeExporter commented 9 years ago
Sorry, but I don't think that I understand your first point from above.

Using a Java annotation would still require source changes because you'd have to
annotate your field or class with an annotation such as "@Ignore".

Writing the code to support this features is actually quite simple because of 
the
ExclusionStrategy concept.  We already do something very similar to this for 
the API
versioning control; however, I am not completely convinced that adding an 
annotation
for this purpose is better than specifying the field as "transient".  I 
understand
that this affect the default Java object serialization, but you can assume that
passing an object through Gson is just serializing an object into a JSON format,
which can later be translated back into a Java Object instance.

@Inder, what are your thoughts on this feature request?

Original comment by joel.leitch@gmail.com on 13 May 2009 at 7:25

GoogleCodeExporter commented 9 years ago
I think we should have an annotation way of excluding fields too. Requiring 
that the 
users use transient may have unintended side-effects.

Original comment by inder123 on 14 May 2009 at 12:28

GoogleCodeExporter commented 9 years ago
hi,joel,before I know gson,I use my own utils using json.org to serialize 
objects,and
I have defined a annotation @NotInJson for my application,so I wish I can reuse 
this
annotation and needn't make changes for my annotated classes when I switch lib 
from
json.org to gson.

Original comment by zhouyanm...@gmail.com on 14 May 2009 at 1:11

GoogleCodeExporter commented 9 years ago
Hello Joel,
Marking a field as transient does not solve the problem. For example: Class A
reference class B (class A has a collection reference of type class B), if I
serialization a collection instance of class A, it will serialize all the 
references
of class B as well, which is fine. But when I have another class, let called it 
C,
class C have a collection reference of class A, and when I serialize instances 
of
class C, I don't want all instances class B to be serialized (maybe because 
they are
unneccessary or way too big). I know that marking a field as transient is good 
for
serialization and deserialization back, but in some case, I only use gson just 
for
serialization. I'm developing an application which uses GWT and GWT-Ext, for
deserialization I use the built-in mechanism of GWT but for serialization 
(mostly
consumed by GWT-Ext widgets) I use gson.

Original comment by xuantinh@gmail.com on 21 Aug 2009 at 12:15

GoogleCodeExporter commented 9 years ago
In Gson 1.4, we extended the Expose annotation to support finer-grained control 
over
what gets output or not. The expose annotation now supports a serialize and
deserialize parameter, if you set both to false, then that is effectively same 
as
@NotInJson.

 Expose (serialize = false, deserialize = true) 

Original comment by inder123 on 24 Aug 2009 at 5:28

GoogleCodeExporter commented 9 years ago
is it possible let users make their own annotation?
I need this @NotInJson for compatibility with some code written by org.json.xxx 
before

Original comment by zhouyanm...@gmail.com on 25 Aug 2009 at 1:08

GoogleCodeExporter commented 9 years ago
My gut reaction will be to advise to use a custom serializer/deserializer to do 
this,
but do you have a proposal on how you would like to see it work?

Original comment by inder123 on 25 Aug 2009 at 1:19

GoogleCodeExporter commented 9 years ago
please see my patch in Comment 1

Original comment by zhouyanm...@gmail.com on 25 Aug 2009 at 1:51