|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.json; |
| 7 | + |
| 8 | +import com.google.gson.Gson; |
| 9 | +import com.google.gson.GsonBuilder; |
| 10 | +import io.jooby.Body; |
| 11 | +import io.jooby.Context; |
| 12 | +import io.jooby.Extension; |
| 13 | +import io.jooby.Jooby; |
| 14 | +import io.jooby.MediaType; |
| 15 | +import io.jooby.MessageDecoder; |
| 16 | +import io.jooby.MessageEncoder; |
| 17 | +import io.jooby.ServiceRegistry; |
| 18 | + |
| 19 | +import javax.annotation.Nonnull; |
| 20 | +import java.io.ByteArrayInputStream; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.io.InputStreamReader; |
| 23 | +import java.lang.reflect.Type; |
| 24 | + |
| 25 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 26 | + |
| 27 | +/** |
| 28 | + * JSON module using Gson: https://github.com/google/gson. |
| 29 | + * |
| 30 | + * Usage: |
| 31 | + * |
| 32 | + * <pre>{@code |
| 33 | + * { |
| 34 | + * |
| 35 | + * install(new GsonModule()); |
| 36 | + * |
| 37 | + * get("/", ctx -> { |
| 38 | + * MyObject myObject = ...; |
| 39 | + * // send json |
| 40 | + * return myObject; |
| 41 | + * }); |
| 42 | + * |
| 43 | + * post("/", ctx -> { |
| 44 | + * // read json |
| 45 | + * MyObject myObject = ctx.body(MyObject.class); |
| 46 | + * // send json |
| 47 | + * return myObject; |
| 48 | + * }); |
| 49 | + * } |
| 50 | + * }</pre> |
| 51 | + * |
| 52 | + * For body decoding the client must specify the <code>Content-Type</code> header set to |
| 53 | + * <code>application/json</code>. |
| 54 | + * |
| 55 | + * You can retrieve the {@link Gson} object via require call: |
| 56 | + * |
| 57 | + * <pre>{@code |
| 58 | + * { |
| 59 | + * |
| 60 | + * Gson gson = require(Gson.class); |
| 61 | + * |
| 62 | + * } |
| 63 | + * }</pre> |
| 64 | + * |
| 65 | + * Complete documentation is available at: https://jooby.io/modules/gson. |
| 66 | + * |
| 67 | + * @author edgar |
| 68 | + * @since 2.7.2 |
| 69 | + */ |
| 70 | +public class GsonModule implements Extension, MessageDecoder, MessageEncoder { |
| 71 | + |
| 72 | + private Gson gson; |
| 73 | + |
| 74 | + /** |
| 75 | + * Creates a new module and use a Gson instance. |
| 76 | + * |
| 77 | + * @param gson Gson to use. |
| 78 | + */ |
| 79 | + public GsonModule(@Nonnull Gson gson) { |
| 80 | + this.gson = gson; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates a new Gson module. |
| 85 | + */ |
| 86 | + public GsonModule() { |
| 87 | + this(new GsonBuilder().create()); |
| 88 | + } |
| 89 | + |
| 90 | + @Override public void install(@Nonnull Jooby application) throws Exception { |
| 91 | + application.decoder(MediaType.json, this); |
| 92 | + application.encoder(MediaType.json, this); |
| 93 | + |
| 94 | + ServiceRegistry services = application.getServices(); |
| 95 | + services.put(Gson.class, gson); |
| 96 | + } |
| 97 | + |
| 98 | + @Nonnull @Override public Object decode(@Nonnull Context ctx, @Nonnull Type type) |
| 99 | + throws Exception { |
| 100 | + Body body = ctx.body(); |
| 101 | + if (body.isInMemory()) { |
| 102 | + return gson.fromJson(new InputStreamReader(new ByteArrayInputStream(body.bytes())), type); |
| 103 | + } else { |
| 104 | + try (InputStream stream = body.stream()) { |
| 105 | + return gson.fromJson(new InputStreamReader(stream), type); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Nonnull @Override public byte[] encode(@Nonnull Context ctx, @Nonnull Object value) { |
| 111 | + ctx.setDefaultResponseType(MediaType.json); |
| 112 | + return gson.toJson(value).getBytes(UTF_8); |
| 113 | + } |
| 114 | +} |
0 commit comments