jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • Jackson JSON Processor
  • JACKSON-77

Add annotation for ignoring unknown properties (@JsonIgnoreProperties?)

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.4
  • Component/s: None
  • Labels:
    None

Description

In addition to other methods of dealing with "unknown" properties (Json Object fields of which name does not match any declared setter or constructor arg of type to deserialize to), it would be nice to have a simple annotation that allows:

  • enumerating properties to ignore
  • specify "all unknown" (perhaps String "*")
  • possibly limited wildcarding (suffix/prefix with leading/trailing asterisk)

to cover a common case of having to ignore some or all properties (legacy data that gets passed in but that is not to be mapped or such).

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Tatu Saloranta added a comment - 30/Mar/09 3:35 PM

One additional use case: this could also be used to prevent binding of sub-trees that are not useful. If such property/-ies are defined using this method, such sub-trees are skipped without binding. This is more efficient than having a dummy set method that ignores its argument.

Show
Tatu Saloranta added a comment - 30/Mar/09 3:35 PM One additional use case: this could also be used to prevent binding of sub-trees that are not useful. If such property/-ies are defined using this method, such sub-trees are skipped without binding. This is more efficient than having a dummy set method that ignores its argument.
Hide
Permalink
Tatu Saloranta added a comment - 20/Jul/09 11:43 AM

Thinking bit more about this, I think this can be implemented with just 2 pieces:

1. Add a DeserializationConfig.Feature that will set default handling of unknown properties; (error or ignore), to be used if other methods fails (i.e. for properties that match no other processing)
2. Make @JsonIgnoreProperties take a class that implements interface (IgnorableFilter or whatever), to be used for determining handling of unknown properties (and perhaps couple of standard implementations)

Latter would allow any kind of handling, provided it takes enough information (parser, context; perhaps Bean instance property logically belongs to).

One potential problem: what to do with "deferred" deserialization; that is, cases where bean instantiation occurs after encountering such properties (can happen with non-default constructors (new 1.2 feature) at least).

Show
Tatu Saloranta added a comment - 20/Jul/09 11:43 AM Thinking bit more about this, I think this can be implemented with just 2 pieces: 1. Add a DeserializationConfig.Feature that will set default handling of unknown properties; (error or ignore), to be used if other methods fails (i.e. for properties that match no other processing) 2. Make @JsonIgnoreProperties take a class that implements interface (IgnorableFilter or whatever), to be used for determining handling of unknown properties (and perhaps couple of standard implementations) Latter would allow any kind of handling, provided it takes enough information (parser, context; perhaps Bean instance property logically belongs to). One potential problem: what to do with "deferred" deserialization; that is, cases where bean instantiation occurs after encountering such properties (can happen with non-default constructors (new 1.2 feature) at least).
Hide
Permalink
Tatu Saloranta added a comment - 29/Jul/09 1:10 AM

Given time constraints, I will split this request into 2 issues: one (this one) will deal with annotation(s) to add; and another one (which I will create) that only deals with the global default handling.
Only latter will be implemented for 1.2; annotation will be added to 1.3 so that there is little bit more to hash out details so that the new feature will work well, cover enough use cases, and is consistent with rest of API.

Show
Tatu Saloranta added a comment - 29/Jul/09 1:10 AM Given time constraints, I will split this request into 2 issues: one (this one) will deal with annotation(s) to add; and another one (which I will create) that only deals with the global default handling. Only latter will be implemented for 1.2; annotation will be added to 1.3 so that there is little bit more to hash out details so that the new feature will work well, cover enough use cases, and is consistent with rest of API.
Hide
Permalink
Tatu Saloranta added a comment - 29/Jul/09 1:19 AM

Created JACKSON-147 to cover the global default setting for error reporting.

Show
Tatu Saloranta added a comment - 29/Jul/09 1:19 AM Created JACKSON-147 to cover the global default setting for error reporting.
Hide
Permalink
Tatu Saloranta added a comment - 07/Nov/09 11:36 PM

Will implement a simple solution; annotation (@JsonIgnoreProperties) with just two choices – explicit list of ignorable properties, or "ignore any unknown". But it will apply to both serialization (explicit list) and deserialization (explicit, ignore-unknown).
And finally, if a non-annotation method is desired in future, it can be added based on same internal mechanism.

Show
Tatu Saloranta added a comment - 07/Nov/09 11:36 PM Will implement a simple solution; annotation (@JsonIgnoreProperties) with just two choices – explicit list of ignorable properties, or "ignore any unknown". But it will apply to both serialization (explicit list) and deserialization (explicit, ignore-unknown). And finally, if a non-annotation method is desired in future, it can be added based on same internal mechanism.
Hide
Permalink
Tatu Saloranta added a comment - 10/Nov/09 12:58 AM

Implemented

Show
Tatu Saloranta added a comment - 10/Nov/09 12:58 AM Implemented
Hide
Permalink
zhouyanming added a comment - 14/Dec/09 3:17 AM

does it support custom annotation?

public class User{
...
@DontIncludeMe
private String getPassword(){ return password; } }

Show
zhouyanming added a comment - 14/Dec/09 3:17 AM does it support custom annotation? public class User{ ... @DontIncludeMe private String getPassword(){ return password; } }
Hide
Permalink
Tatu Saloranta added a comment - 14/Dec/09 1:19 PM

Not by default.
But you could implement support by defining your own AnnotationIntrospector implementation, which overrides methods that are used to find annotation settings. There is a base implementation (NopAnnotationIntrospector) that has dummy implementations for all methods, so you could extend that, just override 2 methods you need.

Show
Tatu Saloranta added a comment - 14/Dec/09 1:19 PM Not by default. But you could implement support by defining your own AnnotationIntrospector implementation, which overrides methods that are used to find annotation settings. There is a base implementation (NopAnnotationIntrospector) that has dummy implementations for all methods, so you could extend that, just override 2 methods you need.
Hide
Permalink
zhouyanming added a comment - 14/Dec/09 8:38 PM

thanks,I have override JacksonAnnotationIntrospector,actually 3 methods,isHandled(Annotation) must return true

private static ObjectMapper objectMapper = new ObjectMapper()
.setSerializationConfig(new SerializationConfig(
new BasicClassIntrospector(),
new JacksonAnnotationIntrospector() {
public boolean isHandled(Annotation ann) { return true; }

@Override
public boolean isIgnorableField(AnnotatedField f) { return super.isIgnorableField(f) || isIgnorable(f); }

@Override
public boolean isIgnorableMethod(AnnotatedMethod m) { return super.isIgnorableMethod(m) || isIgnorable(m); }

private boolean isIgnorable(Annotated a) { return (a.getAnnotation(NotInJson.class) != null || a .getAnnotation(NotInCopy.class) != null); }
}));

BTW: how to assign serializedName like @SerializedName in google-gson

@SerializedName("outline-colour")// because '-' is not allowed in java syntax
private String outlineColour;

I will move from gson to jackson, hope jackson can handle this.

Show
zhouyanming added a comment - 14/Dec/09 8:38 PM thanks,I have override JacksonAnnotationIntrospector,actually 3 methods,isHandled(Annotation) must return true private static ObjectMapper objectMapper = new ObjectMapper() .setSerializationConfig(new SerializationConfig( new BasicClassIntrospector(), new JacksonAnnotationIntrospector() { public boolean isHandled(Annotation ann) { return true; } @Override public boolean isIgnorableField(AnnotatedField f) { return super.isIgnorableField(f) || isIgnorable(f); } @Override public boolean isIgnorableMethod(AnnotatedMethod m) { return super.isIgnorableMethod(m) || isIgnorable(m); } private boolean isIgnorable(Annotated a) { return (a.getAnnotation(NotInJson.class) != null || a .getAnnotation(NotInCopy.class) != null); } })); BTW: how to assign serializedName like @SerializedName in google-gson @SerializedName("outline-colour")// because '-' is not allowed in java syntax private String outlineColour; I will move from gson to jackson, hope jackson can handle this.
Hide
Permalink
Tatu Saloranta added a comment - 14/Dec/09 9:51 PM

Ok good. Yes, forgot to mention that isHandled() needs to be implemented; it's ok to just return true, although the idea is to return true just for annotations that your code handles. But that's just an optimization (to ignore annotations used for other purposes).

Naming of JSON properties (fields, methods) can be done using @JsonProperty annotation. That will also serve to indicate serializable/deserializable fields.
Or if you want to customize that, there are separate methods in AnnotationIntrospector.

I hope things work out!

Btw; feel free to send these questions on user or dev lists – that way other users can also suggest ways to do things as well as learn new techniques.

Show
Tatu Saloranta added a comment - 14/Dec/09 9:51 PM Ok good. Yes, forgot to mention that isHandled() needs to be implemented; it's ok to just return true, although the idea is to return true just for annotations that your code handles. But that's just an optimization (to ignore annotations used for other purposes). Naming of JSON properties (fields, methods) can be done using @JsonProperty annotation. That will also serve to indicate serializable/deserializable fields. Or if you want to customize that, there are separate methods in AnnotationIntrospector. I hope things work out! Btw; feel free to send these questions on user or dev lists – that way other users can also suggest ways to do things as well as learn new techniques.
Hide
Permalink
Tatu Saloranta added a comment - 14/Feb/10 4:37 PM

Included in 1.4.0 release.

Show
Tatu Saloranta added a comment - 14/Feb/10 4:37 PM Included in 1.4.0 release.

People

  • Assignee:
    Tatu Saloranta
    Reporter:
    Tatu Saloranta
Vote (0)
Watch (1)

Dates

  • Created:
    18/Mar/09 1:45 AM
    Updated:
    14/Feb/10 4:37 PM
    Resolved:
    10/Nov/09 12:58 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.