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

com.artemis.io.ComponentLookupSerializer Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package com.artemis.io;

import com.artemis.Component;
import com.artemis.World;
import com.artemis.utils.reflect.ClassReflection;
import com.artemis.utils.reflect.ReflectionException;
import com.esotericsoftware.jsonbeans.Json;
import com.esotericsoftware.jsonbeans.JsonSerializer;
import com.esotericsoftware.jsonbeans.JsonValue;

import java.util.IdentityHashMap;
import java.util.Map;

public class ComponentLookupSerializer implements JsonSerializer {
	private World world;
	private IdentityHashMap, String> componentMap;

	public ComponentLookupSerializer(World world) {
		this.world = world;
	}

	public void setComponentMap(IdentityHashMap, String> componentMap) {
		this.componentMap = componentMap;
	}

	@Override
	public void write(Json json, IdentityHashMap object, Class knownType) {
		json.writeObjectStart();
		for (Map.Entry, String> entry : componentMap.entrySet()) {
			json.writeValue(entry.getKey().getName(), entry.getValue());
		}
		json.writeObjectEnd();
	}

	@Override
	public IdentityHashMap read(Json json, JsonValue jsonData, Class type) {
		IdentityHashMap, String> componentMap
				= new IdentityHashMap, String>();

		JsonValue component = jsonData.child;
		try {
			while (component != null) {
				Class c = ClassReflection.forName(component.name());
				componentMap.put(c, component.asString());
				component = component.next;
			}
		} catch (ReflectionException e) {
			throw new RuntimeException(e);
		}

		return componentMap;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy