@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ") public Date getStartedDateTime() { return startedDateTime; }
private void read(ReflectAnnotatedElement annotations) { if (annotations == null) { return; } JsonFormat format = annotations.getAnnotation(JsonFormat.class); if (format == null) { return; } if (format.shape() == JsonFormat.Shape.STRING) { asString = true; } else { return; } pattern = format.pattern(); if (pattern.isEmpty()) { pattern = null; } locale = format.locale(); }
/** * A quick check to see if a declaration defines a enum schema type. * * @param declaration The declaration to check. * @return the value of the check. */ protected boolean isEnumType(TypeElement declaration) { JsonFormat format = declaration.getAnnotation(JsonFormat.class); if (format != null && format.shape() == JsonFormat.Shape.OBJECT) { return false; } return declaration.getKind() == ElementKind.ENUM; }
@Override public void setCell(Date o, Cell cell, Field field) { if (o != null) { String format = field.getAnnotation(JsonFormat.class).pattern(); cell.setCellValue(new SimpleDateFormat(format).format(o)); } }
/** * Add the common property parameters to the code builder. * * @param paramBuilder the code builder * @param property the information about the property */ protected final void buildCommonPropertyParameters( CodeBlock.Builder paramBuilder, PropertyInfo property ) { if ( property.getFormat().isPresent() ) { JsonFormat format = property.getFormat().get(); if ( !Strings.isNullOrEmpty( format.pattern() ) ) { paramBuilder.add( "\n.setPattern($S)", format.pattern() ); } paramBuilder.add( "\n.setShape($T.$L)", Shape.class, format.shape().name() ); if ( !Strings.isNullOrEmpty( format.locale() ) && !JsonFormat.DEFAULT_LOCALE.equals( format.locale() ) ) { logger.log( Type.WARN, "JsonFormat.locale is not supported by default" ); paramBuilder.add( "\n.setLocale($S)", format.locale() ); } } if ( property.getIgnoredProperties().isPresent() ) { for ( String ignoredProperty : property.getIgnoredProperties().get() ) { paramBuilder.add( "\n.addIgnoredProperty($S)", ignoredProperty ); } } }
/** * A quick check to see if a declaration defines a enum schema type. * * @param declaration The declaration to check. * @return the value of the check. */ protected boolean isEnumType(TypeElement declaration) { JsonFormat format = declaration.getAnnotation(JsonFormat.class); if (format != null && format.shape() == JsonFormat.Shape.OBJECT) { return false; } return declaration.getKind() == ElementKind.ENUM; }
@Override public Date parse(JsonParser parser, Field field) throws IOException { if (!isValid(parser)) { return null; } JsonFormat formatAnn = field.getAnnotation(JsonFormat.class); if (formatAnn == null) { throw new RuntimeException("JsonFormat with pattern needed for: " + field.getName()); } try { return new SimpleDateFormat(formatAnn.pattern(), Locale.getDefault()).parse(parser.getValueAsString()); } catch (ParseException e) { throw new RuntimeException("JsonFormat with pattern is wrong for: " + field.getName() + " pattern: " + formatAnn.pattern()); } } }
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.ssZ") public Date getStartedDateTime() { return startedDateTime; }
private DeclarationModel parseEnumOrObjectEnum(SourceType<Class<?>> sourceClass) { final JsonFormat jsonFormat = sourceClass.type.getAnnotation(JsonFormat.class); if (jsonFormat != null && jsonFormat.shape() == JsonFormat.Shape.OBJECT) { return parseBean(sourceClass); jsonFormat.shape() == JsonFormat.Shape.NUMBER || jsonFormat.shape() == JsonFormat.Shape.NUMBER_FLOAT || jsonFormat.shape() == JsonFormat.Shape.NUMBER_INT);
/** * First it checks is there any annotation class for parsing operations, * if it is, parses with given format, if there is a exception while * parsing with given format catches and tries with default values, * If there is no given format, tries with static values * * @param o Object from cell value * @param field Field from given pojo * @return Valid date after parsing with pattern */ @Override public Date parse(Object o, Field field) { if (!isValid(o)) { return null; } JsonFormat formatAnn = field.getAnnotation(JsonFormat.class); if (formatAnn == null) { throw new RuntimeException("JsonFormat with pattern needed for: " + field.getName()); } try { return new SimpleDateFormat(formatAnn.pattern(), Locale.getDefault()).parse(o.toString()); } catch (ParseException e) { throw new RuntimeException("JsonFormat with pattern is wrong for: " + field.getName() + " pattern: " + formatAnn.pattern()); } }
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.ssZ") public Date getExpires() { return expires; }
switch (format.shape()) { case ARRAY: return KnownJsonType.ARRAY;
static CellProcessorAdaptor decideAdaptor(Field field) { String fieldType = field.getType().getSimpleName().toUpperCase(Locale.ENGLISH); // if it is enum convert name to ENUM if(field.getType().getGenericSuperclass() != null) { if (field.getType().getGenericSuperclass().toString().startsWith("java.lang.Enum")) { fieldType = "ENUM"; return Parsers.valueOf(fieldType).getParser(field.getType()); } } if(fieldType.equals("DATE")){ if(field.getAnnotation(JsonFormat.class) != null){ String format = field.getAnnotation(JsonFormat.class).pattern(); return new ParseDate(format); }else{ throw new RuntimeException("Date type must have SimpleDateFormat annotation with a valid format."); } }else{ return Parsers.valueOf(fieldType).getParser(); } } }
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss") public Date getLastAccess() { return lastAccess; }
switch (format.shape()) { case ARRAY: return KnownJsonType.ARRAY;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ") public Date getLastModified() { return lastModified; }