Skip to content
This repository was archived by the owner on Sep 4, 2020. It is now read-only.

Commit 96ff555

Browse files
author
Caitlin Bales (MSFT)
authored
Merge pull request #46 from microsoftgraph/EnumSets
Add EnumSet serializer
2 parents 6870d8f + 80a4bbb commit 96ff555

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2015 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, sublicense, 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+
package com.microsoft.graph.serializer;
23+
24+
import com.google.gson.Gson;
25+
import com.google.gson.JsonPrimitive;
26+
27+
import java.lang.reflect.Type;
28+
import java.util.EnumSet;
29+
import java.util.Iterator;
30+
31+
/**
32+
* Serializes and deserializes EnumSets
33+
* The Graph service expects a single enum value as a comma-delimited string
34+
* Here, we flatten the EnumSet to serialize the object
35+
* and insert the response into an array to deserialize back to an EnumSet
36+
*/
37+
public class EnumSetSerializer {
38+
39+
/**
40+
* Not available for instantiation.
41+
*/
42+
private EnumSetSerializer() {
43+
}
44+
45+
/**
46+
* Deserializes a comma-delimited string of enum values
47+
* @param type The type
48+
* @param jsonStrToDeserialize The string to deserialize
49+
* @return EnumSet of values
50+
*/
51+
public static EnumSet deserialize(Type type, String jsonStrToDeserialize) {
52+
Gson gson = new Gson();
53+
String arrayString = "[" + jsonStrToDeserialize + "]";
54+
return jsonStrToDeserialize == null ? null : (EnumSet) gson.fromJson(arrayString, type);
55+
}
56+
57+
/**
58+
* Serializes an EnumSet into a comma-delimited string
59+
* @param src The source EnumSet
60+
* @return A comma-delimited string of enum values
61+
*/
62+
public static JsonPrimitive serialize(EnumSet<?> src) {
63+
String serializedString = "";
64+
65+
Iterator i = src.iterator();
66+
while (i.hasNext()) {
67+
serializedString += i.next().toString() + ",";
68+
}
69+
serializedString = serializedString.substring(0, serializedString.length()-1);
70+
return new JsonPrimitive(serializedString);
71+
}
72+
}

graphsdk/src/main/java/com/microsoft/graph/serializer/GsonFactory.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.lang.reflect.Type;
3838
import java.text.ParseException;
3939
import java.util.Calendar;
40+
import java.util.EnumSet;
4041
import java.util.GregorianCalendar;
4142

4243
/**
@@ -156,6 +157,32 @@ public DateOnly deserialize(final JsonElement json,
156157
}
157158
};
158159

160+
final JsonSerializer<EnumSet> enumSetJsonSerializer = new JsonSerializer<EnumSet>() {
161+
@Override
162+
public JsonElement serialize(final EnumSet src,
163+
final Type typeOfSrc,
164+
final JsonSerializationContext context) {
165+
if (src == null || src.size() == 0) {
166+
return null;
167+
}
168+
169+
return EnumSetSerializer.serialize(src);
170+
}
171+
};
172+
173+
final JsonDeserializer<EnumSet> enumSetJsonDeserializer = new JsonDeserializer<EnumSet>() {
174+
@Override
175+
public EnumSet deserialize(final JsonElement json,
176+
final Type typeOfT,
177+
final JsonDeserializationContext context) throws JsonParseException {
178+
if (json == null) {
179+
return null;
180+
}
181+
182+
return EnumSetSerializer.deserialize(typeOfT, json.getAsString());
183+
}
184+
};
185+
159186
return new GsonBuilder()
160187
.excludeFieldsWithoutExposeAnnotation()
161188
.registerTypeAdapter(Calendar.class, calendarJsonSerializer)
@@ -166,6 +193,8 @@ public DateOnly deserialize(final JsonElement json,
166193
.registerTypeAdapter(byte[].class, byteArrayJsonSerializer)
167194
.registerTypeAdapter(DateOnly.class, dateJsonSerializer)
168195
.registerTypeAdapter(DateOnly.class, dateJsonDeserializer)
196+
.registerTypeAdapter(EnumSet.class, enumSetJsonSerializer)
197+
.registerTypeAdapter(EnumSet.class, enumSetJsonDeserializer)
169198
.registerTypeAdapterFactory(new FallBackEnumTypeAdapter())
170199
.create();
171200
}

0 commit comments

Comments
 (0)