public NinePatch read (Json json, Object jsonData, Class type) {
if (jsonData instanceof String) return getResource((String)jsonData, NinePatch.class);
if (jsonData instanceof Array) {
TextureRegion[] regions = json.readValue(TextureRegion[].class, jsonData);
if (regions.length == 1) return new NinePatch(regions[0]);
return new NinePatch(regions);
} else {
ObjectMap map = (ObjectMap)jsonData;
NinePatch ninePatch;
if (map.containsKey("regions"))
ninePatch = new NinePatch(json.readValue("regions", TextureRegion[].class, jsonData));
else if (map.containsKey("region"))
ninePatch = new NinePatch(json.readValue("region", TextureRegion.class, jsonData));
else
ninePatch = new NinePatch(json.readValue(TextureRegion.class, jsonData));
// throw new SerializationException("Missing ninepatch regions: " + map);
if (map.containsKey("color")) ninePatch.setColor(json.readValue("color", Color.class, jsonData));
return ninePatch;
}
}
});
json.setSerializer(Color.class, new Serializer<Color>() {
public void write (Json json, Color color, Class valueType) {
json.writeObjectStart();
json.writeFields(color);
json.writeObjectEnd();
}
public Color read (Json json, Object jsonData, Class type) {
if (jsonData instanceof String) return getResource((String)jsonData, Color.class);
ObjectMap map = (ObjectMap)jsonData;
float r = json.readValue("r", float.class, 0f, jsonData);
float g = json.readValue("g", float.class, 0f, jsonData);
float b = json.readValue("b", float.class, 0f, jsonData);
float a = json.readValue("a", float.class, 1f, jsonData);
return new Color(r, g, b, a);
}
});
json.setSerializer(TintedNinePatch.class, new Serializer() {
public void write (Json json, Object tintedPatch, Class valueType) {
json.writeObjectStart();
json.writeField(tintedPatch, "name");
json.writeField(tintedPatch, "color");
json.writeObjectEnd();
}
public Object read (Json json, Object jsonData, Class type) {
String name = json.readValue("name", String.class, jsonData);
Color color = json.readValue("color", Color.class, jsonData);
return new NinePatch(getResource(name, NinePatch.class), color);
}
});
return json;
}