Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

java - Gson custom deserializer for specific fields to String

I have a variety of JSON files (with slightly different schemas) flowing on Kinesis. Their Schema is really complex. They are user raw hits and their schema is super complex. I would like to create a Single POJO to represent all underneath messages (Something Spark does internally by creating a Single schema). I was trying to use GSON library but the only way to accomplish this is, by writing a custom deserializer and in that case, I will end up writing deserialization logic for all the fields in those JSONs. Is there any way where we can only overwrite the deserialization of a few fields only and the rest of the fields can still be deserialized by GSON as a default way?

JSON-1
{
   "first_col":"abc",
   "second_col":false
}

JSON-2
{
   "first_col":"abc",
   "second_col":"false-String"
}


JSON-3
{
   "first_col":"abc",
   "second_col":{
      "col1":"xyz",
      "col2":123
   }
}

Common POJO


import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

private String firstCol;

private String secondCol;

public String getFirstCol() {
return firstCol;
}

public void setFirstCol(String firstCol) {
this.firstCol = firstCol;
}

public Boolean getSecondCol() {
return secondCol;
}

public void setSecondCol(String secondCol) {
this.secondCol = secondCol;
}

} 

So basically second_col could be boolean, string, or complex object. first_col is always a string. So I don't want to write deserialize logic for first_col. I want to write deserialize logic for only second_col and deserialize it to string only and the downstream consumer will take care of converting it to the right type before consuming this field.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...