Search in sources :

Example 1 with JsonPropertyOrder

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonPropertyOrder in project webanno by webanno.

the class BeanAsArraySerializer method serializeContents.

@Override
public void serializeContents(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    JsonPropertyOrder order = value.getClass().getAnnotation(JsonPropertyOrder.class);
    String[] propOrder = (order == null) ? null : order.value();
    if (propOrder == null) {
        throw new IllegalStateException("Bean must declare JsonPropertyOrder!");
    }
    if (propOrder.length == 0) {
        return;
    }
    int i = 0;
    try {
        do {
            Field field = value.getClass().getDeclaredField(propOrder[i]);
            ReflectionUtils.makeAccessible(field);
            Object elem = field.get(value);
            if (elem == null) {
                provider.defaultSerializeNull(jgen);
            } else {
                Class<?> cc = elem.getClass();
                JsonSerializer<Object> serializer = provider.findValueSerializer(cc, null);
                serializer.serialize(elem, jgen, provider);
            }
            ++i;
        } while (i < propOrder.length);
    } catch (Exception e) {
        // [JACKSON-55] Need to add reference information
        wrapAndThrow(provider, e, value, i);
    }
}
Also used : Field(java.lang.reflect.Field) JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder) IOException(java.io.IOException)

Example 2 with JsonPropertyOrder

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonPropertyOrder in project crnk-framework by crnk-project.

the class DefaultResourceInformationProvider method build.

public ResourceInformation build(Class<?> resourceClass, boolean allowNonResourceBaseClass) {
    List<ResourceField> resourceFields = getResourceFields(resourceClass);
    String resourceType = getResourceType(resourceClass, allowNonResourceBaseClass);
    Optional<JsonPropertyOrder> propertyOrder = ClassUtils.getAnnotation(resourceClass, JsonPropertyOrder.class);
    if (propertyOrder.isPresent()) {
        JsonPropertyOrder propertyOrderAnnotation = propertyOrder.get();
        Collections.sort(resourceFields, new FieldOrderedComparator(propertyOrderAnnotation.value(), propertyOrderAnnotation.alphabetic()));
    }
    DefaultResourceInstanceBuilder<?> instanceBuilder = new DefaultResourceInstanceBuilder(resourceClass);
    Class<?> superclass = resourceClass.getSuperclass();
    String superResourceType = superclass != Object.class && context.accept(superclass) ? context.getResourceType(superclass) : null;
    ResourceInformation information = new ResourceInformation(context.getTypeParser(), resourceClass, resourceType, superResourceType, instanceBuilder, resourceFields, pagingBehaviors.get(ClassUtils.getAnnotation(resourceClass, JsonApiResource.class).get().pagingBehavior()));
    if (!allowNonResourceBaseClass && information.getIdField() == null) {
        throw new ResourceIdNotFoundException(resourceClass.getCanonicalName());
    }
    return information;
}
Also used : FieldOrderedComparator(io.crnk.core.engine.internal.utils.FieldOrderedComparator) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder) ResourceIdNotFoundException(io.crnk.core.exception.ResourceIdNotFoundException)

Example 3 with JsonPropertyOrder

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonPropertyOrder in project logging-log4j2 by apache.

the class XmlLayoutTest method checkJsonPropertyOrder.

private void checkJsonPropertyOrder(final boolean includeContextStack, final boolean includeContextMap, final boolean includeStacktrace, final String str) {
    final JsonPropertyOrder annotation = AbstractLogEventXmlMixIn.class.getAnnotation(JsonPropertyOrder.class);
    Assert.assertNotNull(annotation);
    int previousIndex = 0;
    String previousName = null;
    for (final String name : annotation.value()) {
        final int currentIndex = str.indexOf(name);
        if (!includeContextStack && XmlConstants.ELT_CONTEXT_STACK.equals(name)) {
            Assert.assertTrue(String.format("Unexpected element '%s' in: %s", name, str), currentIndex == NOT_FOUND);
            break;
        }
        if (!includeContextMap && XmlConstants.ELT_CONTEXT_MAP.equals(name)) {
            Assert.assertTrue(String.format("Unexpected element '%s' in: %s", name, str), currentIndex == NOT_FOUND);
            break;
        }
        if (!includeStacktrace && XmlConstants.ELT_EXTENDED_STACK_TRACE.equals(name)) {
            Assert.assertTrue(String.format("Unexpected element '%s' in: %s", name, str), currentIndex == NOT_FOUND);
            break;
        }
        if (!includeStacktrace && XmlConstants.ELT_EXTENDED_STACK_TRACE_ITEM.equals(name)) {
            Assert.assertTrue(String.format("Unexpected element '%s' in: %s", name, str), currentIndex == NOT_FOUND);
            break;
        }
        // TODO
        // Bug: The method
        // com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._sortProperties(Map<String,
        // POJOPropertyBuilder>) messes up the order defined in AbstractXmlLogEventMixIn's JsonPropertyOrder
        // annotations.
        // Assert.assertTrue(String.format("name='%s', previousIndex=%,d, previousName='%s', currentIndex=%,d: %s",
        // name, previousIndex, previousName, currentIndex, str), previousIndex < currentIndex);
        previousIndex = currentIndex;
        previousName = name;
    }
}
Also used : JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder)

Example 4 with JsonPropertyOrder

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonPropertyOrder in project Gaffer by gchq.

the class JSONSerialisationTest method shouldHaveJsonPropertyAnnotation.

@Test
public void shouldHaveJsonPropertyAnnotation() {
    // Given
    final T op = getTestObject();
    // When
    final JsonPropertyOrder annotation = op.getClass().getAnnotation(JsonPropertyOrder.class);
    // Then
    assertTrue(null != annotation && annotation.alphabetic(), "Missing JsonPropertyOrder annotation on class. It should de defined and set to alphabetical." + op.getClass().getName());
}
Also used : JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder) Test(org.junit.jupiter.api.Test)

Aggregations

JsonPropertyOrder (com.fasterxml.jackson.annotation.JsonPropertyOrder)4 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 FieldOrderedComparator (io.crnk.core.engine.internal.utils.FieldOrderedComparator)1 ResourceIdNotFoundException (io.crnk.core.exception.ResourceIdNotFoundException)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Test (org.junit.jupiter.api.Test)1