public void testEquality() { assertTrue(EMPTY.equals(EMPTY)); assertTrue(new JsonFormat.Value().equals(new JsonFormat.Value())); JsonFormat.Value v1 = JsonFormat.Value.forShape(Shape.BOOLEAN); JsonFormat.Value v2 = JsonFormat.Value.forShape(Shape.BOOLEAN); JsonFormat.Value v3 = JsonFormat.Value.forShape(Shape.SCALAR); assertTrue(v1.equals(v2)); assertTrue(v2.equals(v1)); assertFalse(v1.equals(v3)); assertFalse(v3.equals(v1)); assertFalse(v2.equals(v3)); assertFalse(v3.equals(v2)); // not strictly guaranteed but... assertFalse(v1.hashCode() == v3.hashCode()); // then let's converge assertEquals(v1, v3.withShape(Shape.BOOLEAN)); assertFalse(v1.equals(v1.withPattern("ZBC"))); assertFalse(v1.equals(v1.withFeature(Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY))); assertFalse(v1.equals(v1.withoutFeature(Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY))); }
protected boolean useTimestamp(SerializerProvider provider) { if (_useTimestamp != null) { return _useTimestamp.booleanValue(); } if (_shape != null) { if (_shape == Shape.STRING) { return false; } if (_shape == Shape.NUMBER_INT) { return true; } } // assume that explicit formatter definition implies use of textual format if (_formatter != null) { return false; } return provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); }
protected static Boolean _isShapeWrittenUsingIndex(Class<?> paramClass, JsonFormat.Value paramValue, boolean paramBoolean) { JsonFormat.Shape localShape1; if (paramValue == null) localShape1 = null; else localShape1 = paramValue.getShape(); JsonFormat.Shape localShape2 = localShape1; if (localShape1 == null) return null; if ((localShape2 == JsonFormat.Shape.ANY) || (localShape2 == JsonFormat.Shape.SCALAR)) return null; if (localShape2 == JsonFormat.Shape.STRING) return Boolean.FALSE; if (localShape2.isNumeric()) return Boolean.TRUE; StringBuilder localStringBuilder = new StringBuilder("Unsupported serialization shape (").append(localShape2).append(") for Enum ").append(paramClass.getName()).append(", not supported as "); String str; if (paramBoolean) str = "class"; else str = "property"; throw new IllegalArgumentException(str + " annotation"); }
protected JsonSerializer<?> buildEnumSerializer(SerializationConfig paramSerializationConfig, JavaType paramJavaType, BeanDescription paramBeanDescription) { JsonFormat.Value localValue = paramBeanDescription.findExpectedFormat(null); if ((localValue != null) && (localValue.getShape() == JsonFormat.Shape.OBJECT)) { ((BasicBeanDescription)paramBeanDescription).removeProperty("declaringClass"); return null; } Object localObject = EnumSerializer.construct(paramJavaType.getRawClass(), paramSerializationConfig, paramBeanDescription, localValue); if (this._factoryConfig.hasSerializerModifiers()) { Iterator localIterator = this._factoryConfig.serializerModifiers().iterator(); while (localIterator.hasNext()) localObject = ((BeanSerializerModifier)localIterator.next()).modifyEnumSerializer(paramSerializationConfig, paramJavaType, paramBeanDescription, (JsonSerializer)localObject); } return localObject; }
/** * 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 ); } } }
/** * Helper method called to check whether */ protected static Boolean _isShapeWrittenUsingIndex(Class<?> enumClass, JsonFormat.Value format, boolean fromClass) { JsonFormat.Shape shape = (format == null) ? null : format.getShape(); if (shape == null) { return null; } if (shape == Shape.ANY || shape == Shape.SCALAR) { // i.e. "default", check dynamically return null; } if (shape == Shape.STRING) { return Boolean.FALSE; } if (shape.isNumeric()) { return Boolean.TRUE; } throw new IllegalArgumentException("Unsupported serialization shape ("+shape+") for Enum "+enumClass.getName() +", not supported as " + (fromClass? "class" : "property") +" annotation"); }
public void testFromAnnotation() { JsonFormat ann = Bogus.class.getAnnotation(JsonFormat.class); JsonFormat.Value v = JsonFormat.Value.from(ann); assertEquals("xyz", v.getPattern()); assertEquals(JsonFormat.Shape.BOOLEAN, v.getShape()); // note: since it's not valid, should not try access as real thing assertEquals("bogus", v.timeZoneAsString()); // also: assertSame(EMPTY, JsonFormat.Value.from(null)); }
public void testShape() { assertFalse(JsonFormat.Shape.STRING.isNumeric()); assertFalse(JsonFormat.Shape.STRING.isStructured()); assertTrue(JsonFormat.Shape.NUMBER_INT.isNumeric()); assertTrue(JsonFormat.Shape.NUMBER_FLOAT.isNumeric()); assertTrue(JsonFormat.Shape.NUMBER.isNumeric()); assertTrue(JsonFormat.Shape.ARRAY.isStructured()); assertTrue(JsonFormat.Shape.OBJECT.isStructured()); }
@Override protected void doSerialize(JsonWriter writer, Date value, JsonSerializationContext ctx, JsonSerializerParameters params) { if ((ctx.isWriteDatesAsTimestamps() || params.getShape().isNumeric()) && params.getShape() != Shape.STRING) { writer.value(value.getTime()); } else { String date = JacksonContextProvider.get().dateFormat().format(params, value); if (null == params.getPattern()) { writer.unescapeValue(date); } else { writer.value(date); } } }
protected JSR310FormattedSerializerBase(JSR310FormattedSerializerBase<?> base, Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) { super(base.handledType()); _useTimestamp = useTimestamp; _formatter = dtf; _shape = shape; }
public BeanSerializerBase(JavaType paramJavaType, BeanSerializerBuilder paramBeanSerializerBuilder, BeanPropertyWriter[] paramArrayOfBeanPropertyWriter1, BeanPropertyWriter[] paramArrayOfBeanPropertyWriter2) { super(paramJavaType); this._props = paramArrayOfBeanPropertyWriter1; this._filteredProps = paramArrayOfBeanPropertyWriter2; BeanSerializerBase localBeanSerializerBase; JsonFormat.Shape localShape; if (paramBeanSerializerBuilder == null) { this._typeId = null; this._anyGetterWriter = null; this._propertyFilterId = null; this._objectIdWriter = null; localBeanSerializerBase = this; localShape = null; } else { this._typeId = paramBeanSerializerBuilder.getTypeId(); this._anyGetterWriter = paramBeanSerializerBuilder.getAnyGetter(); this._propertyFilterId = paramBeanSerializerBuilder.getFilterId(); this._objectIdWriter = paramBeanSerializerBuilder.getObjectIdWriter(); JsonFormat.Value localValue = paramBeanSerializerBuilder.getBeanDescription().findExpectedFormat(null); localBeanSerializerBase = this; if (localValue == null) localShape = null; else localShape = localValue.getShape(); } localBeanSerializerBase._serializationShape = localShape; }
protected final JsonDeserializer<?> _createDeserializer2(DeserializationContext paramDeserializationContext, DeserializerFactory paramDeserializerFactory, JavaType paramJavaType, BeanDescription paramBeanDescription) { DeserializationConfig localDeserializationConfig = paramDeserializationContext.getConfig(); if (paramJavaType.isEnumType()) return paramDeserializerFactory.createEnumDeserializer(paramDeserializationContext, paramJavaType, paramBeanDescription); if (paramJavaType.isContainerType()) { if (paramJavaType.isArrayType()) return paramDeserializerFactory.createArrayDeserializer(paramDeserializationContext, (ArrayType)paramJavaType, paramBeanDescription); if (paramJavaType.isMapLikeType()) { MapLikeType localMapLikeType = (MapLikeType)paramJavaType; if (localMapLikeType.isTrueMapType()) return paramDeserializerFactory.createMapDeserializer(paramDeserializationContext, (MapType)localMapLikeType, paramBeanDescription); return paramDeserializerFactory.createMapLikeDeserializer(paramDeserializationContext, localMapLikeType, paramBeanDescription); } if (paramJavaType.isCollectionLikeType()) { JsonFormat.Value localValue = paramBeanDescription.findExpectedFormat(null); if ((localValue == null) || (localValue.getShape() != JsonFormat.Shape.OBJECT)) { CollectionLikeType localCollectionLikeType = (CollectionLikeType)paramJavaType; if (localCollectionLikeType.isTrueCollectionType()) return paramDeserializerFactory.createCollectionDeserializer(paramDeserializationContext, (CollectionType)localCollectionLikeType, paramBeanDescription); return paramDeserializerFactory.createCollectionLikeDeserializer(paramDeserializationContext, localCollectionLikeType, paramBeanDescription); } } } if (JsonNode.class.isAssignableFrom(paramJavaType.getRawClass())) return paramDeserializerFactory.createTreeDeserializer(localDeserializationConfig, paramJavaType, paramBeanDescription); return paramDeserializerFactory.createBeanDeserializer(paramDeserializationContext, paramJavaType, paramBeanDescription); }
protected BeanDeserializerBase(BeanDeserializerBuilder paramBeanDeserializerBuilder, BeanDescription paramBeanDescription, BeanPropertyMap paramBeanPropertyMap, Map<String, SettableBeanProperty> paramMap, HashSet<String> paramHashSet, boolean paramBoolean1, boolean paramBoolean2) { super(paramBeanDescription.getType()); this._classAnnotations = paramBeanDescription.getClassInfo().getAnnotations(); this._beanType = paramBeanDescription.getType(); this._valueInstantiator = paramBeanDeserializerBuilder.getValueInstantiator(); this._beanProperties = paramBeanPropertyMap; this._backRefs = paramMap; this._ignorableProps = paramHashSet; this._ignoreAllUnknown = paramBoolean1; this._anySetter = paramBeanDeserializerBuilder.getAnySetter(); List localList = paramBeanDeserializerBuilder.getInjectables(); ValueInjector[] arrayOfValueInjector; if ((localList == null) || (localList.isEmpty())) arrayOfValueInjector = null; else arrayOfValueInjector = (ValueInjector[])localList.toArray(new ValueInjector[localList.size()]); this._injectables = arrayOfValueInjector; this._objectIdReader = paramBeanDeserializerBuilder.getObjectIdReader(); boolean bool1; if ((this._unwrappedPropertyHandler != null) || (this._valueInstantiator.canCreateUsingDelegate()) || (this._valueInstantiator.canCreateFromObjectWith()) || (!this._valueInstantiator.canCreateUsingDefault())) bool1 = true; else bool1 = false; this._nonStandardCreation = bool1; JsonFormat.Value localValue = paramBeanDescription.findExpectedFormat(null); JsonFormat.Shape localShape; if (localValue == null) localShape = null; else localShape = localValue.getShape(); this._serializationShape = localShape; this._needViewProcesing = paramBoolean2; boolean bool2; if ((!this._nonStandardCreation) && (this._injectables == null) && (!this._needViewProcesing) && (this._objectIdReader != null)) bool2 = true; else bool2 = false; this._vanillaProcessing = bool2; }
@Override protected void doSerialize( JsonWriter writer, Date value, JsonSerializationContext ctx, JsonSerializerParameters params ) { if ( (ctx.isWriteDatesAsTimestamps() || params.getShape().isNumeric()) && params.getShape() != Shape.STRING ) { writer.value( value.getTime() ); } else { String date = DateFormat.format( params, value ); if ( null == params.getPattern() ) { writer.unescapeValue( date ); } else { writer.value( date ); } } }
public void testToString() { assertEquals("JsonFormat.Value(pattern=null,shape=STRING,lenient=null,locale=null,timezone=null)", JsonFormat.Value.forShape(JsonFormat.Shape.STRING).toString()); assertEquals("JsonFormat.Value(pattern=[.],shape=ANY,lenient=null,locale=null,timezone=null)", JsonFormat.Value.forPattern("[.]").toString()); }
public void testSimpleMerge() { // Start with an empty instance assertFalse(EMPTY.hasLocale()); assertFalse(EMPTY.hasPattern()); assertFalse(EMPTY.hasShape()); assertFalse(EMPTY.hasTimeZone()); assertNull(EMPTY.getLocale()); // then with a non-empty one final String TEST_PATTERN = "format-string"; // not parsed, usage varies JsonFormat.Value v = JsonFormat.Value.forPattern(TEST_PATTERN); assertTrue(v.hasPattern()); assertEquals(TEST_PATTERN, v.getPattern()); assertFalse(v.hasLocale()); assertFalse(v.hasShape()); assertFalse(v.hasTimeZone()); // and ensure nothing overridden with empty JsonFormat.Value merged = v.withOverrides(EMPTY); assertEquals(TEST_PATTERN, merged.getPattern()); assertFalse(merged.hasLocale()); assertFalse(merged.hasShape()); assertFalse(merged.hasTimeZone()); // minor optimization: overriding with itself has no effect assertSame(merged, merged.withOverrides(merged)); // but that empty is overridden merged = JsonFormat.Value.merge(EMPTY, v); assertEquals(TEST_PATTERN, merged.getPattern()); assertFalse(merged.hasLocale()); assertFalse(merged.hasShape()); assertFalse(merged.hasTimeZone()); // also some shortcuts: assertSame(merged, merged.withOverrides(null)); // then with some other combinations final JsonFormat.Shape TEST_SHAPE = JsonFormat.Shape.NUMBER; JsonFormat.Value v2 = JsonFormat.Value.forShape(TEST_SHAPE); merged = v.withOverrides(v2); assertEquals(TEST_PATTERN, merged.getPattern()); assertFalse(merged.hasLocale()); assertEquals(TEST_SHAPE, merged.getShape()); assertFalse(merged.hasTimeZone()); merged = v2.withOverrides(v); assertEquals(TEST_PATTERN, merged.getPattern()); assertFalse(merged.hasLocale()); assertEquals(TEST_SHAPE, merged.getShape()); assertFalse(merged.hasTimeZone()); }
protected abstract JSR310FormattedSerializerBase<?> withFormat(Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape);
@Override public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException { JsonFormat.Value format = findFormatOverrides(prov, property, handledType()); if (format != null) { Boolean useTimestamp = null; // Simple case first: serialize as numeric timestamp? JsonFormat.Shape shape = format.getShape(); if (shape == JsonFormat.Shape.ARRAY || shape.isNumeric() ) { useTimestamp = Boolean.TRUE; } else { useTimestamp = (shape == JsonFormat.Shape.STRING) ? Boolean.FALSE : null; } DateTimeFormatter dtf = _formatter; // If not, do we have a pattern? if (format.hasPattern()) { final String pattern = format.getPattern(); final Locale locale = format.hasLocale() ? format.getLocale() : prov.getLocale(); if (locale == null) { dtf = DateTimeFormatter.ofPattern(pattern); } else { dtf = DateTimeFormatter.ofPattern(pattern, locale); } //Issue #69: For instant serializers/deserializers we need to configure the formatter with //a time zone picked up from JsonFormat annotation, otherwise serialization might not work if (format.hasTimeZone()) { dtf = dtf.withZone(format.getTimeZone().toZoneId()); } } JSR310FormattedSerializerBase<?> ser = this; if ((shape != _shape) || (useTimestamp != _useTimestamp) || (dtf != _formatter)) { ser = ser.withFormat(useTimestamp, dtf, shape); } Boolean writeZoneId = format.getFeature(JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID); if (writeZoneId != null) { ser = ser.withFeatures(writeZoneId); } return ser; } return this; }
protected JsonSerializer<?> buildCollectionSerializer(SerializationConfig paramSerializationConfig, CollectionType paramCollectionType, BeanDescription paramBeanDescription, boolean paramBoolean, TypeSerializer paramTypeSerializer, JsonSerializer<Object> paramJsonSerializer) { Object localObject = null; Iterator localIterator1 = customSerializers().iterator(); JsonSerializer localJsonSerializer; do { if (!localIterator1.hasNext()) break; localJsonSerializer = ((Serializers)localIterator1.next()).findCollectionSerializer(paramSerializationConfig, paramCollectionType, paramBeanDescription, paramTypeSerializer, paramJsonSerializer); localObject = localJsonSerializer; } while (localJsonSerializer == null); if (localObject == null) { JsonFormat.Value localValue = paramBeanDescription.findExpectedFormat(null); if ((localValue != null) && (localValue.getShape() == JsonFormat.Shape.OBJECT)) return null; Class localClass1 = paramCollectionType.getRawClass(); if (EnumSet.class.isAssignableFrom(localClass1)) { JavaType localJavaType1 = paramCollectionType.getContentType(); JavaType localJavaType2 = localJavaType1; if (!localJavaType1.isEnumType()) localJavaType2 = null; localObject = StdContainerSerializers.enumSetSerializer(localJavaType2); } else { Class localClass2 = paramCollectionType.getContentType().getRawClass(); if (isIndexedList(localClass1)) { if (localClass2 == String.class) { if ((paramJsonSerializer == null) || (ClassUtil.isJacksonStdImpl(paramJsonSerializer))) localObject = IndexedStringListSerializer.instance; } else localObject = StdContainerSerializers.indexedListSerializer(paramCollectionType.getContentType(), paramBoolean, paramTypeSerializer, paramJsonSerializer); } else if ((localClass2 == String.class) && ((paramJsonSerializer == null) || (ClassUtil.isJacksonStdImpl(paramJsonSerializer)))) localObject = StringCollectionSerializer.instance; if (localObject == null) localObject = StdContainerSerializers.collectionSerializer(paramCollectionType.getContentType(), paramBoolean, paramTypeSerializer, paramJsonSerializer); } } if (this._factoryConfig.hasSerializerModifiers()) { Iterator localIterator2 = this._factoryConfig.serializerModifiers().iterator(); while (localIterator2.hasNext()) localObject = ((BeanSerializerModifier)localIterator2.next()).modifyCollectionSerializer(paramSerializationConfig, paramCollectionType, paramBeanDescription, (JsonSerializer)localObject); } return localObject; }
public JsonDeserializer<?> createContextual(DeserializationContext paramDeserializationContext, BeanProperty paramBeanProperty) { ObjectIdReader localObjectIdReader = this._objectIdReader; AnnotationIntrospector localAnnotationIntrospector = paramDeserializationContext.getAnnotationIntrospector(); AnnotatedMember localAnnotatedMember; if ((paramBeanProperty == null) || (localAnnotationIntrospector == null)) localAnnotatedMember = null; else localAnnotatedMember = paramBeanProperty.getMember(); String[] arrayOfString = null; if (paramBeanProperty != null) { arrayOfString = null; if (localAnnotationIntrospector != null) { arrayOfString = localAnnotationIntrospector.findPropertiesToIgnore(localAnnotatedMember); ObjectIdInfo localObjectIdInfo1 = localAnnotationIntrospector.findObjectIdInfo(localAnnotatedMember); if (localObjectIdInfo1 != null) { ObjectIdInfo localObjectIdInfo2 = localAnnotationIntrospector.findObjectReferenceInfo(localAnnotatedMember, localObjectIdInfo1); Class localClass = localObjectIdInfo2.getGeneratorType(); SettableBeanProperty localSettableBeanProperty1; JavaType localJavaType2; Object localObject; if (localClass == ObjectIdGenerators.PropertyGenerator.class) { String str = localObjectIdInfo2.getPropertyName(); SettableBeanProperty localSettableBeanProperty2 = findProperty(str); localSettableBeanProperty1 = localSettableBeanProperty2; if (localSettableBeanProperty2 == null) throw new IllegalArgumentException("Invalid Object Id definition for " + getBeanClass().getName() + ": can not find property with name '" + str + "'"); localJavaType2 = localSettableBeanProperty1.getType(); localObject = new PropertyBasedObjectIdGenerator(localObjectIdInfo2.getScope()); } else { JavaType localJavaType1 = paramDeserializationContext.constructType(localClass); localJavaType2 = paramDeserializationContext.getTypeFactory().findTypeParameters(localJavaType1, ObjectIdGenerator.class)[0]; localSettableBeanProperty1 = null; localObject = paramDeserializationContext.objectIdGeneratorInstance(localAnnotatedMember, localObjectIdInfo2); } JsonDeserializer localJsonDeserializer = paramDeserializationContext.findRootValueDeserializer(localJavaType2); localObjectIdReader = ObjectIdReader.construct(localJavaType2, localObjectIdInfo2.getPropertyName(), (ObjectIdGenerator)localObject, localJsonDeserializer, localSettableBeanProperty1); } } } BeanDeserializerBase localBeanDeserializerBase = this; if ((localObjectIdReader != null) && (localObjectIdReader != this._objectIdReader)) localBeanDeserializerBase = localBeanDeserializerBase.withObjectIdReader(localObjectIdReader); if ((arrayOfString != null) && (arrayOfString.length != 0)) localBeanDeserializerBase = localBeanDeserializerBase.withIgnorableProperties(ArrayBuilders.setAndArray(localBeanDeserializerBase._ignorableProps, arrayOfString)); JsonFormat.Shape localShape = null; if (localAnnotatedMember != null) { JsonFormat.Value localValue = localAnnotationIntrospector.findFormat(localAnnotatedMember); localShape = null; if (localValue != null) localShape = localValue.getShape(); } if (localShape == null) localShape = this._serializationShape; if (localShape == JsonFormat.Shape.ARRAY) localBeanDeserializerBase = localBeanDeserializerBase.asArrayDeserializer(); return localBeanDeserializerBase; }
public FormatDateBean( @JsonProperty( "dateInParameter" ) @JsonFormat( shape = Shape.STRING, pattern = "'P'yyyy/MM/dd" ) Date dateParameter ) { this.dateInParameter = dateParameter; }
/** * <p>Getter for the field <code>shape</code>.</p> * * @return a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object. */ @Override public Shape getShape() { return shape; }
/** * <p>Setter for the field <code>shape</code>.</p> * * @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object. * @return a {@link JsonSerializerParameters} object. */ @Override public JsonSerializerParameters setShape(Shape shape) { this.shape = shape; return this; }
/** * <p>Setter for the field <code>shape</code>.</p> * * @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object. * @return a {@link JsonDeserializerParameters} object. */ @Override public JsonDeserializerParameters setShape(Shape shape) { this.shape = shape; return this; }
/** * <p>Getter for the field <code>shape</code>.</p> * * @return a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object. */ public Shape getShape() { return shape; }
/** * <p>Setter for the field <code>shape</code>.</p> * * @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object. * @return a {@link com.github.nmorel.gwtjackson.client.JsonDeserializerParameters} object. */ public JsonDeserializerParameters setShape( Shape shape ) { this.shape = shape; return this; }
/** * <p>Setter for the field <code>shape</code>.</p> * * @param shape a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} object. * @return a {@link com.github.nmorel.gwtjackson.client.JsonSerializerParameters} object. */ public JsonSerializerParameters setShape( Shape shape ) { this.shape = shape; return this; }