1+ /*
2+ * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
3+ *
4+ * This program and the accompanying materials are made available under the
5+ * terms of the Eclipse Public License 2.0 which is available at
6+ * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+ * which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+ *
9+ * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+ */
111package io .vertx .core .json .jackson .v3 ;
212
313import io .netty .buffer .ByteBufInputStream ;
3040import static io .vertx .core .json .impl .JsonUtil .BASE64_ENCODER ;
3141import static java .time .format .DateTimeFormatter .ISO_INSTANT ;
3242
43+ /**
44+ * @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
45+ */
3346public class JacksonCodec implements JsonCodec {
3447
35- private static final Logger log = LoggerFactory .getLogger (io . vertx . core . json . jackson . JacksonCodec .class );
48+ private static final Logger log = LoggerFactory .getLogger (JacksonCodec .class );
3649
3750 private static JsonFactory buildFactory () {
3851 JsonFactoryBuilder tsfBuilder = JsonFactory .builder ();
@@ -102,38 +115,7 @@ private static JsonFactory buildFactory() {
102115 return tsfBuilder .build ();
103116 }
104117
105- private static final JsonFactory factory = buildFactory ();
106-
107- public JacksonCodec () {
108- }
109-
110- private static JsonParser createParser (Buffer buf ) {
111- return factory .createParser (ObjectReadContext .empty (), (InputStream ) new ByteBufInputStream (((BufferInternal )buf ).getByteBuf ()));
112- }
113-
114- private static JsonParser createParser (String str ) {
115- return factory .createParser (ObjectReadContext .empty (), str );
116- }
117-
118- private static ObjectWriteContext owc (boolean pretty ) {
119- if (pretty ) {
120- PrettyPrinter prettyPrinter = new DefaultPrettyPrinter ();
121- return new ObjectWriteContext .Base () {
122- @ Override
123- public PrettyPrinter getPrettyPrinter () { return prettyPrinter ; }
124- };
125- } else {
126- return ObjectWriteContext .empty ();
127- }
128- }
129-
130- private static JsonGenerator createGenerator (OutputStream out , boolean pretty ) {
131- return factory .createGenerator (owc (pretty ), out );
132- }
133-
134- private static JsonGenerator createGenerator (Writer out , boolean pretty ) {
135- return factory .createGenerator (owc (pretty ), out );
136- }
118+ static final JsonFactory factory = buildFactory ();
137119
138120 @ Override
139121 public <T > T fromString (String json , Class <T > clazz ) throws DecodeException {
@@ -182,12 +164,28 @@ public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
182164 }
183165 }
184166
167+ public static JsonParser createParser (String str ) {
168+ return factory .createParser (ObjectReadContext .empty (), str );
169+ }
170+
171+ public static JsonParser createParser (Buffer buf ) {
172+ return factory .createParser (ObjectReadContext .empty (), (InputStream ) new ByteBufInputStream (((BufferInternal )buf ).getByteBuf ()));
173+ }
174+
175+ private static JsonGenerator createGenerator (Writer out , boolean pretty ) {
176+ return factory .createGenerator (owc (pretty ), out );
177+ }
178+
179+ private static JsonGenerator createGenerator (OutputStream out , boolean pretty ) {
180+ return factory .createGenerator (owc (pretty ), out );
181+ }
182+
185183 public static <T > T fromParser (JsonParser parser , Class <T > type ) throws DecodeException {
186184 Object res ;
187185 JsonToken remaining ;
188186 try {
189187 parser .nextToken ();
190- res = parseAny (parser );
188+ res = parseValue (parser );
191189 remaining = parser .nextToken ();
192190 } catch (JacksonException | IOException e ) {
193191 throw new DecodeException (e .getMessage (), e );
@@ -200,7 +198,7 @@ public static <T> T fromParser(JsonParser parser, Class<T> type) throws DecodeEx
200198 return cast (res , type );
201199 }
202200
203- private static Object parseAny (JsonParser parser ) throws IOException , DecodeException {
201+ private static Object parseValue (JsonParser parser ) throws IOException , DecodeException {
204202 switch (parser .currentTokenId ()) {
205203 case JsonTokenId .ID_START_OBJECT :
206204 return parseObject (parser );
@@ -222,21 +220,34 @@ private static Object parseAny(JsonParser parser) throws IOException, DecodeExce
222220 }
223221 }
224222
225- private static Map <String , Object > parseObject (JsonParser parser ) throws IOException {
223+ /**
224+ * Parse a JSON object given the {@code parser}, the parser current token must be {@link JsonTokenId#ID_START_OBJECT}
225+ *
226+ * @param parser the parser
227+ * @return the parsed object
228+ */
229+ public static Map <String , Object > parseObject (JsonParser parser ) throws IOException {
230+ if (parser .currentTokenId () != JsonTokenId .ID_START_OBJECT ) {
231+ throw new DecodeException ("Expecting the current parser token to be the start of an object" );
232+ }
233+ return internalParseObject (parser );
234+ }
235+
236+ private static Map <String , Object > internalParseObject (JsonParser parser ) throws IOException {
226237 String key1 = parser .nextName ();
227238 if (key1 == null ) {
228239 return new LinkedHashMap <>(2 );
229240 }
230241 parser .nextToken ();
231- Object value1 = parseAny (parser );
242+ Object value1 = parseValue (parser );
232243 String key2 = parser .nextName ();
233244 if (key2 == null ) {
234245 LinkedHashMap <String , Object > obj = new LinkedHashMap <>(2 );
235246 obj .put (key1 , value1 );
236247 return obj ;
237248 }
238249 parser .nextToken ();
239- Object value2 = parseAny (parser );
250+ Object value2 = parseValue (parser );
240251 String key = parser .nextName ();
241252 if (key == null ) {
242253 LinkedHashMap <String , Object > obj = new LinkedHashMap <>(2 );
@@ -250,14 +261,27 @@ private static Map<String, Object> parseObject(JsonParser parser) throws IOExcep
250261 obj .put (key2 , value2 );
251262 do {
252263 parser .nextToken ();
253- Object value = parseAny (parser );
264+ Object value = parseValue (parser );
254265 obj .put (key , value );
255266 key = parser .nextName ();
256267 } while (key != null );
257268 return obj ;
258269 }
259270
260- private static List <Object > parseArray (JsonParser parser ) throws IOException {
271+ /**
272+ * Parse a JSON array given the {@code parser}, the parser current token must be {@link JsonTokenId#ID_START_ARRAY}
273+ *
274+ * @param parser the parser
275+ * @return the parsed array
276+ */
277+ public static List <Object > parseArray (JsonParser parser ) throws IOException {
278+ if (parser .currentTokenId () != JsonTokenId .ID_START_ARRAY ) {
279+ throw new DecodeException ("Expecting the current parser token to be the start of an array" );
280+ }
281+ return internalParseArray (parser );
282+ }
283+
284+ private static List <Object > internalParseArray (JsonParser parser ) throws IOException {
261285 List <Object > array = new ArrayList <>();
262286 while (true ) {
263287 parser .nextToken ();
@@ -267,7 +291,7 @@ private static List<Object> parseArray(JsonParser parser) throws IOException {
267291 } else if (tokenId == JsonTokenId .ID_END_ARRAY ) {
268292 return array ;
269293 }
270- Object value = parseAny (parser );
294+ Object value = parseValue (parser );
271295 array .add (value );
272296 }
273297 }
@@ -451,4 +475,16 @@ private static <T> T cast(Object o, Class<T> clazz) {
451475 return clazz .cast (o );
452476 }
453477 }
478+
479+ private static ObjectWriteContext owc (boolean pretty ) {
480+ if (pretty ) {
481+ PrettyPrinter prettyPrinter = new DefaultPrettyPrinter ();
482+ return new ObjectWriteContext .Base () {
483+ @ Override
484+ public PrettyPrinter getPrettyPrinter () { return prettyPrinter ; }
485+ };
486+ } else {
487+ return ObjectWriteContext .empty ();
488+ }
489+ }
454490}
0 commit comments