Last active
May 9, 2016 04:33
-
-
Save varren/766b6830878c9e15c51784f57b303646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.*; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Objects; | |
public class Main30 { | |
private static String json = "{\n" + | |
" \"ok\": true,\n" + | |
" \"result\": [\n" + | |
" {\n" + | |
" \"update_id\": 489881706,\n" + | |
" \"message\": {\n" + | |
" \"message_id\": 5,\n" + | |
" \"from\": {\n" + | |
" \"id\": 188474643,\n" + | |
" \"first_name\": \"Alireza\",\n" + | |
" \"last_name\": \"Mohamadi\",\n" + | |
" \"username\": \"SuNova\"\n" + | |
" },\n" + | |
" \"chat\": {\n" + | |
" \"id\": 188474643,\n" + | |
" \"first_name\": \"Alireza\",\n" + | |
" \"last_name\": \"Mohamadi\",\n" + | |
" \"username\": \"SuNova\",\n" + | |
" \"type\": \"private\"\n" + | |
" },\n" + | |
" \"date\": 1462608191,\n" + | |
" \"text\": \"1\"\n" + | |
" }\n" + | |
" }\n" + | |
" ]\n" + | |
"}"; | |
private static String json2 = "{\n" + | |
" \"ok\": true,\n" + | |
" \"result\": [\n" + | |
" {\n" + | |
" \"update_id\": 489881706,\n" + | |
" \"message_entity\": {\n" + | |
" \"message_entity_id\": 5\n" + | |
" }\n" + | |
" }\n" + | |
" ]\n" + | |
"}"; | |
private static String json3 = "{\n" + | |
" \"ok\": true,\n" + | |
" \"result\": [\n" + | |
" {\n" + | |
" \"update_id\": 489881706,\n" + | |
" \"chat\": {\n" + | |
" \"id\": 188474643,\n" + | |
" \"first_name\": \"Alireza\",\n" + | |
" \"last_name\": \"Mohamadi\",\n" + | |
" \"username\": \"SuNova\",\n" + | |
" \"type\": \"private\"\n" + | |
" }\n" + | |
" }\n" + | |
" ]\n" + | |
"}"; | |
public static void main(String[] args) throws IOException { | |
ObjectMapper mapper = new ObjectMapper(); | |
Result result = mapper.readValue(json, Result.class); | |
System.out.println(mapper.writeValueAsString(result)); | |
Result result2 = mapper.readValue(json2, Result.class); | |
System.out.println(mapper.writeValueAsString(result2)); | |
Result result3 = mapper.readValue(json3, Result.class); | |
System.out.println(mapper.writeValueAsString(result3)); | |
} | |
public static class Result { | |
@JsonProperty("result") | |
private Update[] result; | |
@JsonProperty("ok") | |
private boolean ok; | |
} | |
public static class Update { | |
private static final ObjectMapper mapper = new ObjectMapper(); | |
@JsonProperty("update_id") | |
private int updateId; | |
private TObject updateInfo; | |
@JsonAnySetter | |
public void setValues(String key, JsonNode value) { | |
System.out.println("Found: " + key); | |
try { | |
if (Objects.equals(key, "chat")) { | |
updateInfo = mapper.treeToValue(value, Chat.class); | |
} else if (Objects.equals(key, "message")) { | |
updateInfo = mapper.treeToValue(value, MessageImpl.class); | |
} else if (Objects.equals(key, "message_entity")) { | |
updateInfo = mapper.treeToValue(value, MessageEntity.class); | |
} | |
} catch (JsonProcessingException e) { | |
e.printStackTrace(); | |
} | |
} | |
@JsonAnyGetter | |
public Map<String, Object> getValues() { | |
Map<String, Object> map = new HashMap<>(); | |
String key = ""; | |
if (updateInfo instanceof Chat) { | |
key = "chat"; | |
} else if (updateInfo instanceof MessageImpl) { | |
key = "message"; | |
} else if (updateInfo instanceof MessageEntity) { | |
key = "message_entity"; | |
} | |
map.put(key, updateInfo); | |
return map; | |
} | |
} | |
public static class From { | |
@JsonProperty("id") | |
public Integer id; | |
@JsonProperty("first_name") | |
public String firstName; | |
@JsonProperty("last_name") | |
public String lastName; | |
@JsonProperty("username") | |
public String username; | |
} | |
public interface TObject {} | |
public static class Chat extends From implements TObject { | |
@JsonProperty("type") | |
public String type; | |
} | |
public static class MessageImpl implements TObject { | |
@JsonProperty("message_id") | |
public Integer messageId; | |
@JsonProperty("from") | |
public From from; | |
@JsonProperty("chat") | |
public Chat chat; | |
@JsonProperty("date") | |
public Integer date; | |
@JsonProperty("text") | |
public String text; | |
} | |
public static class MessageEntity implements TObject { | |
@JsonProperty("message_entity_id") | |
public Integer MessageEntityID; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.*; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.databind.*; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import java.io.IOException; | |
public class Main30 { | |
private static String json = "{\n" + | |
" \"ok\": true,\n" + | |
" \"result\": [\n" + | |
" {\n" + | |
" \"update_id\": 489881706,\n" + | |
" \"message\": {\n" + | |
" \"message_id\": 5,\n" + | |
" \"from\": {\n" + | |
" \"id\": 188474643,\n" + | |
" \"first_name\": \"Alireza\",\n" + | |
" \"last_name\": \"Mohamadi\",\n" + | |
" \"username\": \"SuNova\"\n" + | |
" },\n" + | |
" \"chat\": {\n" + | |
" \"id\": 188474643,\n" + | |
" \"first_name\": \"Alireza\",\n" + | |
" \"last_name\": \"Mohamadi\",\n" + | |
" \"username\": \"SuNova\",\n" + | |
" \"type\": \"private\"\n" + | |
" },\n" + | |
" \"date\": 1462608191,\n" + | |
" \"text\": \"1\"\n" + | |
" }\n" + | |
" }\n" + | |
" ]\n" + | |
"}"; | |
private static String json2 = "{\n" + | |
" \"ok\": true,\n" + | |
" \"result\": [\n" + | |
" {\n" + | |
" \"update_id\": 489881706,\n" + | |
" \"message_entity\": {\n" + | |
" \"message_entity_id\": 5\n" + | |
" }\n" + | |
" }\n" + | |
" ]\n" + | |
"}"; | |
private static String json3 = "{\n" + | |
" \"ok\": true,\n" + | |
" \"result\": [\n" + | |
" {\n" + | |
" \"update_id\": 489881706,\n" + | |
" \"chat\": {\n" + | |
" \"id\": 188474643,\n" + | |
" \"first_name\": \"Alireza\",\n" + | |
" \"last_name\": \"Mohamadi\",\n" + | |
" \"username\": \"SuNova\",\n" + | |
" \"type\": \"private\"\n" + | |
" }\n" + | |
" }\n" + | |
" ]\n" + | |
"}"; | |
public static void main(String[] args) throws IOException { | |
ObjectMapper mapper = new ObjectMapper(); | |
SimpleModule module = new SimpleModule(); | |
module.addDeserializer(TObject.class, new TObjectDeserializer()); | |
module.addSerializer(TObject.class, new TObjectSerializer()); | |
mapper.registerModule(module); | |
Result result = mapper.readValue(json, Result.class); | |
System.out.println(mapper.writeValueAsString(result)); | |
Result result2 = mapper.readValue(json2, Result.class); | |
System.out.println(mapper.writeValueAsString(result2)); | |
Result result3 = mapper.readValue(json3, Result.class); | |
System.out.println(mapper.writeValueAsString(result3)); | |
} | |
public static class Result { | |
@JsonProperty("result") | |
private TObject[] result; | |
@JsonProperty("ok") | |
private boolean ok; | |
} | |
public static abstract class TObject { | |
@JsonIgnore | |
private int updateId; | |
@JsonIgnore | |
public int getUpdateId() { | |
return updateId; | |
} | |
@JsonIgnore | |
public void setUpdateId(int updateId) { | |
this.updateId = updateId; | |
} | |
} | |
public static class Chat extends TObject { | |
@JsonProperty("id") | |
public Integer id; | |
@JsonProperty("first_name") | |
public String firstName; | |
@JsonProperty("last_name") | |
public String lastName; | |
@JsonProperty("username") | |
public String username; | |
@JsonProperty("type") | |
public String type; | |
} | |
public static class Message extends TObject { | |
@JsonProperty("message_id") | |
public Integer messageId; | |
@JsonProperty("from") | |
public From from; | |
@JsonProperty("chat") | |
public Chat chat; | |
@JsonProperty("date") | |
public Integer date; | |
@JsonProperty("text") | |
public String text; | |
} | |
public static class MessageEntity extends TObject { | |
@JsonProperty("message_entity_id") | |
public Integer MessageEntityID; | |
} | |
public static class From { | |
@JsonProperty("id") | |
public Integer id; | |
@JsonProperty("first_name") | |
public String firstName; | |
@JsonProperty("last_name") | |
public String lastName; | |
@JsonProperty("username") | |
public String username; | |
} | |
private static class TObjectDeserializer extends JsonDeserializer<TObject> { | |
@Override | |
public TObject deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | |
JsonNode tree = p.readValueAsTree(); | |
tree.get("chat"); | |
TObject resultValue = null; | |
if(tree.has("chat")){ | |
resultValue = p.getCodec().treeToValue(tree.get("chat"), Chat.class); | |
}else if(tree.has("message")){ | |
resultValue = p.getCodec().treeToValue(tree.get("message"),Message.class); | |
} else if(tree.has("message_entity")){ | |
resultValue = p.getCodec().treeToValue(tree.get("message_entity"),MessageEntity.class); | |
} | |
if(resultValue!= null) | |
resultValue.setUpdateId(tree.get("update_id").asInt()); | |
return resultValue; | |
} | |
} | |
private static class TObjectSerializer extends JsonSerializer<TObject> { | |
private static ObjectMapper mapper = new ObjectMapper(); | |
@Override | |
public void serialize(TObject value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | |
gen.writeStartObject(); | |
gen.writeNumberField("update_id", value.getUpdateId()); | |
String key = ""; | |
if(value instanceof Chat){ | |
key = "chat"; | |
}else if(value instanceof Message){ | |
key = "message"; | |
} else if(value instanceof MessageEntity){ | |
key = "message_entity"; | |
} | |
gen.writeFieldName(key); | |
gen.writeRawValue(mapper.writeValueAsString(value)); | |
gen.writeEndObject(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment