DZone
Integration Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Hacking With the JSONB 1.0 Public Review [Snippet]

Hacking With the JSONB 1.0 Public Review [Snippet]

This article gives a quick example of JSR 367, the JSONB spec.

Sebastian Daschner user avatar by
Sebastian Daschner
·
Jul. 01, 16 · Integration Zone · Tutorial
Like (1)
Save
Tweet
1.69K Views

Join the DZone community and get the full member experience.

Join For Free

The Public Review of JSR 367 (JSONB 1.0) has just recently been announced. The snapshot version can already be tested.

For an example see the JSONB module of my JAX-RS Hypermedia project.

As JSONB — provided by the reference implementation — can’t obviously be integrated in JAX-RS yet, we need some boilerplate code in the JAX-RS resource methods.

See the CartResource class as an example:

@Path("shopping_cart")
public class CartResource {

    ...

    // JAX-RS 2.0 obviously isn't integrated with JSONB yet
    private Jsonb jsonb;

    // as deserialization is done manually, validation also isn't integrated by JAX-RS
    @Inject
    Validator validator;

    @PostConstruct
    public void initJsonb() {
        jsonb = JsonbBuilder.create();
    }

    @GET
    public StreamingOutput getShoppingCart() {
        final ShoppingCart cart = shoppingCart.getShoppingCart();
        ...

        return output -> jsonb.toJson(cart, output);
    }

    @POST
    public void addItem(InputStream input) {
        final CartInsertion insertion = jsonb.fromJson(input, CartInsertion.class);

        final Set<ConstraintViolation<CartUpdate>> violations = validator.validate(insertion);
        if (!violations.isEmpty())
            throwBadRequest(violations);

        ...
    }
}

This shows us one of the most important benefits of using Java EE: being able to seamlessly integrate various specifications — without these extra efforts of course — as soon as the new specs are final and incorporated.

Reference implementation Boilerplate code Integration Java (programming language) Hypermedia Implementation Snapshot (computer storage)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Database B-Tree Indexing Works
  • What Is URL Rewriting? | Java Servlets
  • Role of Development Team in an Agile Environment
  • 10 Steps to Become an Outstanding Java Developer

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo