17
17
*/
18
18
package org .apache .polygene .serialization .javaxjson ;
19
19
20
- import java .io .BufferedReader ;
21
- import java .io .IOException ;
22
20
import java .io .Reader ;
23
- import java .io .StringReader ;
24
- import java .io .UncheckedIOException ;
25
21
import java .lang .reflect .Array ;
26
22
import java .util .AbstractMap ;
27
23
import java .util .ArrayList ;
31
27
import java .util .LinkedHashSet ;
32
28
import java .util .List ;
33
29
import java .util .Map ;
30
+ import java .util .Scanner ;
34
31
import java .util .Set ;
35
32
import java .util .function .Function ;
36
33
import java .util .stream .Stream ;
37
34
import javax .json .JsonArray ;
38
35
import javax .json .JsonObject ;
39
36
import javax .json .JsonReader ;
40
- import javax .json .JsonString ;
41
37
import javax .json .JsonStructure ;
42
38
import javax .json .JsonValue ;
43
- import javax .json .stream .JsonParser ;
44
- import javax .json .stream .JsonParsingException ;
45
39
import org .apache .polygene .api .association .AssociationDescriptor ;
46
40
import org .apache .polygene .api .composite .CompositeDescriptor ;
47
41
import org .apache .polygene .api .composite .StatefulAssociationCompositeDescriptor ;
68
62
import static java .util .Collections .unmodifiableList ;
69
63
import static java .util .Collections .unmodifiableMap ;
70
64
import static java .util .Collections .unmodifiableSet ;
71
- import static java .util .stream .Collectors .joining ;
72
65
import static java .util .stream .Collectors .toCollection ;
73
66
import static org .apache .polygene .api .util .Collectors .toMapWithNullValues ;
74
67
import static org .apache .polygene .serialization .javaxjson .JavaxJson .asString ;
@@ -92,88 +85,35 @@ public class JavaxJsonDeserializer extends AbstractTextDeserializer
92
85
private ServiceDescriptor descriptor ;
93
86
94
87
private JavaxJsonSettings settings ;
95
- private JsonString emptyJsonString ;
96
88
97
89
@ Override
98
90
public void initialize () throws Exception
99
91
{
100
92
settings = JavaxJsonSettings .orDefault ( descriptor .metaInfo ( JavaxJsonSettings .class ) );
101
- emptyJsonString = jsonFactories .builderFactory ().createObjectBuilder ().add ( "s" , "" ).build ()
102
- .getJsonString ( "s" );
103
93
}
104
94
105
- @ Override
106
- public <T > T deserialize ( ModuleDescriptor module , ValueType valueType , Reader state )
95
+ @ SuppressWarnings ( "unchecked" )
96
+ public <T > T deserialize (ModuleDescriptor module , ValueType valueType , Reader state )
107
97
{
108
- // JSR-353 Does not allow reading "out of structure" values
109
- // See https://www.jcp.org/en/jsr/detail?id=353
110
- // And commented JsonReader#readValue() method in the javax.json API
111
- // BUT, it will be part of the JsonReader contract in the next version
112
- // See https://www.jcp.org/en/jsr/detail?id=374
113
- // Implementation by provider is optional though, so we'll always need a default implementation here.
114
- // Fortunately, JsonParser has new methods allowing to read structures while parsing so it will be easy to do.
115
- // In the meantime, a poor man's implementation reading the json into memory will do.
116
- // TODO Revisit values out of structure JSON deserialization when JSR-374 is out
117
- String stateString ;
118
- try ( BufferedReader buffer = new BufferedReader ( state ) )
119
- {
120
- stateString = buffer .lines ().collect ( joining ( "\n " ) );
121
- }
122
- catch ( IOException ex )
123
- {
124
- throw new UncheckedIOException ( ex );
125
- }
126
- // We want plain Strings, BigDecimals, BigIntegers to be deserialized even when unquoted
127
- Function <String , T > plainValueFunction = string ->
128
- {
129
- String poorMans = "{\" value\" :" + string + "}" ;
130
- JsonObject poorMansJson = jsonFactories .readerFactory ()
131
- .createReader ( new StringReader ( poorMans ) )
132
- .readObject ();
133
- JsonValue value = poorMansJson .get ( "value" );
134
- return fromJson ( module , valueType , value );
135
- };
136
- Function <String , T > outOfStructureFunction = string ->
98
+ Converter <Object > converter = converters .converterFor (valueType );
99
+ if (converter != null )
137
100
{
138
- // Is this an unquoted plain value?
139
- try
140
- {
141
- return plainValueFunction .apply ( '"' + string + '"' );
142
- }
143
- catch ( JsonParsingException ex )
144
- {
145
- return plainValueFunction .apply ( string );
146
- }
147
- };
148
- try ( JsonParser parser = jsonFactories .parserFactory ().createParser ( new StringReader ( stateString ) ) )
149
- {
150
- if ( parser .hasNext () )
151
- {
152
- JsonParser .Event e = parser .next ();
153
- switch ( e )
154
- {
155
- case VALUE_NULL :
156
- return null ;
157
- case START_ARRAY :
158
- case START_OBJECT :
159
- // JSON Structure
160
- try ( JsonReader reader = jsonFactories .readerFactory ()
161
- .createReader ( new StringReader ( stateString ) ) )
162
- {
163
- return fromJson ( module , valueType , reader .read () );
164
- }
165
- default :
166
- // JSON Value out of structure
167
- return outOfStructureFunction .apply ( stateString );
168
- }
101
+ String stateString = readString (state );
102
+ if (isJsonNull (stateString )) {
103
+ return null ;
104
+ } else {
105
+ return (T ) converter .fromString (stateString );
169
106
}
170
107
}
171
- catch ( JsonParsingException ex )
172
- {
173
- return outOfStructureFunction .apply ( stateString );
108
+
109
+ JavaxJsonAdapter <?> adapter = adapters .adapterFor (valueType );
110
+ if (adapter != null ) {
111
+ return (T ) adapter .deserialize (readJsonString (state ), (jsonValue , type ) -> doDeserialize (module , type , jsonValue ));
112
+ }
113
+
114
+ try (JsonReader reader = jsonFactories .readerFactory ().createReader (state )) {
115
+ return fromJson (module , valueType , reader .readValue ());
174
116
}
175
- // Empty state string?
176
- return fromJson ( module , valueType , emptyJsonString );
177
117
}
178
118
179
119
@ Override
@@ -182,6 +122,24 @@ public <T> T fromJson( ModuleDescriptor module, ValueType valueType, JsonValue s
182
122
return doDeserialize ( module , valueType , state );
183
123
}
184
124
125
+ private JsonValue readJsonString (Reader reader ) {
126
+ String str = readString (reader );
127
+ if (isJsonNull (str )) {
128
+ return JsonValue .NULL ;
129
+ } else {
130
+ return jsonFactories .provider ().createValue (str );
131
+ }
132
+ }
133
+
134
+ private boolean isJsonNull (String str ) {
135
+ return "null" .equals (str );
136
+ }
137
+
138
+ private String readString (Reader reader ) {
139
+ Scanner scanner = new Scanner (reader ).useDelimiter ("\\ A" );
140
+ return scanner .hasNext () ? scanner .next () : "" ;
141
+ }
142
+
185
143
@ SuppressWarnings ( "unchecked" )
186
144
private <T > T doDeserialize ( ModuleDescriptor module , ValueType valueType , JsonValue json )
187
145
{
0 commit comments