|
| 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 | +} |
0 commit comments