@@ -57,54 +57,64 @@ private <T> T deserialize(final Type type, final JsonParser parser, final Deseri
57
57
58
58
@ Override
59
59
public <T > T fromJson (String str , Class <T > type ) throws JsonbException {
60
- final JsonParser parser = jsonbContext .getJsonProvider ().createParser (new StringReader (str ));
61
- final DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
62
- return deserialize (type , parser , unmarshaller );
60
+ try (JsonParser parser = jsonbContext .getJsonProvider ().createParser (new StringReader (str ))) {
61
+ final DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
62
+ return deserialize (type , parser , unmarshaller );
63
+ }
63
64
}
64
65
65
66
@ Override
66
67
public <T > T fromJson (String str , Type type ) throws JsonbException {
67
- JsonParser parser = jsonbContext .getJsonProvider ().createParser (new StringReader (str ));
68
- DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
69
- return deserialize (type , parser , unmarshaller );
68
+ try (JsonParser parser = jsonbContext .getJsonProvider ().createParser (new StringReader (str ))) {
69
+ DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
70
+ return deserialize (type , parser , unmarshaller );
71
+ }
70
72
}
71
73
72
74
@ Override
73
75
public <T > T fromJson (Reader reader , Class <T > type ) throws JsonbException {
74
- JsonParser parser = jsonbContext .getJsonProvider ().createParser (reader );
75
- DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
76
- return deserialize (type , parser , unmarshaller );
76
+ try (JsonParser parser = jsonbContext .getJsonProvider ().createParser (reader )) {
77
+ DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
78
+ return deserialize (type , parser , unmarshaller );
79
+ }
77
80
}
78
81
79
82
@ Override
80
83
public <T > T fromJson (Reader reader , Type type ) throws JsonbException {
81
- JsonParser parser = jsonbContext .getJsonProvider ().createParser (reader );
82
- DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
83
- return deserialize (type , parser , unmarshaller );
84
+ try (JsonParser parser = jsonbContext .getJsonProvider ().createParser (reader )) {
85
+ DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
86
+ return deserialize (type , parser , unmarshaller );
87
+ }
84
88
}
85
89
86
90
@ Override
87
91
public <T > T fromJson (InputStream stream , Class <T > clazz ) throws JsonbException {
88
92
DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
89
- return deserialize (clazz , inputStreamParser (stream ), unmarshaller );
93
+ try (JsonParser parser = inputStreamParser (stream )) {
94
+ return deserialize (clazz , inputStreamParser (stream ), unmarshaller );
95
+ }
90
96
}
91
97
92
98
@ Override
93
99
public <T > T fromJson (InputStream stream , Type type ) throws JsonbException {
94
100
DeserializationContextImpl unmarshaller = new DeserializationContextImpl (jsonbContext );
95
- return deserialize (type , inputStreamParser (stream ), unmarshaller );
101
+ try (JsonParser parser = inputStreamParser (stream )) {
102
+ return deserialize (type , inputStreamParser (stream ), unmarshaller );
103
+ }
96
104
}
97
105
98
106
@ Override
99
107
public <T > T fromJsonStructure (JsonStructure jsonStructure , Class <T > type ) throws JsonbException {
100
- JsonParser parser = new JsonStructureToParserAdapter (jsonStructure );
101
- return deserialize (type , parser , new DeserializationContextImpl (jsonbContext ));
108
+ try (JsonParser parser = new JsonStructureToParserAdapter (jsonStructure )) {
109
+ return deserialize (type , parser , new DeserializationContextImpl (jsonbContext ));
110
+ }
102
111
}
103
112
104
113
@ Override
105
114
public <T > T fromJsonStructure (JsonStructure jsonStructure , Type runtimeType ) throws JsonbException {
106
- JsonParser parser = new JsonStructureToParserAdapter (jsonStructure );
107
- return deserialize (runtimeType , parser , new DeserializationContextImpl (jsonbContext ));
115
+ try (JsonParser parser = new JsonStructureToParserAdapter (jsonStructure )) {
116
+ return deserialize (runtimeType , parser , new DeserializationContextImpl (jsonbContext ));
117
+ }
108
118
}
109
119
110
120
private JsonParser inputStreamParser (InputStream stream ) {
@@ -117,29 +127,35 @@ private JsonParser inputStreamParser(InputStream stream) {
117
127
@ Override
118
128
public String toJson (Object object ) throws JsonbException {
119
129
StringWriter writer = new StringWriter ();
120
- final JsonGenerator generator = writerGenerator (writer );
121
- new SerializationContextImpl (jsonbContext ).marshall (object , generator );
130
+ try (JsonGenerator generator = writerGenerator (writer )) {
131
+ new SerializationContextImpl (jsonbContext ).marshall (object , generator );
132
+ }
122
133
return writer .toString ();
123
134
}
124
135
125
136
@ Override
126
137
public String toJson (Object object , Type type ) throws JsonbException {
127
138
StringWriter writer = new StringWriter ();
128
- final JsonGenerator generator = writerGenerator (writer );
129
- new SerializationContextImpl (jsonbContext , type ).marshall (object , generator );
139
+ try (JsonGenerator generator = writerGenerator (writer )) {
140
+ new SerializationContextImpl (jsonbContext , type ).marshall (object , generator );
141
+ }
130
142
return writer .toString ();
131
143
}
132
144
133
145
@ Override
134
146
public void toJson (Object object , Writer writer ) throws JsonbException {
135
147
final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext );
136
- marshaller .marshallWithoutClose (object , writerGenerator (writer ));
148
+ try (JsonGenerator generator = writerGenerator (writer )) {
149
+ marshaller .marshallWithoutClose (object , generator );
150
+ }
137
151
}
138
152
139
153
@ Override
140
154
public void toJson (Object object , Type type , Writer writer ) throws JsonbException {
141
155
final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext , type );
142
- marshaller .marshallWithoutClose (object , writerGenerator (writer ));
156
+ try (JsonGenerator generator = writerGenerator (writer )) {
157
+ marshaller .marshallWithoutClose (object , generator );
158
+ }
143
159
}
144
160
145
161
private JsonGenerator writerGenerator (Writer writer ) {
@@ -153,13 +169,17 @@ private JsonGenerator writerGenerator(Writer writer) {
153
169
@ Override
154
170
public void toJson (Object object , OutputStream stream ) throws JsonbException {
155
171
final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext );
156
- marshaller .marshall (object , streamGenerator (stream ));
172
+ try (JsonGenerator generator = streamGenerator (stream )) {
173
+ marshaller .marshall (object , generator );
174
+ }
157
175
}
158
176
159
177
@ Override
160
178
public void toJson (Object object , Type type , OutputStream stream ) throws JsonbException {
161
179
final SerializationContextImpl marshaller = new SerializationContextImpl (jsonbContext , type );
162
- marshaller .marshall (object , streamGenerator (stream ));
180
+ try (JsonGenerator generator = streamGenerator (stream )) {
181
+ marshaller .marshall (object , generator );
182
+ }
163
183
}
164
184
165
185
@ Override
0 commit comments