All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.lib.json.SpecialHandler Maven / Gradle / Ivy

Go to download

The bndlib project is a general library to be used with OSGi bundles. It contains lots of cool functionality that calculates dependencies, etc.

There is a newer version: 2.4.0
Show newest version
package aQute.lib.json;

import java.io.*;
import java.lang.reflect.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class SpecialHandler extends Handler {
	@SuppressWarnings("rawtypes")
	final Class						type;
	final Method					valueOf;
	final Constructor< ? >			constructor;
	final static SimpleDateFormat	sdf	= new SimpleDateFormat();

	public SpecialHandler(Class< ? > type, Constructor< ? > constructor, Method valueOf) {
		this.type = type;
		this.constructor = constructor;
		this.valueOf = valueOf;
	}

	@Override
	void encode(Encoder app, Object object, Map visited) throws IOException, Exception {
		StringHandler.string(app, object.toString());
	}

	@Override
	Object decode(Decoder dec, String s) throws Exception {
		if (type == Pattern.class)
			return Pattern.compile(s);

		if (constructor != null)
			return constructor.newInstance(s);

		if (valueOf != null)
			return valueOf.invoke(null, s);

		throw new IllegalArgumentException("Do not know how to convert a " + type + " from a string");
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy