项目作者: protostuff

项目描述 :
Java serialization library, proto compiler, code generator
高级语言: Java
项目地址: git://github.com/protostuff/protostuff.git
创建时间: 2014-01-20T01:22:51Z
项目社区:https://github.com/protostuff/protostuff

开源协议:Apache License 2.0

下载


Protostuff

A java serialization library with built-in support for forward-backward compatibility (schema evolution) and validation.

  • efficient, both in speed and memory
  • flexible, supporting pluggable formats

Usecase

  • messaging layer in RPC
  • storage format in the datastore or cache

For more information, go to https://protostuff.github.io/docs/

Maven

  1. For the core formats (protostuff, protobuf, graph)

    1. <dependency>
    2. <groupId>io.protostuff</groupId>
    3. <artifactId>protostuff-core</artifactId>
    4. <version>1.7.4</version>
    5. </dependency>
  2. For schemas generated at runtime

    1. <dependency>
    2. <groupId>io.protostuff</groupId>
    3. <artifactId>protostuff-runtime</artifactId>
    4. <version>1.7.4</version>
    5. </dependency>

Usage

  1. public final class Foo
  2. {
  3. String name;
  4. int id;
  5. public Foo(String name, int id)
  6. {
  7. this.name = name;
  8. this.id = id;
  9. }
  10. }
  11. static void roundTrip()
  12. {
  13. Foo foo = new Foo("foo", 1);
  14. // this is lazily created and cached by RuntimeSchema
  15. // so its safe to call RuntimeSchema.getSchema(Foo.class) over and over
  16. // The getSchema method is also thread-safe
  17. Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);
  18. // Re-use (manage) this buffer to avoid allocating on every serialization
  19. LinkedBuffer buffer = LinkedBuffer.allocate(512);
  20. // ser
  21. final byte[] protostuff;
  22. try
  23. {
  24. protostuff = ProtostuffIOUtil.toByteArray(foo, schema, buffer);
  25. }
  26. finally
  27. {
  28. buffer.clear();
  29. }
  30. // deser
  31. Foo fooParsed = schema.newMessage();
  32. ProtostuffIOUtil.mergeFrom(protostuff, fooParsed, schema);
  33. }

Important (for version 1.8.x)

If you are to purely use this to replace java serialization (no compatibility with protobuf), set the following system properties:

  1. -Dprotostuff.runtime.always_use_sun_reflection_factory=true
  2. -Dprotostuff.runtime.preserve_null_elements=true
  3. -Dprotostuff.runtime.morph_collection_interfaces=true
  4. -Dprotostuff.runtime.morph_map_interfaces=true
  5. -Dprotostuff.runtime.morph_non_final_pojos=true

You can also customize it programmatically:

  1. static final DefaultIdStrategy STRATEGY = new DefaultIdStrategy(IdStrategy.DEFAULT_FLAGS
  2. | IdStrategy.PRESERVE_NULL_ELEMENTS
  3. | IdStrategy.MORPH_COLLECTION_INTERFACES
  4. | IdStrategy.MORPH_MAP_INTERFACES
  5. | IdStrategy.MORPH_NON_FINAL_POJOS);

Use it:

  1. Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class, STRATEGY);

Questions/Concerns/Suggestions

Requirements

Java 1.6 or higher

Build Requirements

Maven 3.2.3 or higher

Developing with eclipse

  1. mvn install && mvn eclipse:eclipse
  2. # Open eclipse, import existing project, navigate to the protostuff module you're after, then hit 'Finish'.