DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

JSON-B Asymmetrical Property Binding [Snippet]

Let's take a brief look at how you can use the JSON-B specification to quickly and easily map Java objects to JSON and back again.

Sebastian Daschner user avatar by
Sebastian Daschner
·
Aug. 22, 18 · Code Snippet
Like (3)
Save
Tweet
Share
9.25K Views

Join the DZone community and get the full member experience.

Join For Free

The JSON-B specification defines binding annotations, such as @JsonbProperty or @JsonbTransient, to declaratively map Java objects to JSON and back. These annotations can be used asymmetrically to define the different handling of serialization and deserialization.

If JSON binding annotations are annotated on Java properties, or on both getters and setters, they will control how the objects are serialized and deserialized. If they are only defined on either the getter or the setter, the behavior will only take action for either serialization or deserialization, respectively. The binding definitions for multiple properties can be mixed and matched within a single class.

See the following example:

public class Account {

    private long id;

    // will always map name to testName
    @JsonbProperty("testName")
    private String name;

    // will serialize id to JSON
    public long getId() {
        return id;
    }

    // will not deserialize id from JSON
    @JsonbTransient
    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


Happy asymmetrical JSON binding!

Binding (linguistics) Property (programming)

Published at DZone with permission of Sebastian Daschner. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • Rust vs Go: Which Is Better?
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • How Elasticsearch Works

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: