How to use dynamic property names for a Json object

You can use JsonAnySetter JsonAnyGetter annotations. Behind you can use Map instance. In case you have always one-key-object you can use Collections.singletonMap in other case use HashMap or other implementation. Below example shows how easy you can use this approach and create as many random key-s as you want: import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.databind.ObjectMapper; … Read more

Wildfly 9 – How do I exclude Jackson

I just ran into this issue myself. After upgrading a library in my application, I received the following error on a request: Exception handling request to /path: java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonParser.hasToken(Lcom/fasterxml/jackson/core/JsonToken;) Here is how I solved it: I obviously had to exclude jackson-core-2.5.1 that wildfly-9 provides. I listed all modules that depend on ‘jackson-core’ with /opt/wildfly/modules# grep … Read more

How do I parametrize response parsing in Java?

You should separate JSON deserialisation from other parts of your app. You can not implement one method for all responses but you probably have a limited number of responses and you can declare some simple methods for each class. Generally, you could have only one method with declaration like below: public <T> T deserialise(String payload, … Read more

Android with Gradle (Java finished with non-zero exit value 2)

I think you should change your java JDK change jvm v8 for jdk7. This link can help you: Is it possible to use Java 8 for Android development? Other possible issue its dependency error, clean gradle before build. And change your jackson library for this: compile ‘com.fasterxml.jackson.core:jackson-databind:2.2.+’ compile ‘com.fasterxml.jackson.core:jackson-core:2.2.+’ compile ‘com.fasterxml.jackson.core:jackson-annotations:2.2.+’

include class name in all objects serialized by jackson

You need to annotated your type with the @JsonTypeInfo annotation and configure how the type information should be serialized. Refer this page for reference. Example: public class JacksonClassInfo { @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = “__class”) public static class Bean { public final String field; @JsonCreator public Bean(@JsonProperty(“field”) String field) { this.field = field; } @Override … Read more

Spring-MVC 406 Not Acceptable instead of JSON Response

Add the following in DispatcherServlet-servlet.xml. <bean id=”jacksonMessageConverter” class=”org.springframework.http.converter.json.MappingJacksonHttpMessageConverter”></bean> <bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”> <property name=”messageConverters”> <list> <ref bean=”jacksonMessageConverter”/> </list> </property> </bean>

Jackson custom annotation for custom value serialization

import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.ContextualSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.IOException; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; // Create own annotation storing your unit value @Target({ ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) @interface Unit { String value(); } // Create custom serializer checking @Unit annotation … Read more

How to use @JsonIdentityInfo with circular references?

It seems jackson-jr has a subset of Jackson’s features. @JsonIdentityInfo must not have made the cut. If you can use the full Jackson library, just use a standard ObjectMapper with the @JsonIdentityInfo annotation you suggested in your question and serialize your object. For example @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property=”@id”) public class A {/* all that good stuff */} … Read more