1+ // ------------------------------------------------------------------------------
2+ // Copyright (c) 2017 Microsoft Corporation
3+ //
4+ // Permission is hereby granted, free of charge, to any person obtaining a copy
5+ // of this software and associated documentation files (the "Software"), to deal
6+ // in the Software without restriction, including without limitation the rights
7+ // to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+ // copies of the Software, and to permit persons to whom the Software is
9+ // furnished to do so, subject to the following conditions:
10+ //
11+ // The above copyright notice and this permission notice shall be included in
12+ // all copies or substantial portions of the Software.
13+ //
14+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+ // THE SOFTWARE.
21+ // ------------------------------------------------------------------------------
22+
23+ package com .microsoft .graph .serializer ;
24+
25+ import java .lang .reflect .InvocationTargetException ;
26+ import java .lang .reflect .Type ;
27+ import java .util .ArrayList ;
28+ import java .util .Arrays ;
29+ import java .util .List ;
30+
31+ import com .google .gson .JsonArray ;
32+ import com .google .gson .JsonElement ;
33+ import com .google .gson .JsonObject ;
34+ import com .google .gson .JsonParseException ;
35+ import com .google .gson .JsonParser ;
36+ import com .google .common .reflect .TypeToken ;
37+ import com .microsoft .graph .http .BaseCollectionPage ;
38+ import com .microsoft .graph .http .IRequestBuilder ;
39+ import com .microsoft .graph .logger .ILogger ;
40+
41+ // those imports are useless but build will fail if code-gen conventions change, keep it
42+ import com .microsoft .graph .models .extensions .Attachment ;
43+ import com .microsoft .graph .requests .extensions .AttachmentCollectionPage ;
44+ import com .microsoft .graph .requests .extensions .AttachmentCollectionResponse ;
45+ import com .microsoft .graph .requests .extensions .IAttachmentCollectionRequestBuilder ;
46+
47+ public class CollectionPageSerializer {
48+
49+ private static DefaultSerializer serializer ;
50+ /** length of the word "page" */
51+ private final static Integer pageLength = 4 ;
52+ /** length of the word "collection" */
53+ private final static Integer collectionLength = 10 ;
54+ /** length of the work "response" */
55+ private final static Integer responseLength = 8 ;
56+ /** the extensions segment in the package name of target classes */
57+ private final static String extensionsPath = "extensions." ;
58+
59+ /**
60+ * Not available for instantiation
61+ */
62+ private CollectionPageSerializer () {
63+ }
64+
65+ /**
66+ * Serializes an CollectionPage
67+ *
68+ * @param src the CollectionPage variable for serialization
69+ * @param logger the logger
70+ * @return JsonElement of CollectionPage
71+ */
72+ public static <T1 , T2 extends IRequestBuilder > JsonElement serialize (final BaseCollectionPage <T1 , T2 > src , final ILogger logger ) {
73+ if (src == null ) {
74+ return null ;
75+ }
76+ JsonArray jsonArray = new JsonArray ();
77+ List <T1 > items = src .getCurrentPage ();
78+ serializer = new DefaultSerializer (logger );
79+ for (T1 item : items ) {
80+ final String json = serializer .serializeObject (item );
81+ final JsonElement element = JsonParser .parseString (json );
82+ if (element != null && element .isJsonObject ()) {
83+ final JsonObject jsonObject = element .getAsJsonObject ();
84+ jsonArray .add (jsonObject );
85+ }
86+ }
87+ return jsonArray ;
88+ }
89+
90+ /**
91+ * Deserializes the JsonElement
92+ *
93+ * @param json the source CollectionPage's Json
94+ * @param typeOfT The type of the CollectionPage to deserialize to
95+ * @param logger the logger
96+ * @throws JsonParseException the parse exception
97+ * @return the deserialized CollectionPage
98+ */
99+ @ SuppressWarnings ("unchecked" )
100+ public static <T1 , T2 extends IRequestBuilder > BaseCollectionPage <T1 , T2 > deserialize (final JsonElement json , Type typeOfT , final ILogger logger ) throws JsonParseException {
101+ if (json == null ) {
102+ return null ;
103+ }
104+ serializer = new DefaultSerializer (logger );
105+ final JsonObject [] sourceArray = serializer .deserializeObject (json .toString (), JsonObject [].class );
106+ final ArrayList <T1 > list = new ArrayList <T1 >(sourceArray .length );
107+ final String collectionPageClassCanonicalName = typeOfT .getTypeName ();
108+ final String entityClassCanonicalName = collectionPageClassCanonicalName
109+ .substring (0 , collectionPageClassCanonicalName .length () - pageLength - collectionLength )
110+ .replace ("requests" , "models" );
111+ try {
112+ final Class <?> entityClass = Class .forName (entityClassCanonicalName );
113+ for (JsonObject sourceObject : sourceArray ) {
114+ final T1 targetObject = (T1 )serializer .deserializeObject (sourceObject .toString (), entityClass );
115+ ((IJsonBackedObject )targetObject ).setRawObject (serializer , sourceObject );
116+ list .add (targetObject );
117+ }
118+ final String responseClassCanonicalName = collectionPageClassCanonicalName
119+ .substring (0 , collectionPageClassCanonicalName .length () - pageLength ) + "Response" ;
120+ final Class <?> responseClass = Class .forName (responseClassCanonicalName );
121+ final Object response = responseClass .getConstructor ().newInstance ();
122+ responseClass .getField ("value" ).set (response , list );
123+ final Class <?> collectionPageClass = Class .forName (collectionPageClassCanonicalName );
124+ final String responseBuilderInterfaceCanonicalName = responseClassCanonicalName
125+ .substring (0 , responseClassCanonicalName .length () - responseLength )
126+ .replace (extensionsPath , extensionsPath + "I" ) + "RequestBuilder" ;
127+ final Class <?> responseBuilderInterfaceClass = Class .forName (responseBuilderInterfaceCanonicalName );
128+ return (BaseCollectionPage <T1 , T2 >)collectionPageClass .getConstructor (responseClass , responseBuilderInterfaceClass ).newInstance (response , null );
129+ } catch (ClassNotFoundException ex ) {
130+ logger .logError ("Could not find class during deserialization" , ex );
131+ } catch (NoSuchMethodException | InstantiationException | InvocationTargetException ex ) {
132+ logger .logError ("Could not instanciate type during deserialization" , ex );
133+ } catch (NoSuchFieldException | IllegalAccessException ex ) {
134+ logger .logError ("Unable to set field value during deserialization" , ex );
135+ }
136+ return null ;
137+ }
138+ }
0 commit comments