Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 124 lines (104 sloc) 7.709 kb
53c4727 Fix links and link names
Martin Grotzke authored
1 A project that provides [kryo](http://code.google.com/p/kryo) serializers for some jdk types and some external libs like e.g. joda time.
84f62b3 Add README
Martin Grotzke authored
2
43aad37 Add section that shows how to register serializers to README
Martin Grotzke authored
3 # Provided serializers / supporting classes:
4
e146c38 Update README to show provided serializer
Martin Grotzke authored
5 * ArraysAsListSerializer - serializer for lists created via Arrays#asList(Object...)
6 * ClassSerializer - for class instances
7 * CollectionsEmptyListSerializer - for Collections#EMPTY_LIST or lists created via Collections#emptyList()
8 * CollectionsEmptyMapSerializer - for Collections#EMPTY_MAP or maps created via Collections#emptyMap()
9 * CollectionsEmptySetSerializer - for Collections#EMPTY_SET or sets created via Collections#emptySet()
10 * CollectionsSingletonListSerializer - for lists created via Collections#singletonList(Object)
11 * CollectionsSingletonMapSerializer - for maps created via Collections#singletonMap(Object, Object)
12 * CollectionsSingletonSetSerializer - for sets created via Collections#singleton(Object)
13 * CopyForIterateCollectionSerializer - creates a copy of the source collection for writing object data.
14 * CopyForIterateMapSerializer - creates a copy of the source map for writing object data.
15 * CurrencySerializer - serializer for Currency
16 * EnumMapSerializer - serializer for EnumMap
17 * EnumSetSerializer - serializer for EnumSet
18 * GregorianCalendarSerializer - optimized serializer for (Gregorian)Calendar (24 bytes vs. 1323 bytes with FieldSerializer)
19 * JdkProxySerializer - for jdk proxies (proxies created via Proxy.newProxyInstance)
20 * KryoReflectionFactorySupport - kryo specialization that uses sun's ReflectionFactory to create new instances for classes without a default constructor
21 * ReferenceFieldSerializerReflectionFactorySupport - a ReferenceFieldSerializer specialization that uses sun's ReflectionFactory to create new instances
22 * StringBufferSerializer - optimized serializer for StringBuffer
23 * StringBuilderSerializer - optimized serializer for StringBuilder
d7c6de2 Add SubListSerializer as serializer for lists created via List.subLis…
Martin Grotzke authored
24 * SubListSerializer - serializer for lists created via List#subList(int, int)
e146c38 Update README to show provided serializer
Martin Grotzke authored
25 * SynchronizedCollectionsSerializer - for synchronized Collections and Maps created via Collections.synchronized*.
26 * UnmodifiableCollectionsSerializer - for unmodifiable Collections and Maps created via Collections.unmodifiable*.
27
28 * cglib/CGLibProxySerializer - serializer for CGLib proxies
29 * jodatime/JodaDateTimeSerializer - serializer for joda's DateTime
30 * wicket/MiniMapSerializer - serializer for wicket's MiniMap
31
32
43aad37 Add section that shows how to register serializers to README
Martin Grotzke authored
33 # Usage
27889fa Use more general words regarding the used build tool vs. maven/ivy.
Martin Grotzke authored
34 To be able to use the serializers you have to add the jar to your classpath. If your build tool support maven repositories you can use this dependency:
2071c35 Fix README / markup formatting so that maven deps definition is visible
Martin Grotzke authored
35
baa1955 Update README with hint regarding the maven dependency definition.
Martin Grotzke authored
36 <dependency>
37 <groupId>de.javakaffee</groupId>
38 <artifactId>kryo-serializers</artifactId>
39 <version>0.9</version>
40 </dependency>
2071c35 Fix README / markup formatting so that maven deps definition is visible
Martin Grotzke authored
41
baa1955 Update README with hint regarding the maven dependency definition.
Martin Grotzke authored
42 It's available in maven central, so you don't need an additional repository definition.
43 If you're managing the classpath differently you can get the jar from the downloads section or [download from maven central](http://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/).
ee98f36 Add hint regarding the jar to add to the classpath to the README
Martin Grotzke authored
44
baa1955 Update README with hint regarding the maven dependency definition.
Martin Grotzke authored
45 After that's done you can register the custom serializers at the kryo instance. The following code snippet shows how this is done for serializers that can be registered statically (directly for a known class).
43aad37 Add section that shows how to register serializers to README
Martin Grotzke authored
46
47 kryo.register( Arrays.asList( "" ).getClass(), new ArraysAsListSerializer( kryo ) );
48 kryo.register( Class.class, new ClassSerializer( kryo ) );
49 kryo.register( Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer() );
50 kryo.register( Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer() );
51 kryo.register( Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer() );
52 kryo.register( Collections.singletonList( "" ).getClass(), new CollectionsSingletonListSerializer( kryo ) );
53 kryo.register( Collections.singleton( "" ).getClass(), new CollectionsSingletonSetSerializer( kryo ) );
54 kryo.register( Collections.singletonMap( "", "" ).getClass(), new CollectionsSingletonMapSerializer( kryo ) );
55 kryo.register( Currency.class, new CurrencySerializer( kryo ) );
56 kryo.register( GregorianCalendar.class, new GregorianCalendarSerializer() );
57 kryo.register( InvocationHandler.class, new JdkProxySerializer( kryo ) );
58 kryo.register( StringBuffer.class, new StringBufferSerializer( kryo ) );
59 kryo.register( StringBuilder.class, new StringBuilderSerializer( kryo ) );
60 UnmodifiableCollectionsSerializer.registerSerializers( kryo );
61 SynchronizedCollectionsSerializer.registerSerializers( kryo );
62
cb39563 Add usage hint for cglib, joda datetime and wicket minimap serializer…
Martin Grotzke authored
63 // custom serializers for non-jdk libs
64
65 // register CGLibProxySerializer, works in combination with the appropriate action in handleUnregisteredClass (see below)
66 kryo.register( CGLibProxySerializer.CGLibProxyMarker.class, new CGLibProxySerializer( kryo ) );
67 // joda datetime
68 kryo.register( DateTime.class, new JodaDateTimeSerializer() );
69 // wicket
70 kryo.register( MiniMap.class, new MiniMapSerializer( kryo ) );
71
ee98f36 Add hint regarding the jar to add to the classpath to the README
Martin Grotzke authored
72 The following code snippet shows how to use the `KryoReflectionFactorySupport` (can only be used with sun/oracly jdk!) and how other serializers are registered via the `newSerializer` method. If you don't want to use the `KryoReflectionFactorySupport` you can override the `newSerializer` method for your `new Kryo()` instance.
43aad37 Add section that shows how to register serializers to README
Martin Grotzke authored
73
74 final Kryo kryo = new KryoReflectionFactorySupport() {
6e0d4e2 Fix tabs/white spaces in README
Martin Grotzke authored
75
76 @Override
77 public Serializer newSerializer(final Class clazz) {
78 if ( EnumSet.class.isAssignableFrom( clazz ) ) {
79 return new EnumSetSerializer( this );
80 }
81 if ( EnumMap.class.isAssignableFrom( clazz ) ) {
82 return new EnumMapSerializer( this );
83 }
84 if ( SubListSerializer.canSerialize( clazz ) ) {
85 return new SubListSerializer( this );
86 }
87 if ( ClassWithEvilDefaultConstructor.class.isAssignableFrom( type ) ) {
88 // for this class we want to create new instances via ReflectionFactory, not by calling the default constructor
89 // can only be used with sun/oracle jdk
90 final ReferenceFieldSerializerReflectionFactorySupport result = new ReferenceFieldSerializerReflectionFactorySupport( _kryo, type );
91 result.setIgnoreSyntheticFields( false );
92 return result;
93 }
94 if ( copyCollectionsForSerialization ) {
95 if ( Collection.class.isAssignableFrom( clazz ) ) {
96 return new CopyForIterateCollectionSerializer( this );
97 }
98 if ( Map.class.isAssignableFrom( clazz ) ) {
99 return new CopyForIterateMapSerializer( this );
100 }
101 }
102 return super.newSerializer( clazz );
103 }
104
105 @Override
106 public boolean handleUnregisteredClass( final Class<?> type ) {
107 // see if the given class is a cglib proxy
108 if ( CGLibProxySerializer.canSerialize( type ) ) {
109 // register the CGLibProxySerializer for this class
110 _kryo.register( type, _kryo.getRegisteredClass( CGLibProxySerializer.CGLibProxyMarker.class ) );
111 return true;
112 }
113 return false;
114 }
115
43aad37 Add section that shows how to register serializers to README
Martin Grotzke authored
116 };
117
118
40ad916 Update docs
Martin Grotzke authored
119 # Where to get help
53c4727 Fix links and link names
Martin Grotzke authored
120 You can [contact me via github](https://github.com/inbox/new/magro) or [submit an issue](https://github.com/magro/kryo-serializers/issues).
40ad916 Update docs
Martin Grotzke authored
121
122 # How to contribute
53c4727 Fix links and link names
Martin Grotzke authored
123 If you want to contribute to this project you can fork the [sources on github](https://github.com/magro/kryo-serializers), make your changes and submit a pull request. Alternatively you can [submit an issue](https://github.com/magro/kryo-serializers/issues) with a patch attached.
Something went wrong with that request. Please try again.