<dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.12.1</version> </dependency> public class Person { private String name; private int age; } import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; import io.protostuff.runtime.RuntimeSchema; import io.protostuff.runtime.ProtobufSerializer; public class ProtostuffYamlExample { private static final ObjectMapper YAML_MAPPER = new YAMLMapper(); private static final RuntimeSchema<Person> PERSON_SCHEMA = RuntimeSchema.createFrom(Person.class); public static void main(String[] args) throws Exception { Person person = new Person(); person.setName("Alice"); person.setAge(25); byte[] protostuffData = ProtobufSerializer.serialize(person, PERSON_SCHEMA); String yamlData = YAML_MAPPER.writeValueAsString(protostuffData); System.out.println("YAML: " + yamlData); byte[] deserializedData = YAML_MAPPER.readValue(yamlData, byte[].class); Person deserializedPerson = PERSON_SCHEMA.newMessage(); ProtobufSerializer.deserialize(deserializedData, deserializedPerson, PERSON_SCHEMA); System.out.println("Deserialized Person: " + deserializedPerson.getName() + ", " + deserializedPerson.getAge()); } } import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import io.protostuff.Output; import io.protostuff.Schema; import io.protostuff.runtime.RuntimeSchema; import io.protostuff.SerializableSerializer; import org.yaml.snakeyaml.introspector.PropertyUtils; import java.io.IOException; public class PersonSerializer extends StdSerializer<Person> implements JsonSerializer<Person> { private static final RuntimeSchema<Person> SCHEMA = RuntimeSchema.createFrom(Person.class); public PersonSerializer() { super(Person.class); } @Override public void serialize(Person person, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { byte[] protostuffData = SerializableSerializer.serialize(person, SCHEMA); jsonGenerator.writeBinary(protostuffData); } @Override public Object serializeWithType(Person person, JsonGenerator jsonGenerator, SerializerProvider serializerProvider, TypeSerializer typeSerializer) throws IOException { serialize(person, jsonGenerator, serializerProvider); return person; } @Override public void writeTo(Output output, Person person) { SerializableSerializer.serialize(output, person, SCHEMA); } @Override public Person readFrom(Input input) { Person person = SCHEMA.newMessage(); SerializableSerializer.deserialize(input, person, SCHEMA); return person; } @Override public Person deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { byte[] protostuffData = jsonParser.getBinaryValue(); return SerializableSerializer.deserialize(protostuffData, SCHEMA); } } Serializer<Person> protostuffSerializer = new PersonSerializer(); YAML_MAPPER.registerModule(new SimpleModule().addSerializer(protostuffSerializer));


上一篇:
下一篇:
切换中文