|
| 1 | +/* |
| 2 | + * Copyright 2002-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.converter.json; |
| 18 | + |
| 19 | +import java.io.Reader; |
| 20 | +import java.io.Writer; |
| 21 | +import java.lang.reflect.Type; |
| 22 | +import javax.json.bind.Jsonb; |
| 23 | +import javax.json.bind.JsonbBuilder; |
| 24 | +import javax.json.bind.JsonbConfig; |
| 25 | + |
| 26 | +import org.springframework.util.Assert; |
| 27 | + |
| 28 | +/** |
| 29 | + * Implementation of {@link org.springframework.http.converter.HttpMessageConverter} |
| 30 | + * that can read and write JSON using the |
| 31 | + * <a href="http://json-b.net/">JSON Binding API</a>. |
| 32 | + * |
| 33 | + * <p>This converter can be used to bind to typed beans or untyped {@code HashMap}s. |
| 34 | + * By default, it supports {@code application/json} and {@code application/*+json} with |
| 35 | + * {@code UTF-8} character set. |
| 36 | + * |
| 37 | + * @author Juergen Hoeller |
| 38 | + * @since 5.0 |
| 39 | + * @see javax.json.bind.Jsonb |
| 40 | + * @see javax.json.bind.JsonbBuilder |
| 41 | + * @see #setJsonb |
| 42 | + */ |
| 43 | +public class JsonbHttpMessageConverter extends AbstractJsonHttpMessageConverter { |
| 44 | + |
| 45 | + private Jsonb jsonb; |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Construct a new {@code JsonbHttpMessageConverter} with default configuration. |
| 50 | + */ |
| 51 | + public JsonbHttpMessageConverter() { |
| 52 | + this(JsonbBuilder.create()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Construct a new {@code JsonbHttpMessageConverter} with the given configuration. |
| 57 | + * @param config the {@code JsonbConfig} for the underlying delegate |
| 58 | + */ |
| 59 | + public JsonbHttpMessageConverter(JsonbConfig config) { |
| 60 | + this(JsonbBuilder.create(config)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Construct a new {@code JsonbHttpMessageConverter} with the given delegate. |
| 65 | + * @param jsonb the Jsonb instance to use |
| 66 | + */ |
| 67 | + public JsonbHttpMessageConverter(Jsonb jsonb) { |
| 68 | + setJsonb(jsonb); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Set the {@code Jsonb} instance to use. |
| 74 | + * If not set, a default {@code Jsonb} instance will be created. |
| 75 | + * <p>Setting a custom-configured {@code Jsonb} is one way to take further |
| 76 | + * control of the JSON serialization process. |
| 77 | + * @see #JsonbHttpMessageConverter(Jsonb) |
| 78 | + * @see #JsonbHttpMessageConverter(JsonbConfig) |
| 79 | + * @see JsonbBuilder |
| 80 | + */ |
| 81 | + public void setJsonb(Jsonb jsonb) { |
| 82 | + Assert.notNull(jsonb, "A Jsonb instance is required"); |
| 83 | + this.jsonb = jsonb; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Return the configured {@code Jsonb} instance for this converter. |
| 88 | + */ |
| 89 | + public Jsonb getJsonb() { |
| 90 | + return this.jsonb; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + @Override |
| 95 | + protected Object readInternal(Type resolvedType, Reader reader) throws Exception { |
| 96 | + return getJsonb().fromJson(reader, resolvedType); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected void writeInternal(Object o, Type type, Writer writer) throws Exception { |
| 101 | + if (type != null) { |
| 102 | + getJsonb().toJson(o, type, writer); |
| 103 | + } |
| 104 | + else { |
| 105 | + getJsonb().toJson(o, writer); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments