• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-wings / #125

31 Aug 2024 04:46AM UTC coverage: 63.579% (+0.7%) from 62.919%
#125

push

web-flow
Merge pull request #290 from trydofor/develop

3.2.130

1428 of 2191 new or added lines in 106 files covered. (65.18%)

41 existing lines in 24 files now uncovered.

12923 of 20326 relevant lines covered (63.58%)

0.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.75
/wings/slardar/src/main/java/pro/fessional/wings/slardar/jackson/JacksonHelper.java
1
package pro.fessional.wings.slardar.jackson;
2

3
import com.fasterxml.jackson.databind.JavaType;
4
import com.fasterxml.jackson.databind.JsonNode;
5
import com.fasterxml.jackson.databind.ObjectMapper;
6
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
7
import com.fasterxml.jackson.databind.json.JsonMapper;
8
import com.fasterxml.jackson.databind.module.SimpleModule;
9
import com.fasterxml.jackson.databind.type.TypeFactory;
10
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
11
import lombok.SneakyThrows;
12
import org.jetbrains.annotations.Contract;
13
import org.jetbrains.annotations.NotNull;
14
import org.springframework.core.ResolvableType;
15
import org.springframework.core.convert.TypeDescriptor;
16
import pro.fessional.mirana.text.WhiteUtil;
17
import pro.fessional.mirana.time.DateFormatter;
18
import pro.fessional.wings.silencer.datetime.DateTimePattern;
19
import pro.fessional.wings.silencer.support.TypeSugar;
20
import pro.fessional.wings.slardar.autozone.AutoZoneType;
21
import pro.fessional.wings.slardar.autozone.json.JacksonLocalDateTimeDeserializer;
22
import pro.fessional.wings.slardar.autozone.json.JacksonLocalDateTimeSerializer;
23
import pro.fessional.wings.slardar.autozone.json.JacksonOffsetDateTimeDeserializer;
24
import pro.fessional.wings.slardar.autozone.json.JacksonOffsetDateTimeSerializer;
25
import pro.fessional.wings.slardar.autozone.json.JacksonZonedDateTimeDeserializer;
26
import pro.fessional.wings.slardar.autozone.json.JacksonZonedDateTimeSerializer;
27

28
import java.lang.reflect.Type;
29
import java.time.LocalDateTime;
30
import java.time.OffsetDateTime;
31
import java.time.ZonedDateTime;
32
import java.time.format.DateTimeFormatter;
33
import java.util.List;
34

35
/**
36
 * <pre>
37
 * <a href="https://github.com/FasterXML/jackson-dataformat-xml#known-limitations">XML limitation</a>
38
 * The common uses are:
39
 * (1) single element node, XML can not distinguish between a single value or only one value in the array, unless nested wrap.
40
 * (2) Xml can not recognize the data type, while Json has string, number, boolean, object, array
41
 *
42
 * Jackson Plain
43
 * - `transient` output
44
 * - `@Transient` No output
45
 * - `byte[]` as base64, `[]` as `""`
46
 * - `char[]` as String, `[]` as `""`
47
 * - WRITE_DATES_AS_TIMESTAMPS = false
48
 * - `ZonedDateTime` parse as `2023-04-04T21:07:08Z` lost timezone
49
 * - `OffsetDateTime` parse as `2023-04-05T10:07:08Z` lost timezone
50
 * Jackson Wings
51
 * - `transient` No output
52
 * - WRITE_DATES_AS_TIMESTAMPS = false
53
 * - `LocalDateTime` as `"2023-04-05T06:07:08"`
54
 * - `ZonedDateTime` as `"2023-04-05T06:07:08[America/New_York]"` keep timezone
55
 * - `OffsetDateTime` as `"2023-04-05T06:07:08-04:00"` keep timezone
56
 * Jackson Bean
57
 * - `LocalDateTime` as `"2023-04-05 06:07:08"`
58
 * - `ZonedDateTime` as `"2023-04-05 06:07:08 Asia/Shanghai"`
59
 * - `OffsetDateTime` as `"2023-04-05 06:07:08 +08:00"`
60
 * - `float`,`double` as `"3.14159"`
61
 * - `BigDecimal`,`BigInteger` as `"299792458"`
62
 * </pre>
63
 *
64
 * @author trydofor
65
 * @since 2022-11-05
66
 */
67
public class JacksonHelper {
1✔
68

69
    public enum Style {
1✔
70
        /**
71
         * at web tier with ' ' datetime and auto timezone/i18n convert
72
         */
73
        Bean,
1✔
74
        /**
75
         * fastjon default with WRITE_DATES_AS_TIMESTAMPS=false and other Disable/Enable
76
         */
77
        Plain,
1✔
78
        /**
79
         * wings config with 'T' datetime format, without timezone/i18n conversion
80
         */
81
        Wings,
1✔
82
    }
83

84
    // spring-jackson-79.properties
85
    public static final com.fasterxml.jackson.databind.DeserializationFeature[] EnableDeserializationFeature = {
1✔
86
        com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS,
87
        com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES,
88
        com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
89
        com.fasterxml.jackson.databind.DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE
90
    };
91
    public static final com.fasterxml.jackson.core.JsonGenerator.Feature[] EnableJsonGeneratorFeature = {
1✔
92
        com.fasterxml.jackson.core.JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN
93
    };
94
    public static final com.fasterxml.jackson.databind.MapperFeature[] EnableMapperFeature = {
1✔
95
        com.fasterxml.jackson.databind.MapperFeature.PROPAGATE_TRANSIENT_MARKER,
96
        com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION,
97
        com.fasterxml.jackson.databind.MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING
98
    };
99
    public static final com.fasterxml.jackson.core.JsonParser.Feature[] EnableParserFeature = {
1✔
100
        com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_COMMENTS,
101
        com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_YAML_COMMENTS,
102
        com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,
103
        com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES,
104
        com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS,
105
        com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_MISSING_VALUES,
106
        com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_TRAILING_COMMA
107
    };
108
    public static final com.fasterxml.jackson.databind.SerializationFeature[] EnableSerializationFeature = {
1✔
109
        com.fasterxml.jackson.databind.SerializationFeature.CLOSE_CLOSEABLE
110
    };
111
    public static final com.fasterxml.jackson.core.JsonParser.Feature[] DisableParserFeature = {
1✔
112
        com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER
113
    };
114
    public static final com.fasterxml.jackson.databind.DeserializationFeature[] DisableDeserializationFeature = {
1✔
115
        com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
116
    };
117
    public static final com.fasterxml.jackson.databind.SerializationFeature[] DisableSerializationFeature = {
1✔
118
        com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
119
        com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS
120
    };
121

122
    public static final JacksonLocalDateTimeDeserializer PlainLocalDateTimeDeserializer = new JacksonLocalDateTimeDeserializer(
1✔
123
        DateTimeFormatter.ISO_LOCAL_DATE_TIME, List.of(DateFormatter.FMT_FULL_PSE), AutoZoneType.Off);
1✔
124

125
    public static final JacksonOffsetDateTimeDeserializer PlainOffsetDateTimeDeserializer = new JacksonOffsetDateTimeDeserializer(
1✔
126
        DateTimeFormatter.ISO_OFFSET_DATE_TIME, List.of(DateFormatter.FMT_ZONE_PSE), AutoZoneType.Off);
1✔
127

128
    public static final JacksonZonedDateTimeSerializer PlainZonedDateTimeSerializer = new JacksonZonedDateTimeSerializer(
1✔
129
        DateTimePattern.FMT_FULL_19TV, AutoZoneType.Off);
130

131
    public static final JacksonZonedDateTimeDeserializer PlainZonedDateTimeDeserializer = new JacksonZonedDateTimeDeserializer(
1✔
132
        DateTimePattern.FMT_FULL_19TV, List.of(DateFormatter.FMT_ZONE_PSE), AutoZoneType.Off);
1✔
133

134
    public static final SimpleModule PlainDateTimeModule = new SimpleModule() {{
1✔
135
        addDeserializer(LocalDateTime.class, PlainLocalDateTimeDeserializer);
1✔
136
        addDeserializer(OffsetDateTime.class, PlainOffsetDateTimeDeserializer);
1✔
137
        addSerializer(ZonedDateTime.class, PlainZonedDateTimeSerializer);
1✔
138
        addDeserializer(ZonedDateTime.class, PlainZonedDateTimeDeserializer);
1✔
139
    }};
1✔
140

141
    @Contract("_->param1")
142
    public static <T extends MapperBuilder<?, ?>> T buildPlain(@NotNull T builder) {
143
        builder
1✔
144
            .enable(EnableDeserializationFeature)
1✔
145
            .enable(EnableJsonGeneratorFeature)
1✔
146
            .enable(EnableMapperFeature)
1✔
147
            .enable(EnableParserFeature)
1✔
148
            .enable(EnableSerializationFeature)
1✔
149
            .disable(DisableParserFeature)
1✔
150
            .disable(DisableDeserializationFeature)
1✔
151
            .disable(DisableSerializationFeature)
1✔
152
            // override
153
            .addModule(PlainDateTimeModule);
1✔
154
        return builder;
1✔
155
    }
156

157
    // https://github.com/FasterXML/jackson-modules-java8/tree/master/datetime#usage
158
    public static final ObjectMapper JsonPlain = buildPlain(JsonMapper.builder().findAndAddModules()).build();
1✔
159
    public static final XmlMapper XmlPlain = buildPlain(XmlMapper.builder().findAndAddModules()).build();
1✔
160
    public static final TypeFactory TypeFactoryPlain = JsonPlain.getTypeFactory();
1✔
161

162
    //// wings autoZone ser/des
163
    protected static JacksonLocalDateTimeSerializer beanLocalDateTimeSerializer;
164
    protected static JacksonLocalDateTimeDeserializer beanLocalDateTimeDeserializer;
165
    protected static JacksonZonedDateTimeSerializer beanZonedDateTimeSerializer;
166
    protected static JacksonZonedDateTimeDeserializer beanZonedDateTimeDeserializer;
167
    protected static JacksonOffsetDateTimeSerializer beanOffsetDateTimeSerializer;
168
    protected static JacksonOffsetDateTimeDeserializer beanOffsetDateTimeDeserializer;
169

170
    @Contract("_->param1")
171
    public static <T extends ObjectMapper> T buildWings(@NotNull T mapper) {
172
        SimpleModule beanDateTimeModule = new SimpleModule();
1✔
173
        // auto off
174
        boolean hasBean = false;
1✔
175
        if (beanLocalDateTimeSerializer != null) {
1✔
176
            hasBean = true;
1✔
177
            beanDateTimeModule.addSerializer(LocalDateTime.class, beanLocalDateTimeSerializer.autoOff());
1✔
178
        }
179
        if (beanLocalDateTimeDeserializer != null) {
1✔
180
            hasBean = true;
1✔
181
            beanDateTimeModule.addDeserializer(LocalDateTime.class, beanLocalDateTimeDeserializer.autoOff());
1✔
182
        }
183
        if (beanZonedDateTimeSerializer != null) {
1✔
184
            hasBean = true;
1✔
185
            beanDateTimeModule.addSerializer(ZonedDateTime.class, beanZonedDateTimeSerializer.autoOff());
1✔
186
        }
187
        if (beanZonedDateTimeDeserializer != null) {
1✔
188
            hasBean = true;
1✔
189
            beanDateTimeModule.addDeserializer(ZonedDateTime.class, beanZonedDateTimeDeserializer.autoOff());
1✔
190
        }
191
        if (beanOffsetDateTimeSerializer != null) {
1✔
192
            hasBean = true;
1✔
193
            beanDateTimeModule.addSerializer(OffsetDateTime.class, beanOffsetDateTimeSerializer.autoOff());
1✔
194
        }
195
        if (beanOffsetDateTimeDeserializer != null) {
1✔
196
            hasBean = true;
1✔
197
            beanDateTimeModule.addDeserializer(OffsetDateTime.class, beanOffsetDateTimeDeserializer.autoOff());
1✔
198
        }
199

200
        SimpleModule dtm = hasBean ? beanDateTimeModule : PlainDateTimeModule;
1✔
201
        mapper.registerModule(dtm);
1✔
202
        return mapper;
1✔
203
    }
204

205
    private static ObjectMapper JsonWings = JsonPlain;
1✔
206
    private static XmlMapper XmlWings = XmlPlain;
1✔
207
    private static boolean wingsPrepared = false;
1✔
208

209
    protected static void prepareWings(@NotNull ObjectMapper json, @NotNull XmlMapper xml) {
210
        JsonWings = buildWings(json);
1✔
211
        XmlWings = buildWings(xml);
1✔
212
        wingsPrepared = true;
1✔
213
    }
1✔
214

215

216
    private static ObjectMapper JsonBean = null;
1✔
217
    private static XmlMapper XmlBean = null;
1✔
218
    private static boolean beanPrepared = false;
1✔
219

220
    protected static void prepareBean(@NotNull ObjectMapper json, @NotNull XmlMapper xml) {
221
        JsonBean = json;
1✔
222
        XmlBean = xml;
1✔
223
        beanPrepared = true;
1✔
224
    }
1✔
225

226
    public static boolean isPrepared(@NotNull Style style) {
NEW
227
        return switch (style) {
×
NEW
228
            case Plain -> true;
×
NEW
229
            case Wings -> wingsPrepared;
×
NEW
230
            case Bean -> beanPrepared;
×
231
        };
232
    }
233
    ////
234

235
    /**
236
     * spring bean style Mapper at web tier with ' ' datetime and auto timezone/i18n convert
237
     */
238
    @NotNull
239
    public static ObjectMapper JsonBean() {
240
        if (JsonBean == null) throw new IllegalStateException("JsonBean not init, check SlardarJacksonWebConfiguration.jacksonHelperRunner");
×
241
        return JsonBean;
×
242
    }
243

244
    /**
245
     * spring bean style Mapper at web tier with ' ' datetime and auto timezone/i18n convert
246
     */
247
    @NotNull
248
    public static XmlMapper XmlBean() {
249
        if (XmlBean == null) throw new IllegalStateException("XmlBean not init, check SlardarJacksonWebConfiguration.jacksonHelperRunner");
×
250
        return XmlBean;
×
251
    }
252

253
    /**
254
     * spring bean style Mapper at web tier with ' ' datetime and auto timezone/i18n convert
255
     */
256
    @NotNull
257
    public static ObjectMapper MapperBean(boolean json) {
258
        return json ? JsonBean() : XmlBean();
×
259
    }
260

261
    /**
262
     * fastjon default with WRITE_DATES_AS_TIMESTAMPS=false and other Disable/Enable
263
     */
264
    @NotNull
265
    public static ObjectMapper JsonPlain() {
266
        return JsonPlain;
×
267
    }
268

269
    /**
270
     * fastjon default with WRITE_DATES_AS_TIMESTAMPS=false and other Disable/Enable
271
     */
272
    @NotNull
273
    public static XmlMapper XmlPlain() {
274
        return XmlPlain;
×
275
    }
276

277
    /**
278
     * fastjon default with WRITE_DATES_AS_TIMESTAMPS=false and other Disable/Enable
279
     */
280
    @NotNull
281
    public static ObjectMapper MapperPlain(boolean json) {
282
        return json ? JsonPlain : XmlPlain;
1✔
283
    }
284

285
    /**
286
     * wings config with 'T' datetime format without timezone/i18n conversion
287
     */
288
    @NotNull
289
    public static ObjectMapper JsonWings() {
290
        return JsonWings;
×
291
    }
292

293
    /**
294
     * wings config with 'T' datetime format without timezone/i18n conversion
295
     */
296
    @NotNull
297
    public static XmlMapper XmlWings() {
298
        return XmlWings;
×
299
    }
300

301
    /**
302
     * wings config with 'T' datetime format without timezone/i18n conversion
303
     */
304
    @NotNull
305
    public static ObjectMapper MapperWings(boolean json) {
306
        return json ? JsonWings : XmlWings;
1✔
307
    }
308

309
    /**
310
     * wings configed Mapper without auto timezone convert
311
     */
312
    @NotNull
313
    public static ObjectMapper Mapper(@NotNull Style style, boolean json) {
314
        return switch (style) {
1✔
315
            case Wings -> MapperWings(json);
×
316
            case Plain -> MapperPlain(json);
1✔
317
            case Bean -> MapperBean(json);
×
318
        };
319
    }
320

321
    /**
322
     * whether `str` has xml characteristics, i.e. the first and last characters are angle brackets or not
323
     */
324
    @Contract("null->false")
325
    public static boolean asXml(String str) {
326
        if (str == null) return false;
1✔
327

328
        char c1 = WhiteUtil.firstNonWhite(str);
1✔
329
        if (c1 == '<') {
1✔
330
            char c2 = WhiteUtil.lastNonWhite(str);
×
331
            return c2 == '>';
×
332
        }
333
        return false;
1✔
334
    }
335

336
    /**
337
     * whether `str` has xml characteristics, i.e. the first and last characters are angle brackets or not
338
     */
339
    @Contract("null->false")
340
    public static boolean asXml(byte[] str) {
341
        if (str == null) return false;
×
342

343
        byte c1 = WhiteUtil.firstNonWhite(str);
×
344
        if (c1 == (byte) '<') {
×
345
            byte c2 = WhiteUtil.lastNonWhite(str);
×
346
            return c2 == (byte) '>';
×
347
        }
348
        return false;
×
349
    }
350

351
    /////////////
352

353
    /**
354
     * construct jackson's JavaType by TypeSugar
355
     */
356
    public static JavaType javaType(@NotNull Class<?> targetType, Class<?>... generics) {
357
        Type type = TypeSugar.type(targetType, generics);
1✔
358
        return TypeFactoryPlain.constructType(type);
1✔
359
    }
360

361
    /**
362
     * construct jackson's JavaType in spring way
363
     */
364
    public static JavaType javaType(@NotNull TypeDescriptor targetType) {
365
        Type type = targetType.getResolvableType().getType();
×
366
        return TypeFactoryPlain.constructType(type);
×
367
    }
368

369
    /**
370
     * construct jackson's JavaType in spring way
371
     */
372
    public static JavaType javaType(@NotNull ResolvableType targetType) {
373
        return TypeFactoryPlain.constructType(targetType.getType());
×
374
    }
375

376
    /**
377
     * wings style read text to object, if text asXml, read as xml, otherwise as json
378
     */
379
    @SneakyThrows
×
380
    @Contract("!null,_,_->!null")
381
    public static <T> T object(String text, @NotNull Class<?> targetType, Class<?>... generics) {
382
        if (text == null) return null;
1✔
383
        return MapperWings(!asXml(text)).readValue(text, javaType(targetType, generics));
1✔
384
    }
385

386

387
    /**
388
     * Auto read text to object, if text asXml, read as xml, otherwise as json
389
     */
390
    @SneakyThrows
×
391
    @Contract("_,!null,_,_ -> !null")
392
    public static <T> T object(@NotNull Style style, String text, @NotNull Class<?> targetType, Class<?>... generics) {
393
        if (text == null) return null;
1✔
394
        return Mapper(style, !asXml(text)).readValue(text, javaType(targetType, generics));
1✔
395
    }
396

397
    /**
398
     * wings style read text to object, if text asXml, read as xml, otherwise as json
399
     */
400
    @SneakyThrows
×
401
    @Contract("!null,_->!null")
402
    public static <T> T object(String text, @NotNull JavaType targetType) {
403
        if (text == null) return null;
×
404
        return MapperWings(!asXml(text)).readValue(text, targetType);
×
405
    }
406

407
    /**
408
     * Auto read text to object, if text asXml, read as xml, otherwise as json
409
     */
410
    @SneakyThrows
×
411
    @Contract("_,!null,_ -> !null")
412
    public static <T> T object(@NotNull Style style, String text, @NotNull JavaType targetType) {
413
        if (text == null) return null;
×
414
        return Mapper(style, !asXml(text)).readValue(text, targetType);
×
415
    }
416

417

418
    /**
419
     * wings style read text to object, if text asXml, read as xml, otherwise as json
420
     */
421
    @SneakyThrows
×
422
    @Contract("!null,_->!null")
423
    public static <T> T object(String text, @NotNull ResolvableType targetType) {
424
        if (text == null) return null;
×
425
        return MapperWings(!asXml(text)).readValue(text, javaType(targetType));
×
426
    }
427

428
    /**
429
     * Auto read text to object, if text asXml, read as xml, otherwise as json
430
     */
431
    @SneakyThrows
×
432
    @Contract("_,!null,_ -> !null")
433
    public static <T> T object(@NotNull Style style, String text, @NotNull ResolvableType targetType) {
434
        if (text == null) return null;
×
435
        return Mapper(style, !asXml(text)).readValue(text, javaType(targetType));
×
436
    }
437

438

439
    /**
440
     * wings style read text to object, if text asXml, read as xml, otherwise as json
441
     */
442
    @SneakyThrows
×
443
    @Contract("!null,_->!null")
444
    public static <T> T object(String text, @NotNull TypeDescriptor targetType) {
445
        if (text == null) return null;
×
446
        return MapperWings(!asXml(text)).readValue(text, javaType(targetType));
×
447
    }
448

449
    /**
450
     * Auto read text to object, if text asXml, read as xml, otherwise as json
451
     */
452
    @SneakyThrows
×
453
    @Contract("_,!null,_ -> !null")
454
    public static <T> T object(@NotNull Style style, String text, @NotNull TypeDescriptor targetType) {
455
        if (text == null) return null;
×
456
        return Mapper(style, !asXml(text)).readValue(text, javaType(targetType));
×
457
    }
458

459

460
    /**
461
     * wings style read text to object, if text asXml, read as xml, otherwise as json
462
     */
463
    @SneakyThrows
×
464
    @Contract("!null->!null")
465
    public static JsonNode object(String text) {
466
        if (text == null) return null;
1✔
467
        return MapperWings(!asXml(text)).readTree(text);
1✔
468
    }
469

470
    /**
471
     * wings style read text to object, if text asXml, read as xml, otherwise as json
472
     */
473
    @SneakyThrows
×
474
    @Contract("!null,_,_->!null")
475
    public static <T> T object(byte[] text, @NotNull Class<?> targetType, Class<?>... generics) {
476
        if (text == null) return null;
×
477
        return MapperWings(!asXml(text)).readValue(text, javaType(targetType, generics));
×
478
    }
479

480

481
    /**
482
     * Auto read text to object, if text asXml, read as xml, otherwise as json
483
     */
484
    @SneakyThrows
×
485
    @Contract("_,!null,_,_ -> !null")
486
    public static <T> T object(@NotNull Style style, byte[] text, @NotNull Class<?> targetType, Class<?>... generics) {
487
        if (text == null) return null;
×
488
        return Mapper(style, !asXml(text)).readValue(text, javaType(targetType, generics));
×
489
    }
490

491
    /**
492
     * wings style read text to object, if text asXml, read as xml, otherwise as json
493
     */
494
    @SneakyThrows
×
495
    @Contract("!null,_->!null")
496
    public static <T> T object(byte[] text, @NotNull JavaType targetType) {
497
        if (text == null) return null;
×
498
        return MapperWings(!asXml(text)).readValue(text, targetType);
×
499
    }
500

501
    /**
502
     * Auto read text to object, if text asXml, read as xml, otherwise as json
503
     */
504
    @SneakyThrows
×
505
    @Contract("_, !null, _ -> !null")
506
    public static <T> T object(@NotNull Style style, byte[] text, @NotNull JavaType targetType) {
507
        if (text == null) return null;
×
508
        return Mapper(style, !asXml(text)).readValue(text, targetType);
×
509
    }
510

511
    /**
512
     * wings style read text to object, if text asXml, read as xml, otherwise as json
513
     */
514
    @SneakyThrows
×
515
    @Contract("!null,_->!null")
516
    public static <T> T object(byte[] text, @NotNull ResolvableType targetType) {
517
        if (text == null) return null;
×
518
        return MapperWings(!asXml(text)).readValue(text, javaType(targetType));
×
519
    }
520

521
    /**
522
     * Auto read text to object, if text asXml, read as xml, otherwise as json
523
     */
524
    @SneakyThrows
×
525
    @Contract("_, !null, _ -> !null")
526
    public static <T> T object(@NotNull Style style, byte[] text, @NotNull ResolvableType targetType) {
527
        if (text == null) return null;
×
528
        return Mapper(style, !asXml(text)).readValue(text, javaType(targetType));
×
529
    }
530

531
    /**
532
     * wings style read text to object, if text asXml, read as xml, otherwise as json
533
     */
534
    @SneakyThrows
×
535
    @Contract("!null,_->!null")
536
    public static <T> T object(byte[] text, @NotNull TypeDescriptor targetType) {
537
        if (text == null) return null;
×
538
        return MapperWings(!asXml(text)).readValue(text, javaType(targetType));
×
539
    }
540

541
    /**
542
     * Auto read text to object, if text asXml, read as xml, otherwise as json
543
     */
544
    @SneakyThrows
×
545
    @Contract("_, !null, _ -> !null")
546
    public static <T> T object(@NotNull Style style, byte[] text, @NotNull TypeDescriptor targetType) {
547
        if (text == null) return null;
×
548
        return Mapper(style, !asXml(text)).readValue(text, javaType(targetType));
×
549
    }
550

551
    /**
552
     * wings style read text to object, if text asXml, read as xml, otherwise as json
553
     */
554
    @SneakyThrows
×
555
    @Contract("!null->!null")
556
    public static JsonNode object(byte[] text) {
557
        if (text == null) return null;
×
558
        return MapperWings(!asXml(text)).readTree(text);
×
559
    }
560

561
    /**
562
     * Auto read text to object, if text asXml, read as xml, otherwise as json
563
     */
564
    @SneakyThrows
×
565
    @Contract("_, !null -> !null")
566
    public static JsonNode object(@NotNull Style style, byte[] text) {
567
        if (text == null) return null;
×
568
        return Mapper(style, !asXml(text)).readTree(text);
×
569
    }
570

571
    /**
572
     * wings style serialization to json
573
     */
574
    @SneakyThrows
×
575
    @Contract("!null->!null")
576
    public static String string(Object obj) {
577
        return obj == null ? null : MapperWings(true).writeValueAsString(obj);
1✔
578
    }
579

580
    /**
581
     * serialization to json
582
     */
583
    @SneakyThrows
×
584
    @Contract("_, !null -> !null")
585
    public static String string(@NotNull Style style, Object obj) {
586
        return obj == null ? null : Mapper(style, true).writeValueAsString(obj);
1✔
587
    }
588

589
    /**
590
     * wings style serialization to json/xml
591
     */
592
    @SneakyThrows
×
593
    @Contract("!null,_->!null")
594
    public static String string(Object obj, boolean json) {
595
        return obj == null ? null : MapperWings(json).writeValueAsString(obj);
1✔
596
    }
597

598
    /**
599
     * serialization to json/xml
600
     */
601
    @SneakyThrows
×
602
    @Contract("_, !null, _ -> !null")
603
    public static String string(@NotNull Style style, Object obj, boolean json) {
604
        return obj == null ? null : Mapper(style, json).writeValueAsString(obj);
×
605
    }
606

607
    /**
608
     * wings style serialization to json
609
     */
610
    @SneakyThrows
×
611
    @Contract("!null->!null")
612
    public static byte[] bytes(Object obj) {
613
        return obj == null ? null : MapperWings(true).writeValueAsBytes(obj);
×
614
    }
615

616
    /**
617
     * serialization to json
618
     */
619
    @SneakyThrows
×
620
    @Contract("_, !null -> !null")
621
    public static byte[] bytes(@NotNull Style style, Object obj) {
622
        return obj == null ? null : Mapper(style, true).writeValueAsBytes(obj);
×
623
    }
624

625
    /**
626
     * wings style serialization to json/xml
627
     */
628
    @SneakyThrows
×
629
    @Contract("!null,_->!null")
630
    public static byte[] bytes(Object obj, boolean json) {
631
        return obj == null ? null : MapperWings(json).writeValueAsBytes(obj);
×
632
    }
633

634
    /**
635
     * serialization to json/xml
636
     */
637
    @SneakyThrows
×
638
    @Contract("_, !null, _ -> !null")
639
    public static byte[] bytes(@NotNull Style style, Object obj, boolean json) {
640
        return obj == null ? null : Mapper(style, json).writeValueAsBytes(obj);
×
641
    }
642

643
    ////
644

645
    @Contract("_,_,!null->!null")
646
    public static String getString(JsonNode node, String field, String defaults) {
647
        if (node == null) return defaults;
1✔
648
        final JsonNode jn = node.get(field);
1✔
649
        return jn != null ? jn.asText(defaults) : defaults;
1✔
650
    }
651

652
    public static boolean getBoolean(JsonNode node, String field, boolean defaults) {
653
        if (node == null) return defaults;
×
654
        final JsonNode jn = node.get(field);
×
655
        return jn != null ? jn.asBoolean(defaults) : defaults;
×
656
    }
657

658
    public static int getInt(JsonNode node, String field, int defaults) {
659
        if (node == null) return defaults;
1✔
660
        final JsonNode jn = node.get(field);
1✔
661
        return jn != null ? jn.asInt(defaults) : defaults;
1✔
662
    }
663

664
    public static long getLong(JsonNode node, String field, long defaults) {
665
        if (node == null) return defaults;
×
666
        final JsonNode jn = node.get(field);
×
667
        return jn != null ? jn.asLong(defaults) : defaults;
×
668
    }
669

670
    public static double getDouble(JsonNode node, String field, double defaults) {
671
        if (node == null) return defaults;
×
672
        final JsonNode jn = node.get(field);
×
673
        return jn != null ? jn.asDouble(defaults) : defaults;
×
674
    }
675
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc