use of com.microsoft.graph.models.extensions.DateOnly in project msgraph-sdk-java by microsoftgraph.
the class DefaultSeralizerTests method testRecurrenceRangeSerialization.
@Test
public void testRecurrenceRangeSerialization() throws Exception {
final String expected = "{\"type\":\"endDate\",\"startDate\":\"2016-04-25\",\"endDate\":\"2016-05-25\",\"recurrenceTimeZone\":\"PST\",\"numberOfOccurrences\":4}";
final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
BaseRecurrenceRange brr = new BaseRecurrenceRange();
brr.type = RecurrenceRangeType.END_DATE;
brr.startDate = new DateOnly(2016, 4, 25);
brr.endDate = new DateOnly(2016, 5, 25);
brr.recurrenceTimeZone = "PST";
brr.numberOfOccurrences = 4;
String jsonOut = serializer.serializeObject(brr);
assertNotNull(jsonOut);
assertEquals(expected, jsonOut);
}
use of com.microsoft.graph.models.extensions.DateOnly in project msgraph-sdk-java by microsoftgraph.
the class GsonFactory method getGsonInstance.
/**
* Creates an instance of GSON
*
* @param logger the logger
* @return the new instance
*/
public static Gson getGsonInstance(final ILogger logger) {
final JsonSerializer<Calendar> calendarJsonSerializer = new JsonSerializer<Calendar>() {
@Override
public JsonElement serialize(final Calendar src, final Type typeOfSrc, final JsonSerializationContext context) {
if (src == null) {
return null;
}
try {
return new JsonPrimitive(CalendarSerializer.serialize(src));
} catch (final Exception e) {
logger.logError(PARSING_MESSAGE + src, e);
return null;
}
}
};
final JsonDeserializer<Calendar> calendarJsonDeserializer = new JsonDeserializer<Calendar>() {
@Override
public Calendar deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
try {
return CalendarSerializer.deserialize(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
}
};
final JsonSerializer<byte[]> byteArrayJsonSerializer = new JsonSerializer<byte[]>() {
@Override
public JsonElement serialize(final byte[] src, final Type typeOfSrc, final JsonSerializationContext context) {
if (src == null) {
return null;
}
try {
return new JsonPrimitive(ByteArraySerializer.serialize(src));
} catch (final Exception e) {
logger.logError(PARSING_MESSAGE + src, e);
return null;
}
}
};
final JsonDeserializer<byte[]> byteArrayJsonDeserializer = new JsonDeserializer<byte[]>() {
@Override
public byte[] deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
try {
return ByteArraySerializer.deserialize(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
}
};
final JsonSerializer<DateOnly> dateJsonSerializer = new JsonSerializer<DateOnly>() {
@Override
public JsonElement serialize(final DateOnly src, final Type typeOfSrc, final JsonSerializationContext context) {
if (src == null) {
return null;
}
return new JsonPrimitive(src.toString());
}
};
final JsonDeserializer<DateOnly> dateJsonDeserializer = new JsonDeserializer<DateOnly>() {
@Override
public DateOnly deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
try {
return DateOnly.parse(json.getAsString());
} catch (final ParseException e) {
logger.logError(PARSING_MESSAGE + json.getAsString(), e);
return null;
}
}
};
final JsonSerializer<EnumSet<?>> enumSetJsonSerializer = new JsonSerializer<EnumSet<?>>() {
@Override
public JsonElement serialize(final EnumSet<?> src, final Type typeOfSrc, final JsonSerializationContext context) {
if (src == null || src.isEmpty()) {
return null;
}
return EnumSetSerializer.serialize(src);
}
};
final JsonDeserializer<EnumSet<?>> enumSetJsonDeserializer = new JsonDeserializer<EnumSet<?>>() {
@Override
public EnumSet<?> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
if (json == null) {
return null;
}
return EnumSetSerializer.deserialize(typeOfT, json.getAsString());
}
};
final JsonSerializer<Duration> durationJsonSerializer = new JsonSerializer<Duration>() {
@Override
public JsonElement serialize(final Duration src, final Type typeOfSrc, final JsonSerializationContext context) {
return new JsonPrimitive(src.toString());
}
};
final JsonDeserializer<Duration> durationJsonDeserializer = new JsonDeserializer<Duration>() {
@Override
public Duration deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
try {
return DatatypeFactory.newInstance().newDuration(json.toString());
} catch (Exception e) {
return null;
}
}
};
return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Calendar.class, calendarJsonSerializer).registerTypeAdapter(Calendar.class, calendarJsonDeserializer).registerTypeAdapter(GregorianCalendar.class, calendarJsonSerializer).registerTypeAdapter(GregorianCalendar.class, calendarJsonDeserializer).registerTypeAdapter(byte[].class, byteArrayJsonDeserializer).registerTypeAdapter(byte[].class, byteArrayJsonSerializer).registerTypeAdapter(DateOnly.class, dateJsonSerializer).registerTypeAdapter(DateOnly.class, dateJsonDeserializer).registerTypeAdapter(EnumSet.class, enumSetJsonSerializer).registerTypeAdapter(EnumSet.class, enumSetJsonDeserializer).registerTypeAdapter(Duration.class, durationJsonSerializer).registerTypeAdapter(Duration.class, durationJsonDeserializer).registerTypeAdapterFactory(new FallBackEnumTypeAdapter()).create();
}
use of com.microsoft.graph.models.extensions.DateOnly in project msgraph-sdk-java by microsoftgraph.
the class DateOnlyTests method testDateDeserializerIndefinite.
@Test
public void testDateDeserializerIndefinite() throws Exception {
DateOnly date = DateOnly.parse("0001-01-01");
assertEquals(1, date.getYear());
assertEquals(1, date.getMonth());
assertEquals(1, date.getDay());
}
use of com.microsoft.graph.models.extensions.DateOnly in project msgraph-sdk-java by microsoftgraph.
the class DateOnlyTests method testDateDeserializer.
@Test
public void testDateDeserializer() throws Exception {
DateOnly date = DateOnly.parse("2016-04-27");
assertEquals(2016, date.getYear());
assertEquals(4, date.getMonth());
assertEquals(27, date.getDay());
}
Aggregations