-2

Basically I am calling a Server and expect a JSON response. The complication is when I need to pass the parameter which is in the form of JSON. The initial outcome of my thought process was to have a file that has the JSON input and can be modified according to the individual commands.

This approach is cumbersome for the end user as it has to tweak many values in JSON, so the initial research fails.

Here I am in midst of either creating a wrapper that generates the JSON to be sent, around the current application which takes JSON as an input file. So the logic remains the same but the wrapper processes the input and creates a input file which is internally given to the existing application.

But again the problem lies in "How can you create Wrapper that creates the JSON". Isn't it the same old logic ?

My possible parameters expected by server changes with each command.

Any design pattern is suggested here ? Please let me know in case of further explanation is required.

BobDalgleish
  • 4,749
DevMac
  • 7
  • 2

1 Answers1

1

Honestly, I am having a tough time understanding what needs to be done. When you say the server needs to be passed a parameter that is in the form of JSON, what I think you mean is the server REST API expects you to POST a request to a URL with a JSON that has the "parameters". This is probably what you mean, and makes sense because that is how a lot of APIs work.

For example, a site that allows uploading user objects into its database via a REST API might have a URL http://myapp.mycompany.com/user/put and expects that URL to be called with an http POST with a JSON representation of a user object.

The best way to go about this is to find where you GET the object from that same API, because that will spit out a JSON example of an object. Use that JSON to generate a POJO. There are many automated ways of going about this, and a few sites that provide a generator that you upload the JSON and it gives back the POJOs which are structured to match the JSON format.

Then you use something like google gson (my preferecne, there are others) which can translate your POJO into a json object which you use in your http POST, or ingest into your app -- via the POJO you created to match the JSON.

This all sounds involved and time consuming, but really isn't.