-
Notifications
You must be signed in to change notification settings - Fork 94
/
JsonBindingTest.java
65 lines (52 loc) · 2.26 KB
/
JsonBindingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright (c) 2019, 2020 IBM and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.yasson.internal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.lang.reflect.Field;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import org.eclipse.yasson.YassonConfig;
import org.eclipse.yasson.internal.model.ClassModel;
import org.junit.jupiter.api.Test;
public class JsonBindingTest {
public static class EagerParseClass {
public String foo;
}
@Test
public void testEagerInit() throws Exception {
Jsonb jsonb = JsonbBuilder.create(new YassonConfig()
.withEagerParsing(EagerParseClass.class));
assertNotNull(getClassModel(jsonb, EagerParseClass.class));
EagerParseClass obj = new EagerParseClass();
obj.foo = "foo";
assertEquals("{\"foo\":\"foo\"}", jsonb.toJson(obj));
}
@Test
public void testNoEagerInit() throws Exception {
Jsonb jsonb = JsonbBuilder.create();
assertNull(getClassModel(jsonb, EagerParseClass.class));
EagerParseClass obj = new EagerParseClass();
obj.foo = "foo";
assertEquals("{\"foo\":\"foo\"}", jsonb.toJson(obj));
assertNotNull(getClassModel(jsonb, EagerParseClass.class));
}
private ClassModel getClassModel(Jsonb jsonb, Class<?> clazz) throws Exception {
// Do some hacks to ensure that the class had a ClassModel registered
JsonBinding yasson = (JsonBinding) jsonb;
Field jsonbContext = yasson.getClass().getDeclaredField("jsonbContext");
jsonbContext.setAccessible(true);
JsonbContext ctx = (JsonbContext) jsonbContext.get(yasson);
return ctx.getMappingContext().getClassModel(clazz);
}
}