You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Stefan Schönig <st...@sea-gmbh.com> on 2012/05/14 17:08:42 UTC

How to encode/decode java.util.Properties

I'm not able to Encode/Decode a class from the Standard-JDK ( 
java.util.Properties).
The inserted key/value pair is not available after decode.
Any idea what goint wrong?!?

Thank in advance!


Code of the application:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.avro.*;
import org.apache.avro.io.*;
import org.apache.avro.reflect.*;

public class AvroPropertiesTest {

	public static void main(String[] args) throws IOException {
		ReflectData reflectData = ReflectData.get();
		Schema schema = reflectData.getSchema(Properties.class);
		
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		Encoder e = EncoderFactory.get().binaryEncoder(os, null);

		Properties properties = new Properties();
		properties.put("Hello", "World");

		System.out.println("properties: " + properties);
		
		DatumWriter<Properties> writer =
			new ReflectDatumWriter<Properties>(schema);
		writer.write(properties, e);
		e.flush();

		ReflectDatumReader<Properties> reader = new 
ReflectDatumReader<Properties>(
				schema);
		Decoder decoder = DecoderFactory.get().binaryDecoder(os.toByteArray(),
				null);
		Properties decodedProperties = (Properties) reader.read(null, decoder);

		System.out.println("decodedProperties: " + decodedProperties);
	}
}




-- 
This email was Anti Virus checked by Astaro Security Gateway. http://www.astaro.com

Re: How to encode/decode java.util.Properties

Posted by Stefan Schönig <st...@sea-gmbh.com>.
Ok, I have tried to answer the question by myself:

1. ReflectData ignores built-in classes
========================================
I found the following code inside the Method "private Collection<Field> 
getFields(Class recordClass)":

   if (c.getPackage() != null
           && c.getPackage().getName().startsWith("java."))
         break;

The reason why built-in classes are ignored might be documented 
somewhere. I didn’t find anything about the reason.
[AVRO-797] might have something to do with this issue.

The fields within the Properties.class (inherited from Hashtable) are 
declared as transient. Even if the code above is comment out, the class 
is not serialized through Avro. But: Java is able to serialize this 
class?!?

Why is Java able to serialize this class and Avro not?
======================================================
First: The serialization process of java does have the same problem as 
Avro: For sure, the fields are transient for boths APIs. But here is the 
extra edge: A closer look into the class hashtable shows that the 
specific methods "readObject" and "writeObject" (used for serialization) 
are overwritten. Avro does not consider these methods (whichs write 
specific binary-content direct into the outputstream).
I think that's the reason why the class is serialized by the JAVA 
Serialization and not by Avro.

How can this problem be solved?
===============================
Don't use built-in classes in your data model.

Regards

Stefan

-- 
This email was Anti Virus checked by Astaro Security Gateway. http://www.astaro.com