Jackson ignore attribute on sub class that is 3:d party
Date : March 29 2020, 07:55 AM
wish helps you I believe you can achieve a solution by using custom serializers. You can add custom serializers through the ObjectMapper. I have created a small unit test below that demonstrates how it can be achieved: import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.module.SimpleModule;
import org.junit.Test;
import java.io.IOException;
public class JacksonSerializerTest {
@Test
public void test() throws Exception {
C c = new C("initially lowercase string in c");
B b = new B(c);
A a = new A(b);
SimpleModule module = new SimpleModule("MyCustomModule", new Version(1, 0, 0, null));
module.addSerializer(new CustomSerializerForC());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(a);
System.out.println(pretty);
}
public class A {
private B b;
public A(B b) {
this.b = b;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
public class B {
private C c;
public B(C c) {
this.c = c;
}
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
}
public class C {
private String value;
public C(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class CustomSerializerForC extends JsonSerializer<C> {
@Override
public Class<C> handledType() {
return C.class;
}
@Override
public void serialize(C c, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
String upperCase = c.getValue().toUpperCase();
jsonGenerator.writeString(upperCase);
}
}
}
|
Jackson-XML. How can I ignore an XML attribute?
Tag : java , By : Topher Cyll
Date : March 29 2020, 07:55 AM
around this issue I have the following XML file: , Not seeing a way, I would feel inclined to take the attribute too. public class Channel {
static class Number {
public String type; // transient too?
@JacksonXmlText
public Integer value;
public String toString() {
...
}
}
public String name;
public Number number;
@Override
public String toString() {
...
}
}
|
How to ignore attribute when mapping to json from a text file to a Java object using Jackson?
Tag : java , By : micaleel
Date : March 29 2020, 07:55 AM
This might help you I have a text file 'input.txt' which contains this text , try something like this. public class OuterClass{
@JsonProperty("product")
public Product product;
}
OuterClass outerObject = mapper.readValue(new File("input.txt"), OuterClass.class);
|
Make rbindlist skip, ignore or change class attribute of the column
Tag : r , By : Arun Thomas
Date : March 29 2020, 07:55 AM
This might help you I came up with this inelegant solution that bypasses the problem. Basically, What I am doing is to assign the attributes of the columns of the first item of the list to the columns with the same names of all the other items of the list. Keep in mind that this solution is problematic and, depending on the project, it could be a very wrong practice as it has the potential to mess up your data. However, if what you need is to use rbindlist to combine your dataframes, this makes the trick
dfs <- list(df1, df2)
varnames <- names(dfs[[1]]) # variable names
vattr <- purrr::map_chr(varnames, ~class(dfs[[1]][[.x]])) # variable attributes
for (i in seq_along(dfs)) {
# assign the same attributes of list 1 to the rest of the lists
for (j in seq_along(varnames)) {
if (varnames[[j]] %in% names(dfs[[i]])) {
class(dfs[[i]][[varnames[[j]]]]) <- vattr[[j]]
}
}
}
df_merged <- data.table::rbindlist(dfs, fill=TRUE, use.names=TRUE)
|
XSLT: Ignore and skip data for an element and its entire branch of child elements if attribute is set false?
Date : March 29 2020, 07:55 AM
|