1717 */
1818package org .apache .polygene .serialization .javaxjson ;
1919
20- import java .io .BufferedReader ;
21- import java .io .IOException ;
2220import java .io .Reader ;
23- import java .io .StringReader ;
24- import java .io .UncheckedIOException ;
2521import java .lang .reflect .Array ;
2622import java .util .AbstractMap ;
2723import java .util .ArrayList ;
3127import java .util .LinkedHashSet ;
3228import java .util .List ;
3329import java .util .Map ;
30+ import java .util .Scanner ;
3431import java .util .Set ;
3532import java .util .function .Function ;
3633import java .util .stream .Stream ;
3734import javax .json .JsonArray ;
3835import javax .json .JsonObject ;
3936import javax .json .JsonReader ;
40- import javax .json .JsonString ;
4137import javax .json .JsonStructure ;
4238import javax .json .JsonValue ;
43- import javax .json .stream .JsonParser ;
44- import javax .json .stream .JsonParsingException ;
4539import org .apache .polygene .api .association .AssociationDescriptor ;
4640import org .apache .polygene .api .composite .CompositeDescriptor ;
4741import org .apache .polygene .api .composite .StatefulAssociationCompositeDescriptor ;
6862import static java .util .Collections .unmodifiableList ;
6963import static java .util .Collections .unmodifiableMap ;
7064import static java .util .Collections .unmodifiableSet ;
71- import static java .util .stream .Collectors .joining ;
7265import static java .util .stream .Collectors .toCollection ;
7366import static org .apache .polygene .api .util .Collectors .toMapWithNullValues ;
7467import static org .apache .polygene .serialization .javaxjson .JavaxJson .asString ;
@@ -92,88 +85,35 @@ public class JavaxJsonDeserializer extends AbstractTextDeserializer
9285 private ServiceDescriptor descriptor ;
9386
9487 private JavaxJsonSettings settings ;
95- private JsonString emptyJsonString ;
9688
9789 @ Override
9890 public void initialize () throws Exception
9991 {
10092 settings = JavaxJsonSettings .orDefault ( descriptor .metaInfo ( JavaxJsonSettings .class ) );
101- emptyJsonString = jsonFactories .builderFactory ().createObjectBuilder ().add ( "s" , "" ).build ()
102- .getJsonString ( "s" );
10393 }
10494
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 )
10797 {
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 )
137100 {
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 );
169106 }
170107 }
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 ());
174116 }
175- // Empty state string?
176- return fromJson ( module , valueType , emptyJsonString );
177117 }
178118
179119 @ Override
@@ -182,6 +122,24 @@ public <T> T fromJson( ModuleDescriptor module, ValueType valueType, JsonValue s
182122 return doDeserialize ( module , valueType , state );
183123 }
184124
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+
185143 @ SuppressWarnings ( "unchecked" )
186144 private <T > T doDeserialize ( ModuleDescriptor module , ValueType valueType , JsonValue json )
187145 {
0 commit comments