Creating a new Java object automatically with correct data values


Creating a new Java object automatically with correct data values



I'm wondering is there anyway in Java to create an object from a simple POJO automatically with correct data values?



Lets say i have a simple POJO like this:


public class POJO {

private int intVar;
private boolean booleanVar;
private String stringVar;

public POJO(int intVar, boolean booleanVar, String stringVar) {
this.intVar = intVar;
this.booleanVar = booleanVar;
this.stringVar = stringVar;
}

//Geteru & Setteru...
}



So basically I want to know can i create an object from this class with some sorta method or something that would read the variables and their types and assign them with ANY values but the value would be correct for the variable data type. For example intVar should get ofcourse an integer and stringVar should get a String etc.



This does not have to be the way to do this, what I am actually trying to achieve is im trying to automatically create a JSON of these POJO's I have, but the thing is i need some values for the JSON. I have already used gson for creating JSON's of my POJO's but i have no clue how can i automatically assign "correct" values for the variables. It doesn't have to happen by creating the new object, all I need is to get a JSON with variable values created from ANY POJO like this.



For example:


{
intVar=1,
booleanVar=true,
stringVar="my string"
}



The reason I am asking can I automatically create a new object with correct values for variable from a simple POJO like this, is that i would then be able to create the satisfying JSON from it, but if anyone know any alternate solutions for getting the values to the JSON, feel free to suggest them :).





have a look at the jackson library. It does exactly what you want
– Lino
2 days ago





What do you mean with 'any values': Do you need a set of different or random values? If you only need one set of valid values, you can just use a default constructor: then int=0, boolean=false, etc.
– TmTron
2 days ago





I mean that, lets say I have 2 POJO's the other one has an int, boolean and a string and the other has lets say a Double and a float. I need a method or something to create objects of these classes or convert them straight into JSON but the variables MUST have values. The value doesn't matter, it can be the same for all the different variables, for example if a POJO would have 2 integers, they can be both for example 1. But they NEED to get correct values
– Clx3
2 days ago





Sorry, I mean they need to have CORRECT data type but the value doesnt matter, for example a String must get a String value but the value of the String doesn't matter because the same method would be used for example lets say a hundred POJO's.
– Clx3
2 days ago





Then a default constructor should work: At least for the primitives and strings in your example.
– TmTron
2 days ago




3 Answers
3



From my opinion you need to try jackson or GSON libraries. From my experience, jackson better.



I can't write 100% working way for implementation, because in my experience I never do same things. I always convert JSON formatted data into known class (it come to me as mapped object as argument in spring controller). Have no idea, why you need to map abstract class, possibly it is wrong design, possibly not.



Using jackson you will be able to use inheritance:
http://www.baeldung.com/jackson-inheritance, but I never try it before.



If it will not satisfy your needs, you may use this:


Class candidates = new Class{InheritedClass1.class, InheritedClass2.class, InheritedClass3.class};
ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{"your":"json"}";
AbstractClass instance = null;
for(Class clazz : candidates) {
try{
instance = mapper.readValue(jsonInString, clazz);
} catch (Exception e){
}
if(instance !== null) {
break;
}
}
System.out.println(instance);





This is kinda something what i was looking for but not yet there. But hey, I cannot expect someone else to do it all for me! I'll continue from this myself but you got me started, Thanks!
– Clx3
2 days ago



try to implement Serializable to the model.
Then you can change json properties name if you want it with @SeriliazedName("stringVar")



there is an example:


public class POJO implements Serializable{

@SerializedName("intVar")
private int intVar ;
@SerializedName("stringVar")
private String stringVar;



When your class only has primitives and strings (as in your example), you can use the default constructor. The fields will be initialized with the default values for the corresponding data-type:


public class Pojo {
int intVar;
boolean booleanVar;
String stringVar;
}

Pojo pojo = new Pojo();
// pojo.intVar is now 0
// pojo.booleanVar is now false
// pojo.string Var is now ""





I just noticed my POJO's or my entities that I'm working with, are all abstract so I cannot initialize them. Need to find another way :C. They all have DTO's made of them but I tried creating a new object of the DTO class but it seems not to initialize the variables.
– Clx3
2 days ago







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV