I have two java beans as below:
public class Class1 {
private String field1;
private Object field2;
//getter and setter
}
public class Class2 {
private Map<String,Object> field;
//getter and setter
}
When the object gets serialized to Json, it looks like this:
Class1: When field2 is null
{
field1:"value"
}
Class2: when value of map is null
{
field:{"key":null}
}
My question is what is the difference between the two? why for Class1 it didn't include null field in json? How do I include null field in json for Class1? I have tried the following but did't work:
#JsonInclude(JsonInclude.Include.ALWAYS)
public class Class1 {
private String field1;
private Object field2;
//getter and setter
}
and even tried on field level:
public class Class1 {
private String field1;
#JsonInclude(JsonInclude.Include.ALWAYS)
private Object field2;
//getter and setter
}
I am using Jersey.
The following example is with jackson:
ObjectMapper mapper = new ObjectMapper();
Class1 class1 = new Main().new Class1();
System.out.println(mapper.writeValueAsString(class1));
and the output is:
{"field1":null,"field2":null}
Related
I am very new to java and I hope my question is not too stupid and has enough info for you guys to help me out.
I have a list of jsonNodes, each of them is in the following format:
{"field1":value1, "field2":value2, "field3":value3, "notneeded1":value4, "notneeded2":value5}
I am currently using a class like the following and converting it to list
#JsonIgnoreProperties(ignoreUnknown = true)
class customClass:
String field1;
String field2;
String field3;
Using TypeReference to convert the list of jsonNode to list of this class...
What I want to do is add a few of these fields within an another field
{"parentfield":{"field1":value1, "field2":value2}, "field3":value3}
How do I do this using this class?
AFIU you want this:
#JsonIgnoreProperties(ignoreUnknown = true)
class CustomClass {
public String field1;
public String field2;
public String field3;
public CustomClass2 parentfield;
}
#JsonIgnoreProperties(ignoreUnknown = true)
class CustomClass2 {
public String field1;
public String field2;
}
Then depends on your code, to set the values of the fields in an object of CustomClass2 with fields from an object of CustomClass.
I have four class
public class A {
private String field1;
private String field2;
private B b;
// setter and getter
}
public class B {
private String fieldB1;
private String fieldB2;
// setter and getter
}
public class ADto {
private String field1;
private String field2;
private BDto b;
// setter and getter
}
public class BDto {
private String fieldB1;
private String fieldB2;
// setter and getter
}
I want to use modelmapper to populate dto objects
ModelMapper modelMapper = new ModelMapper();
modelMapper.typeMap(A.class, ADto.class, "LAZY")
.addMappings(mapper -> mapper.skip(ADto::setB))
.addMappings(mapper -> mapper.skip(ADto::setField2));
modelMapper.map(a, ADto.class, "LAZY");
I use typeMapName to be able to have more mappers
The mapper correctly skips field2 but instead ignores the skip for B, what am I wrong?
Thanks
How to rename json object name with java annotation?
Object structure in java:
public class ParentClass {
private MyClass myClass;
}
public class MyClass {
private String name;
}
Json will have next view:
{
"myClass":{
"name":"value"
}
}
How can I change name of "myClass" using java/spring annotations, something like
#JsonObjectName("abc")
public class MyClass {
private String name;
}
and json will look like:
{
"abc":{
"name":"value"
}
}
Rename the variable:
private MyClass myClass;
To:
private MyClass abc;
This will yield the correct JSON-output without the use of annotations.
If you still want to use annotations and keep the name of the variable you can use #JsonProperty():
#JsonProperty("abc") // name of the property
private MyClass myClass;
#SerializedName("abc") is also possoble
It depends on the framework you are using. If you are using Jackson Library you can use:
public class ParentClass {
private MyClass myClass;
}
#JsonProperty("abc")
public class MyClass {
private String name;
}
If you are using Gson then
#SerializedName(value = "abc")
public class MyClass {
private String name;
}
Additionally in Gson if you want to use any alternate name for the field during deserialization we can use alternate as below:
#SerializedName(value = "abc", alternate ="xyz")
public class MyClass {
private String name;
}
alternate is to be used only at time of deserialization and GSON will only process/deserialize the last occurence of that field from JSON data.
I have a very basic OOP question. I have two classes as follows
public class Class1 {
private String field1;
private String field2;
private List<String> field3;
// constructors
}
public class Class2 {
private String field1;
private String field2;
private List<Object1> field3;
// constructors
}
The only reason i had to create class2 was because the List in class1 can either be a List of Strings or a List of Object2. Is there a way to et rid of Class2, and have field3 behave as a List of String and also as a List of Object2.
I know i can do something like
public class Class1 {
private String field1;
private String field2;
private List<String> field3;
private List<Object1> field4;
// constructors which are initializing null to either field 3 or field 4
}
But the above doesnt seem clean, as an object instantiated with either list should not have any reference in it of the other List.
Use generics:
public class Class1<TField3> {
private String field1;
private String field2;
private List<TField3> field3;
// constructors
}
Class1<String> stringObj = new Class1<>();
Say I have the following java classes (getters & setters omitted for brevity).
public class AllMyEvents {
private List<SomeEvent<?>> eventList;
}
public class SomeEvent<T> {
private long time;
#JsonProperty("event_type")
private String eventType;
#JsonProperty("event_data")
private T eventData;
}
public class BigEvent {
private List<SomeEvent<LittleEvent>> subEvents;
}
public class LittleEvent {
private long data;
}
When I call:
ObjectMapper om = new ObjectMapper();
AllMyEvents events = om.readValue(IOUtils.toString(jsonData, "UTF-8"),AllMyEvents.class);
The field eventData is of type LinkedHashMap. What I want is for this fields type to be specified by the eventType string. If the string is 'big' I want eventData to have type BigEvent or LittleEvent if the string is 'little'.
Is it possible to do this with Jackson annotations, or will I need to write a custom serializer/deserializer, or some other method? I'm using Jackson 1.9 if that is relevant.
Json Sub types is your answer.
#JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="#class")
#JsonSubTypes({
#JsonSubTypes.Type(value=BigEvent.class, name="big"),
#JsonSubTypes.Type(value=LittleEvent.class, name="little")
})
public class SomeEvent<T> {
private long time;
#JsonProperty("event_type")
private String eventType;
...
Also see: http://wiki.fasterxml.com/JacksonPolymorphicDeserialization