@Test(expected = JsonParseException.class) public void testOpeningTimeAsJsonPrimitive() { JsonPrimitive jsonPrimitive = new JsonPrimitive("12:00"); adapter.deserialize(jsonPrimitive, OpeningTime.class, null); }
@Override public OpeningTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("OpeningTime must be a valid JSON object", e); } Day day = Day.fromValue(obj.getAsJsonPrimitive("day").getAsString()); String timeStartStr = obj.getAsJsonPrimitive("timeStart").getAsString(); String timeStopStr = obj.getAsJsonPrimitive("timeStop").getAsString(); TimeOfDay timeStart; TimeOfDay timeStop; Matcher start = TIME_OF_DAY.matcher(timeStartStr); Matcher stop = TIME_OF_DAY.matcher(timeStopStr); if (start.matches() && stop.matches()) { timeStart = new TimeOfDay(Integer.parseInt(start.group(HOUR_GROUP)), Integer.parseInt(start.group(MINUTES_GROUP))); timeStop = new TimeOfDay(Integer.parseInt(stop.group(HOUR_GROUP)), Integer.parseInt(stop.group(MINUTES_GROUP))); } else { throw new JsonParseException("timeStart and timeStop must be a valid 24-hour time (00:00 - 23:59)"); } return new OpeningTime(day, timeStart, timeStop); }
OpeningTimeTypeAdapterDeserializer implements TypeAdapterDeserializer<OpeningTime> { @Override public OpeningTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("OpeningTime must be a valid JSON object", e); } Day day = Day.fromValue(obj.getAsJsonPrimitive("day").getAsString()); String timeStartStr = obj.getAsJsonPrimitive("timeStart").getAsString(); String timeStopStr = obj.getAsJsonPrimitive("timeStop").getAsString(); TimeOfDay timeStart; TimeOfDay timeStop; Matcher start = TIME_OF_DAY.matcher(timeStartStr); Matcher stop = TIME_OF_DAY.matcher(timeStopStr); if (start.matches() && stop.matches()) { timeStart = new TimeOfDay(Integer.parseInt(start.group(HOUR_GROUP)), Integer.parseInt(start.group(MINUTES_GROUP))); timeStop = new TimeOfDay(Integer.parseInt(stop.group(HOUR_GROUP)), Integer.parseInt(stop.group(MINUTES_GROUP))); } else { throw new JsonParseException("timeStart and timeStop must be a valid 24-hour time (00:00 - 23:59)"); } return new OpeningTime(day, timeStart, timeStop); } }
OpeningTimeTypeAdapterDeserializer implements TypeAdapterDeserializer<OpeningTime> { @Override public OpeningTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("OpeningTime must be a valid JSON object", e); } Day day = Day.fromValue(obj.getAsJsonPrimitive("day").getAsString()); String timeStartStr = obj.getAsJsonPrimitive("timeStart").getAsString(); String timeStopStr = obj.getAsJsonPrimitive("timeStop").getAsString(); TimeOfDay timeStart; TimeOfDay timeStop; Matcher start = TIME_OF_DAY.matcher(timeStartStr); Matcher stop = TIME_OF_DAY.matcher(timeStopStr); if (start.matches() && stop.matches()) { timeStart = new TimeOfDay(Integer.parseInt(start.group(HOUR_GROUP)), Integer.parseInt(start.group(MINUTES_GROUP))); timeStop = new TimeOfDay(Integer.parseInt(stop.group(HOUR_GROUP)), Integer.parseInt(stop.group(MINUTES_GROUP))); } else { throw new JsonParseException("timeStart and timeStop must be a valid 24-hour time (00:00 - 23:59)"); } return new OpeningTime(day, timeStart, timeStop); }  }
OpeningTimeTypeAdapterDeserializer implements TypeAdapterDeserializer<OpeningTime> { @Override public OpeningTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("OpeningTime must be a valid JSON object", e); } Day day = Day.fromValue(obj.getAsJsonPrimitive("day").getAsString()); String timeStartStr = obj.getAsJsonPrimitive("timeStart").getAsString(); String timeStopStr = obj.getAsJsonPrimitive("timeStop").getAsString(); TimeOfDay timeStart; TimeOfDay timeStop; Matcher start = TIME_OF_DAY.matcher(timeStartStr); Matcher stop = TIME_OF_DAY.matcher(timeStopStr); if (start.matches() && stop.matches()) { timeStart = new TimeOfDay(Integer.parseInt(start.group(HOUR_GROUP)), Integer.parseInt(start.group(MINUTES_GROUP))); timeStop = new TimeOfDay(Integer.parseInt(stop.group(HOUR_GROUP)), Integer.parseInt(stop.group(MINUTES_GROUP))); } else { throw new JsonParseException("timeStart and timeStop must be a valid 24-hour time (00:00 - 23:59)"); } return new OpeningTime(day, timeStart, timeStop); }  @Override Class<?> getAdaptedType(); @Override OpeningTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext); }
OpeningTimeTypeAdapterDeserializer implements TypeAdapterDeserializer<OpeningTime> { @Override public OpeningTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { JsonObject obj; try { obj = jsonElement.getAsJsonObject(); if (obj == null) { return null; } } catch (ClassCastException | IllegalStateException e) { throw new JsonParseException("OpeningTime must be a valid JSON object", e); } Day day = Day.fromValue(obj.getAsJsonPrimitive("day").getAsString()); String timeStartStr = obj.getAsJsonPrimitive("timeStart").getAsString(); String timeStopStr = obj.getAsJsonPrimitive("timeStop").getAsString(); TimeOfDay timeStart; TimeOfDay timeStop; Matcher start = TIME_OF_DAY.matcher(timeStartStr); Matcher stop = TIME_OF_DAY.matcher(timeStopStr); if (start.matches() && stop.matches()) { timeStart = new TimeOfDay(Integer.parseInt(start.group(HOUR_GROUP)), Integer.parseInt(start.group(MINUTES_GROUP))); timeStop = new TimeOfDay(Integer.parseInt(stop.group(HOUR_GROUP)), Integer.parseInt(stop.group(MINUTES_GROUP))); } else { throw new JsonParseException("timeStart and timeStop must be a valid 24-hour time (00:00 - 23:59)"); } return new OpeningTime(day, timeStart, timeStop); }  @Override Class<?> getAdaptedType(); @Override OpeningTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext);  }