This actually sounds good to have one POJO defined for each of the input 
JSON that is being dealt and that's what the way I was thinking but was not 
able to map nested json elements to shallow (class level) pojo 
properties(To avoid multiple POJOs for nested json objects). Well now that 
I've this elastic library at hand I can have one POJO defined for each 
input json and can use polymorphic deserialization then.

Thanks a lot for your inputs Tatu..!:)

On Tuesday, September 12, 2017 at 4:03:34 AM UTC+5:30, Tatu Saloranta wrote:
>
> On Sun, Sep 10, 2017 at 12:19 PM, chetan choulwar <[email protected] 
> <javascript:>> wrote: 
> > Okay..Let me help you with one real time example. Consider for an 
> example 
> > different tv channels have exposed their APIs to get list of programs 
> > telecast-ed on them. And now suppose I'm trying to expose one APIs that 
> will 
> > accept channel name and provide the list of programs for that particular 
> > channel by calling it from my API and converting the response received 
> in my 
> > own format (a common one). The kind of data I'm getting from different 
> APIs 
> > is same but their representation is somewhat different, and what I want 
> to 
> > do is to convert these responses into a common format defined by my API 
> and 
> > send it as a response from my API. 
> > So now that I'm sure that I'm dealing with same kinda data represented 
> > somewhat different, is it okay to convert it into common format? 
>
> Yes, but I don't think you should try to use just one single Java 
> class for all of that. 
>
> It makes sense to use one class for representing output to your users, 
> but I think that if input structures, names vary, 
> then POJOs to map them to should be different too. You may reuse some 
> of those for "similar enough" inputs, 
> but there are limits to how far this helps. 
> I mean, you will always have to do some work anyway when adopting new 
> inputs -- even if just adding annotations. 
> So question there becomes: what approaches is easiest, most reliable 
> to maintain? 
>
> > Currently I'm doing this by mapping each of the responses to JsonNode 
> only, 
> > but every time I add a new common API I need to write different logic to 
> > convert the new response into a common one and that to for different 
> APIs 
> > I'm calling from my API. 
> > If any kinda JSON response can be mapped to JsonNode, isn't is possible 
> to 
> > map these different responses to any particular format? I mean can I 
> write 
> > my resource object in such a way that I'd be able to map these different 
> > json responses representing same kinda data to my own resource, in some 
> way, 
> > instead of doing manually by traversing through json tree? 
> > 
> > Hope it is clear to now..Initially I thought it's impossible, but you 
> gave 
> > me direction and I do believe now there'd be some way that I can bind 
> these 
> > different resources to common one. May be the way json objects are 
> mapped to 
> > JsonNode. 
> > 
> > Please let me know if this is clear to you, as right now you the only 
> hope I 
> > can see.! 
> > 
> > Thank you Tatu..! 
>
> It may make sense to map input you get into `JsonNode` and have 
> separate code to traverse them; 
> or to have a set of Input POJOs and code to convert from those to 
> result/output POJO. 
> There are pros and cons to each approach. 
>
> -+ Tatu +- 
>
> > 
> > On Sunday, September 10, 2017 at 10:00:21 PM UTC+5:30, Tatu Saloranta 
> wrote: 
> >> 
> >> On Sat, Sep 9, 2017 at 9:58 PM, chetan choulwar <[email protected]> 
> >> wrote: 
> >> > That @JsonAlias worked fine. But my problem is with nested elements, 
> how 
> >> > can 
> >> > I specify nested json element into alias? 
> >> 
> >> You can't. If your structures differ beond just naming you need to do 
> >> something else. 
> >> Databinding is meant to match incoming document structure to a Java 
> >> object graph and vice versa. 
> >> It is not designed to allow arbitrary transformation, or unification 
> >> of differing structures into single model. 
> >> 
> >> It is possible to unify differing structures, of course, but that is 
> >> true transformation and something explicitly 
> >> out of scope for Jackson. You can use `JsonNode` as structure modify, 
> >> for input and output structures, and it is possible 
> >> to change structure any way you want. But you have to write that 
> >> transformation yourself. 
> >> Or you can use separate Java classes for input(s) and output. 
> >> 
> >> > And reason for doing so is that I'm writing an API that will send the 
> >> > common 
> >> > response out of all these different APIs I am calling from my API. 
> >> 
> >> I am still not sure I fully understand your usage, still; this is very 
> >> generic explanation. 
> >> 
> >> But assuming I do understand... if input data you get from a service 
> >> differs, you should consider separating handling of your input from 
> >> constructing your output. Do not try to use same set of objects if 
> >> structures are not same or similar enough. Trying to do that is false 
> >> savings and tends to sacrifice clean design for seeming savings. 
> >> 
> >> This assuming I actually understood what you are trying to do -- 
> >> without full explanation of steps from calling another service to 
> >> formulating output it is possible to misunderstand intent. 
> >> 
> >> > Apart from this it'd be great if you can you mail me a link where I 
> can 
> >> > find 
> >> > latest documentation and hands-on for the same. 
> >> > 
> >> > Thanking you in anticipation..! 
> >> 
> >> Documentation hub is at: 
> >> 
> >> https://github.com/FasterXML/jackson-docs 
> >> 
> >> and main portal 
> >> 
> >> https://github.com/FasterXML/jackson/ 
> >> 
> >> has some links. 
> >> 
> >> -+ Tatu +- 
> >> 
> >> 
> >> 
> >> > 
> >> > 
> >> > On Sunday, September 10, 2017 at 9:21:55 AM UTC+5:30, Tatu Saloranta 
> >> > wrote: 
> >> >> 
> >> >> On Fri, Sep 8, 2017 at 5:13 AM, chetan choulwar <[email protected]> 
>
> >> >> wrote: 
> >> >> > Thanks a lot for your input. I tried it and it worked as expected. 
> >> >> > But 
> >> >> > I'm 
> >> >> > stuck at a situation where I want to retrieve a value from nested 
> >> >> > block, 
> >> >> > so 
> >> >> > can you help me out? 
> >> >> > For example, 
> >> >> > By calling REST API "https://xyz.com/resources/resource"; gives me 
> { 
> >> >> > 
> >> >> > "value"={"name":"abc.txt"} 
> >> >> > 
> >> >> > } 
> >> >> > and calling "https://abc.com/resources/resource"; gives me 
> >> >> > {"title":"idontknow.txt"} 
> >> >> > now how can I take name out from the first json response? 
> >> >> > 
> >> >> > Once again thanks a lot for your answer..!:) 
> >> >> 
> >> >> I am not sure why my first answer wouldn't work here. All you are 
> >> >> doing is specifying that property name in json is an alias that can 
> be 
> >> >> used for property in POJO, so you would access it with field name 
> (or 
> >> >> getter) you have. 
> >> >> 
> >> >> But at the same time if these are effectively different objects it 
> is 
> >> >> unclear why same Java class should be used anyway -- perhaps they 
> >> >> should use different POJOs. 
> >> >> 
> >> >> -+ Tatu +- 
> >> >> 
> >> >> 
> >> >> > 
> >> >> > On Friday, September 8, 2017 at 9:19:49 AM UTC+5:30, Tatu 
> Saloranta 
> >> >> > wrote: 
> >> >> >> 
> >> >> >> On Thu, Sep 7, 2017 at 5:24 AM, chetan choulwar <
> [email protected]> 
> >> >> >> wrote: 
> >> >> >> > Hi All, 
> >> >> >> > 
> >> >> >> > I'm calling different REST APIs and getting different kinda 
> JSON 
> >> >> >> > responses; 
> >> >> >> > is there any way to pick particular attribute from different 
> JSON 
> >> >> >> > responses 
> >> >> >> > and map it to a one common property of defined POJO (Resource 
> for 
> >> >> >> > my 
> >> >> >> > API) 
> >> >> >> > that can then be sent as a response from the REST API that I've 
> >> >> >> > exposed? 
> >> >> >> > 
> >> >> >> > For Example: 
> >> >> >> > By calling REST API "https://xyz.com/resources/resource"; gives 
> me 
> >> >> >> > {"name":"abc.txt"} 
> >> >> >> > and calling "https://abc.com/resources/resource"; gives me 
> >> >> >> > {"title":"idontknow.txt"} 
> >> >> >> > 
> >> >> >> > And I have one resource class defined for my APIs to return 
> i.e. 
> >> >> >> > MyResource 
> >> >> >> > { 
> >> >> >> >    String fileName; 
> >> >> >> > } 
> >> >> >> > 
> >> >> >> > So is there any way that I can map "name"/"title" to fileName 
> i.e. 
> >> >> >> > how 
> >> >> >> > can I 
> >> >> >> > use jackson to deserialize these jsons to MyResource type? 
> >> >> >> > 
> >> >> >> > Please let me know if this is valid? and if yes, how? 
> >> >> >> 
> >> >> >> If you have many different names to use, it may be simpler to 
> just 
> >> >> >> bind JSON to `Map` or `JsonNode`, and extract value explicitly. 
> >> >> >> 
> >> >> >> But if there are just couple of values, you can use `@JsonAlias` 
> >> >> >> like: 
> >> >> >> 
> >> >> >>     public class POJO { 
> >> >> >>        @JsonAlias({ "name", "title" }) 
> >> >> >>        public String fileName; 
> >> >> >>     } 
> >> >> >> 
> >> >> >> which would then accept alternate names "name" and "title", but 
> >> >> >> serialize as "fileName" (which it also accepts). 
> >> >> >> This annotations was added in Jackson 2.9 
> >> >> >> 
> >> >> >> -+ Tatu +- 
> >> >> > 
> >> >> > -- 
> >> >> > You received this message because you are subscribed to the Google 
> >> >> > Groups 
> >> >> > "jackson-dev" group. 
> >> >> > To unsubscribe from this group and stop receiving emails from it, 
> >> >> > send 
> >> >> > an 
> >> >> > email to [email protected]. 
> >> >> > For more options, visit https://groups.google.com/d/optout. 
> >> > 
> >> > -- 
> >> > You received this message because you are subscribed to the Google 
> >> > Groups 
> >> > "jackson-dev" group. 
> >> > To unsubscribe from this group and stop receiving emails from it, 
> send 
> >> > an 
> >> > email to [email protected]. 
> >> > For more options, visit https://groups.google.com/d/optout. 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "jackson-dev" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to [email protected] <javascript:>. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to