|
package com.pers.telegram.util; |
|
|
|
import java.io.IOException; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
import com.fasterxml.jackson.annotation.JsonInclude; |
|
import com.fasterxml.jackson.core.JsonGenerator; |
|
import com.fasterxml.jackson.core.JsonParser; |
|
import com.fasterxml.jackson.core.JsonToken; |
|
import com.fasterxml.jackson.core.Version; |
|
import com.fasterxml.jackson.databind.DeserializationContext; |
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
import com.fasterxml.jackson.databind.SerializerProvider; |
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
|
import com.fasterxml.jackson.databind.module.SimpleModule; |
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
|
import com.pers.telegram.wnacg.service.pojo.Node; |
|
import com.pers.telegram.wnacg.service.pojo.NodeElement; |
|
import com.pers.telegram.wnacg.service.pojo.NodeText; |
|
|
|
public class JsonUtil { |
|
|
|
public ObjectMapper objectMapper; |
|
|
|
public JsonUtil() { |
|
objectMapper = new ObjectMapper(); |
|
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
|
|
|
SimpleModule simpleModule = new SimpleModule("customNode", new Version(1, 0, 0, null, null, null)); |
|
simpleModule.addSerializer(Node.class, new CustomNodeSerializer()); |
|
simpleModule.addDeserializer(Node.class, new CustomNodeDeserializer()); |
|
objectMapper.registerModules(simpleModule); |
|
} |
|
|
|
public String toJson(Object object) { |
|
try { |
|
return objectMapper.writeValueAsString(object); |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} |
|
return null; |
|
} |
|
|
|
public <T> T toObject(String json, Class<T> clazz) { |
|
try { |
|
return objectMapper.readValue(json, clazz); |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} |
|
return null; |
|
} |
|
|
|
public <T> List<T> toList(String json, Class<T> clazz) { |
|
try { |
|
return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} |
|
return null; |
|
} |
|
|
|
public static class CustomNodeSerializer extends StdSerializer<Node> { |
|
|
|
public CustomNodeSerializer() { |
|
this(null); |
|
} |
|
|
|
protected CustomNodeSerializer(Class<Node> t) { |
|
super(t); |
|
} |
|
|
|
@Override |
|
public void serialize(Node node, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { |
|
if (node instanceof NodeText) { |
|
jsonGenerator.writeString(((NodeText) node).getValue()); |
|
} else if (node instanceof NodeElement) { |
|
NodeElement element = (NodeElement) node; |
|
jsonGenerator.writeStartObject(); |
|
jsonGenerator.writeStringField("tag", element.getTag()); |
|
if (element.getAttrs() != null) { |
|
jsonGenerator.writePOJOField("attrs", element.getAttrs()); |
|
} |
|
if (element.getChildren() != null) { |
|
jsonGenerator.writeArrayFieldStart("children"); |
|
for (Node child : element.getChildren()) { |
|
jsonGenerator.writeObject(child); |
|
} |
|
jsonGenerator.writeEndArray(); |
|
} |
|
jsonGenerator.writeEndObject(); |
|
} else { |
|
jsonGenerator.writeNull(); |
|
} |
|
} |
|
} |
|
|
|
public static class CustomNodeDeserializer extends StdDeserializer<Node> { |
|
|
|
public CustomNodeDeserializer() { |
|
this(null); |
|
} |
|
|
|
protected CustomNodeDeserializer(Class<Node> t) { |
|
super(t); |
|
} |
|
|
|
@Override |
|
public Node deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { |
|
if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) { |
|
return new NodeText(jsonParser.getText()); |
|
} else if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) { |
|
NodeElement nodeElement = new NodeElement(); |
|
while (jsonParser.nextToken() != JsonToken.END_OBJECT) { |
|
String fieldName = jsonParser.getCurrentName(); |
|
jsonParser.nextToken(); |
|
if ("tag".equals(fieldName)) { |
|
nodeElement.setTag(jsonParser.getText()); |
|
} else if ("attrs".equals(fieldName)) { |
|
//noinspection unchecked |
|
nodeElement.setAttrs(jsonParser.readValueAs(Map.class)); |
|
} else if ("children".equals(fieldName)) { |
|
|
|
List<Node> children = new ArrayList<>(); |
|
while (jsonParser.nextToken() != JsonToken.END_ARRAY) { |
|
children.add(this.deserialize(jsonParser, deserializationContext)); |
|
// children.add(jsonParser.readValueAs(Node.class)); |
|
} |
|
nodeElement.setChildren(children); |
|
} |
|
} |
|
return nodeElement; |
|
} else { |
|
return null; |
|
} |
|
} |
|
} |
|
|
|
} |