Package io.jooby.gson

Class GsonModule

java.lang.Object
io.jooby.gson.GsonModule
All Implemented Interfaces:
io.jooby.Extension, io.jooby.MessageDecoder, io.jooby.MessageEncoder

public class GsonModule extends Object implements io.jooby.Extension, io.jooby.MessageDecoder, io.jooby.MessageEncoder
JSON module using Gson: https://github.com/google/gson.

Usage:


 {

   install(new GsonModule());

   get("/", ctx -> {
     MyObject myObject = ...;
     // send json
     return myObject;
   });

   post("/", ctx -> {
     // read json
     MyObject myObject = ctx.body(MyObject.class);
     // send json
     return myObject;
   });
 }
 
For body decoding the client must specify the Content-Type header set to application/json.

You can retrieve the Gson object via require call:


 {

   Gson gson = require(Gson.class);

 }
 
Complete documentation is available at: https://jooby.io/modules/gson.
Since:
2.7.2
Author:
edgar
  • Field Summary

    Fields inherited from interface io.jooby.MessageDecoder

    UNSUPPORTED_MEDIA_TYPE

    Fields inherited from interface io.jooby.MessageEncoder

    TO_STRING
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new Gson module.
    GsonModule(com.google.gson.Gson gson)
    Creates a new module and use a Gson instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    decode(io.jooby.Context ctx, Type type)
     
    io.jooby.output.Output
    encode(io.jooby.Context ctx, Object value)
     
    void
    install(io.jooby.Jooby application)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.jooby.Extension

    lateinit
  • Constructor Details

    • GsonModule

      public GsonModule(@NonNull com.google.gson.Gson gson)
      Creates a new module and use a Gson instance.
      Parameters:
      gson - Gson to use.
    • GsonModule

      public GsonModule()
      Creates a new Gson module.
  • Method Details

    • install

      public void install(@NonNull io.jooby.Jooby application)
      Specified by:
      install in interface io.jooby.Extension
    • decode

      @NonNull public Object decode(@NonNull io.jooby.Context ctx, @NonNull Type type) throws Exception
      Specified by:
      decode in interface io.jooby.MessageDecoder
      Throws:
      Exception
    • encode

      @NonNull public io.jooby.output.Output encode(@NonNull io.jooby.Context ctx, @NonNull Object value)
      Specified by:
      encode in interface io.jooby.MessageEncoder