Get POJO Object from JSONPath
Page 1 of 1
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Tue, 20th Nov 2018 11:52    Post subject: Get POJO Object from JSONPath
Let's say I have a simple POJO
Code:
public class Human() {
   private List<Limb> limbs;
   private HashMap<String, String> basicProperties;
   private Date birthday;
   private String name;

   public static class Limb {
      private String name;
      private long length;
      private String whatever;

      ... Getters and Setters;
   }
   
   ... Getters and Setters;
}


Now I can set and change values programatically by specifying f.e.:
Scenario 1: Setting the name of the human to dude
Code:
human.setName("dude");

Scenario 2: Setting the name of the limb with the name of "bla" to "blabla"
Code:
for(Limb limb: human.getLimbs())
   if (limb.getName().equals("bla")){
      limb.setName("blabla");
      break;
   }
      

I'd rather use a jsonpath though to set the values of my pojo.

So the call should rather look like uh...

Scenario 1:
Code:
setPOJOValue(human, "name", "dude");

Scenario 2:
Code:
setPOJOValue(human, "limbs[name='bla']", "blabla");

or...
Scenario 1:
Code:
human.setWithJson("name", "dude")

Scenario 2:
Code:
human.setWithJson("limbs[name='bla']", "blabla");

Any idea which library to use and how? I already have Jackson available. I'd rather not modify my POJO though and would like to use a function like my fictious setPOJOValue.

Thanks!


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Areius




Posts: 14707

PostPosted: Tue, 20th Nov 2018 13:45    Post subject:
Pfft, it's been a while since I've used Java. Perhaps https://github.com/google/gson can do what you want? Does it need to be in Java? I dislike Java so much Razz
In C# you can use Json.NET and set that up in a few LOC.


PC: Yes. Console: No.
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Tue, 20th Nov 2018 14:16    Post subject:
Jupp, has to be java. gson seems to have that but only to find the correct object in a passed json. So I would have to convert my pojo into json and then use gson to find the correct position, but I want to update the original java object... so that is not an option Sad


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group