Search in sources :

Example 1 with DateOnly

use of com.microsoft.graph.core.DateOnly in project msgraph-sdk-java by microsoftgraph.

the class DefaultSerializerTests method testRecurrenceRangeSerialization.

@Test
public void testRecurrenceRangeSerialization() throws Exception {
    final String expected = "{\"endDate\":\"2016-05-25\",\"numberOfOccurrences\":4,\"recurrenceTimeZone\":\"PST\",\"startDate\":\"2016-04-25\",\"type\":\"endDate\"}";
    final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
    RecurrenceRange brr = new RecurrenceRange();
    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);
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) RecurrenceRange(com.microsoft.graph.models.RecurrenceRange) DateOnly(com.microsoft.graph.core.DateOnly) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) Test(org.junit.jupiter.api.Test)

Example 2 with DateOnly

use of com.microsoft.graph.core.DateOnly in project msgraph-beta-sdk-java by microsoftgraph.

the class DefaultSerializerTests method testRecurrenceRangeSerialization.

@Test
public void testRecurrenceRangeSerialization() throws Exception {
    final String expected = "{\"endDate\":\"2016-05-25\",\"numberOfOccurrences\":4,\"recurrenceTimeZone\":\"PST\",\"startDate\":\"2016-04-25\",\"type\":\"endDate\"}";
    final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
    RecurrenceRange brr = new RecurrenceRange();
    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);
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) RecurrenceRange(com.microsoft.graph.models.RecurrenceRange) DateOnly(com.microsoft.graph.core.DateOnly) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) Test(org.junit.jupiter.api.Test)

Example 3 with DateOnly

use of com.microsoft.graph.core.DateOnly in project msgraph-sdk-java-core 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());
}
Also used : DateOnly(com.microsoft.graph.core.DateOnly) Test(org.junit.jupiter.api.Test)

Example 4 with DateOnly

use of com.microsoft.graph.core.DateOnly in project msgraph-sdk-java-core 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());
}
Also used : DateOnly(com.microsoft.graph.core.DateOnly) Test(org.junit.jupiter.api.Test)

Example 5 with DateOnly

use of com.microsoft.graph.core.DateOnly in project msgraph-sdk-java-core by microsoftgraph.

the class GsonFactory method getGsonInstance.

/**
 * Creates an instance of GSON
 *
 * Serializing of null values can have side effects on the service behavior.
 * Sending null values in a PATCH request might reset existing values on the service side.
 * Sending null values in a POST request might prevent the service from assigning default values to the properties.
 * It is not recommended to send null values to the service in general and this setting should only be used when serializing information for a local store.
 *
 * @param logger         the logger
 * @param serializeNulls the setting of whether or not to serialize the null values in the JSON object
 * @return the new instance
 */
@Nonnull
public static Gson getGsonInstance(@Nonnull final ILogger logger, final boolean serializeNulls) {
    Objects.requireNonNull(logger, "parameter logger cannot be null");
    final JsonSerializer<OffsetDateTime> calendarJsonSerializer = (src, typeOfSrc, context) -> {
        if (src == null) {
            return null;
        }
        try {
            return new JsonPrimitive(OffsetDateTimeSerializer.serialize(src));
        } catch (final Exception e) {
            logger.logError(PARSING_MESSAGE + src, e);
            return null;
        }
    };
    final JsonDeserializer<OffsetDateTime> calendarJsonDeserializer = (json, typeOfT, context) -> {
        if (json == null) {
            return null;
        }
        try {
            return OffsetDateTimeSerializer.deserialize(json.getAsString());
        } catch (final ParseException e) {
            logger.logError(PARSING_MESSAGE + json.getAsString(), e);
            return null;
        }
    };
    final JsonSerializer<byte[]> byteArrayJsonSerializer = (src, typeOfSrc, context) -> {
        if (src == null) {
            return null;
        }
        try {
            return new JsonPrimitive(ByteArraySerializer.serialize(src));
        } catch (final Exception e) {
            logger.logError(PARSING_MESSAGE + Arrays.toString(src), e);
            return null;
        }
    };
    final JsonDeserializer<byte[]> byteArrayJsonDeserializer = (json, typeOfT, context) -> {
        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 = (src, typeOfSrc, context) -> {
        if (src == null) {
            return null;
        }
        return new JsonPrimitive(src.toString());
    };
    final JsonDeserializer<DateOnly> dateJsonDeserializer = (json, typeOfT, context) -> {
        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 EnumSetSerializer eSetSerializer = new EnumSetSerializer(logger);
    final JsonSerializer<EnumSet<?>> enumSetJsonSerializer = (src, typeOfSrc, context) -> {
        if (src == null || src.isEmpty()) {
            return null;
        }
        return eSetSerializer.serialize(src);
    };
    final JsonDeserializer<EnumSet<?>> enumSetJsonDeserializer = (json, typeOfT, context) -> {
        if (json == null) {
            return null;
        }
        return eSetSerializer.deserialize(typeOfT, json.getAsString());
    };
    final JsonSerializer<Duration> durationJsonSerializer = (src, typeOfSrc, context) -> new JsonPrimitive(src.toString());
    final JsonDeserializer<Duration> durationJsonDeserializer = (json, typeOfT, context) -> {
        try {
            return DatatypeFactory.newInstance().newDuration(json.getAsString());
        } catch (Exception e) {
            return null;
        }
    };
    final JsonSerializer<BaseCollectionPage<?, ?>> collectionPageSerializer = (src, typeOfSrc, context) -> CollectionPageSerializer.serialize(src, logger);
    final JsonDeserializer<BaseCollectionPage<?, ?>> collectionPageDeserializer = (json, typeOfT, context) -> CollectionPageSerializer.deserialize(json, typeOfT, logger);
    final JsonDeserializer<BaseCollectionResponse<?>> collectionResponseDeserializer = (json, typeOfT, context) -> CollectionResponseDeserializer.deserialize(json, typeOfT, logger);
    final JsonDeserializer<TimeOfDay> timeOfDayJsonDeserializer = (json, typeOfT, context) -> {
        try {
            return TimeOfDay.parse(json.getAsString());
        } catch (Exception e) {
            return null;
        }
    };
    final JsonSerializer<TimeOfDay> timeOfDayJsonSerializer = (src, typeOfSrc, context) -> new JsonPrimitive(src.toString());
    final JsonDeserializer<Boolean> booleanJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, Boolean.class, logger);
    final JsonDeserializer<String> stringJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, String.class, logger);
    final JsonDeserializer<BigDecimal> bigDecimalJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, BigDecimal.class, logger);
    final JsonDeserializer<Integer> integerJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, Integer.class, logger);
    final JsonDeserializer<Long> longJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, Long.class, logger);
    final JsonDeserializer<UUID> uuidJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, UUID.class, logger);
    final JsonDeserializer<Float> floatJsonDeserializer = (json, typeOfT, context) -> EdmNativeTypeSerializer.deserialize(json, Float.class, logger);
    GsonBuilder builder = new GsonBuilder();
    if (serializeNulls) {
        builder.serializeNulls();
    }
    return builder.excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Boolean.class, booleanJsonDeserializer).registerTypeAdapter(String.class, stringJsonDeserializer).registerTypeAdapter(Float.class, floatJsonDeserializer).registerTypeAdapter(Integer.class, integerJsonDeserializer).registerTypeAdapter(BigDecimal.class, bigDecimalJsonDeserializer).registerTypeAdapter(UUID.class, uuidJsonDeserializer).registerTypeAdapter(Long.class, longJsonDeserializer).registerTypeAdapter(OffsetDateTime.class, calendarJsonSerializer).registerTypeAdapter(OffsetDateTime.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).registerTypeHierarchyAdapter(BaseCollectionPage.class, collectionPageSerializer).registerTypeHierarchyAdapter(BaseCollectionPage.class, collectionPageDeserializer).registerTypeHierarchyAdapter(BaseCollectionResponse.class, collectionResponseDeserializer).registerTypeAdapter(TimeOfDay.class, timeOfDayJsonDeserializer).registerTypeAdapter(TimeOfDay.class, timeOfDayJsonSerializer).registerTypeAdapterFactory(new FallbackTypeAdapterFactory(logger)).create();
}
Also used : JsonParseException(com.google.gson.JsonParseException) Arrays(java.util.Arrays) ILogger(com.microsoft.graph.logger.ILogger) JsonSerializer(com.google.gson.JsonSerializer) DatatypeFactory(javax.xml.datatype.DatatypeFactory) GsonBuilder(com.google.gson.GsonBuilder) TimeOfDay(com.microsoft.graph.core.TimeOfDay) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) JsonElement(com.google.gson.JsonElement) BaseCollectionPage(com.microsoft.graph.http.BaseCollectionPage) BigDecimal(java.math.BigDecimal) Duration(javax.xml.datatype.Duration) Gson(com.google.gson.Gson) JsonSerializationContext(com.google.gson.JsonSerializationContext) JsonPrimitive(com.google.gson.JsonPrimitive) ParseException(java.text.ParseException) Nonnull(javax.annotation.Nonnull) DateOnly(com.microsoft.graph.core.DateOnly) EnumSet(java.util.EnumSet) GregorianCalendar(java.util.GregorianCalendar) UUID(java.util.UUID) Objects(java.util.Objects) OffsetDateTime(java.time.OffsetDateTime) Type(java.lang.reflect.Type) JsonDeserializer(com.google.gson.JsonDeserializer) BaseCollectionResponse(com.microsoft.graph.http.BaseCollectionResponse) BaseCollectionPage(com.microsoft.graph.http.BaseCollectionPage) BaseCollectionResponse(com.microsoft.graph.http.BaseCollectionResponse) JsonPrimitive(com.google.gson.JsonPrimitive) UUID(java.util.UUID) TimeOfDay(com.microsoft.graph.core.TimeOfDay) GsonBuilder(com.google.gson.GsonBuilder) EnumSet(java.util.EnumSet) GregorianCalendar(java.util.GregorianCalendar) DateOnly(com.microsoft.graph.core.DateOnly) Duration(javax.xml.datatype.Duration) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) BigDecimal(java.math.BigDecimal) OffsetDateTime(java.time.OffsetDateTime) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) Nonnull(javax.annotation.Nonnull)

Aggregations

DateOnly (com.microsoft.graph.core.DateOnly)5 Test (org.junit.jupiter.api.Test)4 DefaultLogger (com.microsoft.graph.logger.DefaultLogger)2 RecurrenceRange (com.microsoft.graph.models.RecurrenceRange)2 DefaultSerializer (com.microsoft.graph.serializer.DefaultSerializer)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonDeserializationContext (com.google.gson.JsonDeserializationContext)1 JsonDeserializer (com.google.gson.JsonDeserializer)1 JsonElement (com.google.gson.JsonElement)1 JsonParseException (com.google.gson.JsonParseException)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonSerializationContext (com.google.gson.JsonSerializationContext)1 JsonSerializer (com.google.gson.JsonSerializer)1 TimeOfDay (com.microsoft.graph.core.TimeOfDay)1 BaseCollectionPage (com.microsoft.graph.http.BaseCollectionPage)1 BaseCollectionResponse (com.microsoft.graph.http.BaseCollectionResponse)1 ILogger (com.microsoft.graph.logger.ILogger)1 Type (java.lang.reflect.Type)1 BigDecimal (java.math.BigDecimal)1