This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Separate class for JSON parsing?  RSS feed

 
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a design question.  I will be using a class with jackson annotations and there will be logic for finding the right values.  For example there are arrays of objects in the JSON string but I only want some of that data.  
I will be making alot of these objects so I was wondering if it would be better to have a class that will contain the data only and a class that has the JSON parsing.  One way I can do it is like this:


The amount of values I have to get is way more than what is shown above.
I can also make a custom deserializer for objectMapper.

I am talking 1000s of these objects being created and used so I thought it would make sense to have the deserialization logic outside of the data object.  

Would separating the logic be a good design choice or am I just overthinking it?

thanks



--->   I think I'll answer my own question.  According to  OOP theory,  things should have one job, so naturally the parsing logic and the part that contains the data should be separated.  The correct answer would be to make a custom deserializer.  Turns out one piece of data I need isn't in the web api response so I won't be updating the program in question after all.

I won't resolve the question just in case there is anymore insight.
 
Saloon Keeper
Posts: 10206
216
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have custom deserialization in the first place?

Just create a Data Transfer Object that contains only the properties that you are interested in, properly annotated. Assuming your data looks like this:

I would write code that looks like this:

The following test succeeds:
 
Al Hobbs
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I would use a custom deserializer is because the json is relatively complicated.  This is what the response json would look like


If i didn't use a custom deserializer wouldn't I have to make a bunch of different classes or is that not a big deal?  I feel it might be cleaner and easier to just have logic that parses it but I'm not sure.
 
Al Hobbs
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it would be easier to actually just make the classes.  There wouldn't be that many and in your example you had three classes for a simple JSON.  I would probably do it like that.

My other problem would then be using book data from different sources.  Would I have an interface that the DTO can implement? Or would I just have a way to put the data from the DTO into a generic data class?

 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not valid JSON, there are errors on lines 4, 16 and 39. I fixed them by adding double quotes on line 4, a 'u' on line 16 and removing a backslash on line 39.

It appears that my DTOs would work just fine with the fixed example JSON, you just need an extra class for the outer JSON object:

The following test succeeds:

Making a lot of classes for JSON is not a big deal. It's easier to understand and maintain, and it's nicer to use because all the objects are strongly typed, rather than weak types like Map and Object.

As for your different sources, if the JSON looks completely different, then you have to create new DTOs for the entire response message. In either case, you convert the DTOs from the different services to a class from your business layer that represents a book. Don't perform business logic on DTOs directly.

Don't call the book representation from your business layer a "generic data class". It's not. It's supposed to be a proper representation of a book, and also encapsulates business logic that you want to perform on books.
 
Al Hobbs
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Don't perform business logic on DTOs directly.


If I need to change one of the values that is in the DTO should I do that outside of the DTO or can I do it inside the DTO?

For example: (inside the DTO)

or should I do it outside of the DTO?  I think that it would be okay to put it on the inside of the DTO because otherwise you need another class for changing values that come in a format unique to a source.
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what kind of change are we talking about?
 
Al Hobbs
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like for example if one of the values, say 'title,' sometimes also contained a subtitle like this:  'Pizza 101 - guide to making bomb pizza.'
In this case I would only want 'Pizza 101.'  So I would have to check if there's a '-' or however I would check and then cut that part off.
Or if the author's name is like 'Smith, John' instead of 'John Smith.'
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, where are you going to use the part without the subtitle?
 
Al Hobbs
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the data is put into an excel file as is.  The author will be replaced with an id if it exists that is found using the current value.  That will be done before writing to the excel because it some cases, the data isn't used.   The searching of the author id would be done in a different class.  

For example:


The part without the subtitle is used as the actual title.  bookCreator would be a service that would get the author id.  Originally I was going to put that logic into the same class that gets the book data but since there will be more than one implementation of that, and only one that gets the author id, it makes more sense to put the author id logic into it's own class.
 
Al Hobbs
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to assume that putting some parse logic in the setter is ok.
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're not going to put any business logic in it, you can manipulate the DTO as is. This would be the case if you're just reorganizing data before writing it to some other place again.

However, if you're editing the data based on some set of rules, then usually you will want to convert the DTO to a business model instance and manipulate that, before converting it back to a DTO again.

It depends on how complex your needs are. You also need to just get experience with what solution is more maintainable in what case. There's no silver bullet.
 
money grubbing section goes here:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!