2

What I am trying to achieve is:

  • We have a JSON Object Stream coming in.
  • I want to reconstruct a Java Object from the JSON.
  • I want to apply transformations (like change format of strings and reject an object if some condition is met like 'isToBeConsumed' == false and possible flatten the heirarchy from nesting to linear)
  • I want to be able to store the values of the JSON objects into the database.

When a new type of JSON Object Stream comes in, I am OK to run CREATE TABLE for each type of Stream but i do not want to do any code change.

In other words, I want to be able to define a Java Class in a Config file.

{"studentName":"string", "studentRollNumber":"integer", "studentGrade":"integer", "studentPhoneNumber":"string"}

Now if a 2nd stream comes in like:

{"teacherName":"string", "teacherEmployeeId":"integer", "teacherPhoneNumber":"integer", "teacherAddress":"string"}

i do not want to do a code change.

I am ok to restrict the possibilities to 4 basic data types.

I might have rules like

{"excludeIf": { "columnName": "grade", "condition": "equals", "value":"10"}

But lets not worry about the exclude rules as of now and focus on how to reconstruct a JAVA class from Config File and then Deserialize a JSON stream into an object of that class.

I saw that Gson 2.0+ gives a feature to convert a generic Map https://stackoverflow.com/a/20442943/429218

I am looking for more options to Dynamically Construct a Class in Java and Deserialize a JSON stream into it.

Thanks

17
  • Dynamic create tables for each type? Create a Java class for each different json input? It seems a TERRIBLE design here... Maybe if you explain what you want to do you will get better possibilities. Jan 12, 2016 at 4:49
  • I am going to have 3 type of streams coming in.. Students Info Stream, Teachers Info Stream and Marks Info Steam. I might have a 4th type coming in few months later like School Events Stream. I want to build a system where I have an onboarding process for this new stream (where i will be engaged manually and I will create the database tables and change some configs) But i do not want to make a code change for incorporating this 4th Type of Stream. Jan 12, 2016 at 4:56
  • Why would you do this dynamically if you already KNOW what you will gona receive?? This is a terrible idea and will be impossible to maintain and will have a terrible code. Imagine no you have three types of data, but your code will support anything given your rules. Every thing in your system would have to be dynamic based on the objects you receive. Please, don't do that. Create your domain models, serialize your objects and work with, if you have to change the code later so be it. It will be HUNDRED THOUSANDS times better. This is an honest advice. Please reconsider! Jan 12, 2016 at 5:11
  • The student and teacher stream are placeholders, I have absolutely no idea what kind of data is supposed to come in. I am going to release my applications as a Platform and I need to be data agnostic. It is not an option to restrict myself to specific Data Model. Is it hard to imagine (read as Implement) a system where i have complete control over what i can send in, define rules when i want to exclude a particular object/row and persist it so that i run SQL on it later? Jan 12, 2016 at 5:22
  • Ok, Let's imagine you were able to create such a system (that will be able to receive ANY data and accept it and control it in tables and classes)! What you gone do with it? Lets say after hundreds of different types? I'm POSITIVELY ABSOLUTELY SURE that you don't think this through enough to think on this kind of solution. There is no such a thing as a system that receives ANY type of data and create dynamic java classes for it, it would be impossible to manage and to create another functionalities based on data that you don't know! Jan 12, 2016 at 5:27

1 Answer 1

0

This seems like in runtime, application doesn't know the JSON format and yet need to find the values and keys to create tables and put data in db. Please refer to Parsing Json in Java without knowing structure. This will give you an idea of doing it in generic way. However, you need to write more code with if statements to create tables automatically if application finds something new and non existing. However, this is a terrible design as stated by @Jorge Campos. In Real world application will have a contract i.e. client and server. Doing in generic way impacts performance and lot of cpu utilization for garbage collection.

3
  • 1
    The student and teacher stream are placeholders, I have absolutely no idea what kind of data is supposed to come in. I am going to release my applications as a Platform and I need to be data agnostic. It is not an option to restrict myself to specific Data Model. Is it hard to imagine (read as Implement) a system where i have complete control over what i can send in, define rules when i want to exclude a particular object/row and persist it so that i run SQL on it later? Jan 12, 2016 at 5:23
  • As stated above this can be implemented. Refer to the answers in the link. You just need to tweak your code a little. Try to make a sample and see how that works. The above link pretty much tells you how to get keys and values from the json and then you need to do define your use case with those. Jan 12, 2016 at 5:28
  • There is also possibility for this. AnveshVejandla , instead of saying that the application need to find the keys, let's assume all the keys which ever needed are provided as a configuration as said by rjdthegreat instead of providing keys which are not necessary. Oct 12, 2016 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.