use of com.badlogic.gdx.utils.Json in project libgdx by libgdx.
the class TexturePacker method main.
public static void main(String[] args) throws Exception {
Settings settings = null;
String input = null, output = null, packFileName = "pack.atlas";
switch(args.length) {
case 4:
settings = new Json().fromJson(Settings.class, new FileReader(args[3]));
case 3:
packFileName = args[2];
case 2:
output = args[1];
case 1:
input = args[0];
break;
default:
System.out.println("Usage: inputDir [outputDir] [packFileName] [settingsFileName]");
System.exit(0);
}
if (output == null) {
File inputFile = new File(input);
output = new File(inputFile.getParentFile(), inputFile.getName() + "-packed").getAbsolutePath();
}
if (settings == null)
settings = new Settings();
process(settings, input, output, packFileName);
}
use of com.badlogic.gdx.utils.Json in project libgdx by libgdx.
the class ParticleEffectLoader method save.
/** Saves the effect to the given file contained in the passed in parameter. */
public void save(ParticleEffect effect, ParticleEffectSaveParameter parameter) throws IOException {
ResourceData<ParticleEffect> data = new ResourceData<ParticleEffect>(effect);
// effect assets
effect.save(parameter.manager, data);
// Batches configurations
if (parameter.batches != null) {
for (ParticleBatch<?> batch : parameter.batches) {
boolean save = false;
for (ParticleController controller : effect.getControllers()) {
if (controller.renderer.isCompatible(batch)) {
save = true;
break;
}
}
if (save)
batch.save(parameter.manager, data);
}
}
// save
Json json = new Json();
json.toJson(data, parameter.file);
}
use of com.badlogic.gdx.utils.Json in project libgdx by libgdx.
the class Skin method getJsonLoader.
protected Json getJsonLoader(final FileHandle skinFile) {
final Skin skin = this;
final Json json = new Json() {
public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) {
// If the JSON is a string but the type is not, look up the actual value by name.
if (jsonData.isString() && !ClassReflection.isAssignableFrom(CharSequence.class, type))
return get(jsonData.asString(), type);
return super.readValue(type, elementType, jsonData);
}
};
json.setTypeName(null);
json.setUsePrototypes(false);
json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {
public Skin read(Json json, JsonValue typeToValueMap, Class ignored) {
for (JsonValue valueMap = typeToValueMap.child; valueMap != null; valueMap = valueMap.next) {
try {
readNamedObjects(json, ClassReflection.forName(valueMap.name()), valueMap);
} catch (ReflectionException ex) {
throw new SerializationException(ex);
}
}
return skin;
}
private void readNamedObjects(Json json, Class type, JsonValue valueMap) {
Class addType = type == TintedDrawable.class ? Drawable.class : type;
for (JsonValue valueEntry = valueMap.child; valueEntry != null; valueEntry = valueEntry.next) {
Object object = json.readValue(type, valueEntry);
if (object == null)
continue;
try {
add(valueEntry.name, object, addType);
if (addType != Drawable.class && ClassReflection.isAssignableFrom(Drawable.class, addType))
add(valueEntry.name, object, Drawable.class);
} catch (Exception ex) {
throw new SerializationException("Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name, ex);
}
}
}
});
json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {
public BitmapFont read(Json json, JsonValue jsonData, Class type) {
String path = json.readValue("file", String.class, jsonData);
int scaledSize = json.readValue("scaledSize", int.class, -1, jsonData);
Boolean flip = json.readValue("flip", Boolean.class, false, jsonData);
Boolean markupEnabled = json.readValue("markupEnabled", Boolean.class, false, jsonData);
FileHandle fontFile = skinFile.parent().child(path);
if (!fontFile.exists())
fontFile = Gdx.files.internal(path);
if (!fontFile.exists())
throw new SerializationException("Font file not found: " + fontFile);
// Use a region with the same name as the font, else use a PNG file in the same directory as the FNT file.
String regionName = fontFile.nameWithoutExtension();
try {
BitmapFont font;
Array<TextureRegion> regions = skin.getRegions(regionName);
if (regions != null)
font = new BitmapFont(new BitmapFontData(fontFile, flip), regions, true);
else {
TextureRegion region = skin.optional(regionName, TextureRegion.class);
if (region != null)
font = new BitmapFont(fontFile, region, flip);
else {
FileHandle imageFile = fontFile.parent().child(regionName + ".png");
if (imageFile.exists())
font = new BitmapFont(fontFile, imageFile, flip);
else
font = new BitmapFont(fontFile, flip);
}
}
font.getData().markupEnabled = markupEnabled;
// Scaled size is the desired cap height to scale the font to.
if (scaledSize != -1)
font.getData().setScale(scaledSize / font.getCapHeight());
return font;
} catch (RuntimeException ex) {
throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
}
}
});
json.setSerializer(Color.class, new ReadOnlySerializer<Color>() {
public Color read(Json json, JsonValue jsonData, Class type) {
if (jsonData.isString())
return get(jsonData.asString(), Color.class);
String hex = json.readValue("hex", String.class, (String) null, jsonData);
if (hex != null)
return Color.valueOf(hex);
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(TintedDrawable.class, new ReadOnlySerializer() {
public Object read(Json json, JsonValue jsonData, Class type) {
String name = json.readValue("name", String.class, jsonData);
Color color = json.readValue("color", Color.class, jsonData);
Drawable drawable = newDrawable(name, color);
if (drawable instanceof BaseDrawable) {
BaseDrawable named = (BaseDrawable) drawable;
named.setName(jsonData.name + " (" + name + ", " + color + ")");
}
return drawable;
}
});
return json;
}
use of com.badlogic.gdx.utils.Json in project libgdx by libgdx.
the class JsonTest method create.
public void create() {
json = new Json();
// json.fromJson(Test1.class, //
// "{byteArrayField:[-1\n,-2]}"
// );
// if (true) return;
Test1 test = new Test1();
test.booleanField = true;
test.byteField = 123;
test.charField = 'Z';
test.shortField = 12345;
test.intField = 123456;
test.longField = 123456789;
test.floatField = 123.456f;
test.doubleField = 1.23456d;
test.BooleanField = true;
test.ByteField = -12;
test.CharacterField = 'X';
test.ShortField = -12345;
test.IntegerField = -123456;
test.LongField = -123456789l;
test.FloatField = -123.3f;
test.DoubleField = -0.121231d;
test.stringField = "stringvalue";
test.byteArrayField = new byte[] { 2, 1, 0, -1, -2 };
test.map = new ObjectMap();
test.map.put("one", 1);
test.map.put("two", 2);
test.map.put("nine", 9);
test.stringArray = new Array();
test.stringArray.add("meow");
test.stringArray.add("moo");
test.objectArray = new Array();
test.objectArray.add("meow");
test.objectArray.add(new Test1());
test.someEnum = SomeEnum.b;
roundTrip(test);
test.someEnum = null;
roundTrip(test);
test = new Test1();
roundTrip(test);
test.stringArray = new Array();
roundTrip(test);
test.stringArray.add("meow");
roundTrip(test);
test.stringArray.add("moo");
roundTrip(test);
TestMapGraph objectGraph = new TestMapGraph();
testObjectGraph(objectGraph, "exoticTypeName");
test = new Test1();
test.map = new ObjectMap();
roundTrip(test);
test.map.put("one", 1);
roundTrip(test);
test.map.put("two", 2);
test.map.put("nine", 9);
roundTrip(test);
test.map.put("\nst\nuff\n", 9);
test.map.put("\r\nst\r\nuff\r\n", 9);
roundTrip(test);
equals(json.toJson("meow"), "meow");
equals(json.toJson("meow "), "\"meow \"");
equals(json.toJson(" meow"), "\" meow\"");
equals(json.toJson(" meow "), "\" meow \"");
equals(json.toJson("\nmeow\n"), "\\nmeow\\n");
equals(json.toJson(Array.with(1, 2, 3), null, int.class), "[1,2,3]");
equals(json.toJson(Array.with("1", "2", "3"), null, String.class), "[1,2,3]");
equals(json.toJson(Array.with(" 1", "2 ", " 3 "), null, String.class), "[\" 1\",\"2 \",\" 3 \"]");
equals(json.toJson(Array.with("1", "", "3"), null, String.class), "[1,\"\",3]");
System.out.println();
System.out.println("Success!");
}
use of com.badlogic.gdx.utils.Json in project libgdx by libgdx.
the class JsonTest method testObjectGraph.
private void testObjectGraph(TestMapGraph object, String typeName) {
Json json = new Json();
json.setTypeName(typeName);
json.setUsePrototypes(false);
json.setIgnoreUnknownFields(true);
json.setOutputType(OutputType.json);
String text = json.prettyPrint(object);
TestMapGraph object2 = json.fromJson(TestMapGraph.class, text);
if (object2.map.size() != object.map.size()) {
throw new RuntimeException("Too many items in deserialized json map.");
}
if (object2.objectMap.size != object.objectMap.size) {
throw new RuntimeException("Too many items in deserialized json object map.");
}
if (object2.arrayMap.size != object.arrayMap.size) {
throw new RuntimeException("Too many items in deserialized json map.");
}
}
Aggregations