Search in sources :

Example 1 with DateOnly

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);
}
Also used : BaseRecurrenceRange(com.microsoft.graph.models.generated.BaseRecurrenceRange) DateOnly(com.microsoft.graph.models.extensions.DateOnly) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) Test(org.junit.Test)

Example 2 with DateOnly

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();
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonSerializer(com.google.gson.JsonSerializer) JsonDeserializer(com.google.gson.JsonDeserializer) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) EnumSet(java.util.EnumSet) GregorianCalendar(java.util.GregorianCalendar) DateOnly(com.microsoft.graph.models.extensions.DateOnly) Duration(javax.xml.datatype.Duration) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) JsonSerializationContext(com.google.gson.JsonSerializationContext) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException)

Example 3 with DateOnly

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());
}
Also used : DateOnly(com.microsoft.graph.models.extensions.DateOnly) Test(org.junit.Test)

Example 4 with DateOnly

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());
}
Also used : DateOnly(com.microsoft.graph.models.extensions.DateOnly) Test(org.junit.Test)

Aggregations

DateOnly (com.microsoft.graph.models.extensions.DateOnly)4 Test (org.junit.Test)3 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 DefaultLogger (com.microsoft.graph.logger.DefaultLogger)1 BaseRecurrenceRange (com.microsoft.graph.models.generated.BaseRecurrenceRange)1 Type (java.lang.reflect.Type)1 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 EnumSet (java.util.EnumSet)1 GregorianCalendar (java.util.GregorianCalendar)1 Duration (javax.xml.datatype.Duration)1