Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 7b0247f

Browse files
committed
Add Jackson view support. fix jooby-project#1087
1 parent 743956a commit 7b0247f

File tree

7 files changed

+199
-279
lines changed

7 files changed

+199
-279
lines changed

doc/doc/jackson/jackson.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,27 @@ import org.jooby.json.Jackson;
3737
}
3838
```
3939

40-
### advanced configuration
40+
## views
41+
42+
Dynamic views are supported via `org.jooby.json.JacksonView` object:
43+
44+
```java
45+
{
46+
use(new Jackson());
47+
48+
get("/public", req -> {
49+
Item item = ...
50+
return new JacksonView<>(Views.Public.class, item);
51+
});
52+
53+
get("/internal", req -> {
54+
Item item = ...
55+
return new JacksonView<>(Views.Internal.class, item);
56+
});
57+
}
58+
```
59+
60+
## advanced configuration
4161

4262
If you need a special setting or configuration for your [ObjectMapper](http://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/ObjectMapper.html):
4363

modules/jooby-jackson/src/main/java/org/jooby/json/Jackson.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -203,25 +203,6 @@
203203
*/
204204
package org.jooby.json;
205205

206-
import static java.util.Objects.requireNonNull;
207-
208-
import java.text.SimpleDateFormat;
209-
import java.util.ArrayList;
210-
import java.util.List;
211-
import java.util.Locale;
212-
import java.util.Optional;
213-
import java.util.Set;
214-
import java.util.TimeZone;
215-
import java.util.function.Consumer;
216-
217-
import javax.inject.Inject;
218-
219-
import org.jooby.Env;
220-
import org.jooby.Jooby;
221-
import org.jooby.MediaType;
222-
import org.jooby.Parser;
223-
import org.jooby.Renderer;
224-
225206
import com.fasterxml.jackson.databind.Module;
226207
import com.fasterxml.jackson.databind.ObjectMapper;
227208
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
@@ -232,6 +213,22 @@
232213
import com.google.inject.multibindings.Multibinder;
233214
import com.google.inject.name.Names;
234215
import com.typesafe.config.Config;
216+
import static java.util.Objects.requireNonNull;
217+
import org.jooby.Env;
218+
import org.jooby.Jooby;
219+
import org.jooby.MediaType;
220+
import org.jooby.Parser;
221+
import org.jooby.Renderer;
222+
223+
import javax.inject.Inject;
224+
import java.text.SimpleDateFormat;
225+
import java.util.ArrayList;
226+
import java.util.List;
227+
import java.util.Locale;
228+
import java.util.Optional;
229+
import java.util.Set;
230+
import java.util.TimeZone;
231+
import java.util.function.Consumer;
235232

236233
/**
237234
* <h1>jackson</h1>
@@ -265,6 +262,25 @@
265262
* }
266263
* </pre>
267264
*
265+
* <h2>views</h2>
266+
* <p>Dynamic views are supported via {@link JacksonView}:</p>
267+
*
268+
* <pre>{@code
269+
* {
270+
* use(new Jackson());
271+
*
272+
* get("/public", req -> {
273+
* Item item = ...;
274+
* return new JacksonView<>(Views.Public.class, item);
275+
* });
276+
*
277+
* get("/public", req -> {
278+
* Item item = ...;
279+
* return new JacksonView<>(Views.Internal.class, item);
280+
* });
281+
* }
282+
* }</pre>
283+
*
268284
* <h2>advanced configuration</h2>
269285
* <p>
270286
* If you need a special setting or configuration for your {@link ObjectMapper}:
@@ -462,7 +478,6 @@ public void configure(final Env env, final Config config, final Binder binder) {
462478
// direct access?
463479
binder.bind(Key.get(Renderer.class, Names.named(renderer.toString()))).toInstance(renderer);
464480
binder.bind(Key.get(Parser.class, Names.named(parser.toString()))).toInstance(parser);
465-
466481
}
467482

468483
}

modules/jooby-jackson/src/main/java/org/jooby/json/JacksonBaseRenderer.java

Lines changed: 0 additions & 242 deletions
This file was deleted.

modules/jooby-jackson/src/main/java/org/jooby/json/JacksonRawRenderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@
203203
*/
204204
package org.jooby.json;
205205

206-
import org.jooby.MediaType;
207-
208206
import com.fasterxml.jackson.databind.ObjectMapper;
207+
import org.jooby.MediaType;
208+
import org.jooby.Renderer;
209209

210210
class JacksonRawRenderer extends JacksonRenderer {
211211

@@ -214,7 +214,7 @@ public JacksonRawRenderer(final ObjectMapper mapper, final MediaType type) {
214214
}
215215

216216
@Override
217-
protected void renderValue(final Object value, final Context ctx) throws Exception {
217+
protected void renderValue(final Object value, final Renderer.Context ctx) throws Exception {
218218
if (value instanceof CharSequence) {
219219
ctx.send(value.toString());
220220
} else {

0 commit comments

Comments
 (0)