use of com.badlogic.gdx.utils.Json in project libgdx by libgdx.
the class ReflectionTest method create.
@Override
public void create() {
font = new BitmapFont();
batch = new SpriteBatch();
try {
Vector2 fromDefaultConstructor = ClassReflection.newInstance(Vector2.class);
println("From default constructor: " + fromDefaultConstructor);
Method mSet = ClassReflection.getMethod(Vector2.class, "set", float.class, float.class);
mSet.invoke(fromDefaultConstructor, 10, 11);
println("Set to 10/11: " + fromDefaultConstructor);
Constructor copyConstroctor = ClassReflection.getConstructor(Vector2.class, Vector2.class);
Vector2 fromCopyConstructor = (Vector2) copyConstroctor.newInstance(fromDefaultConstructor);
println("From copy constructor: " + fromCopyConstructor);
Method mMul = ClassReflection.getMethod(Vector2.class, "scl", float.class);
println("Multiplied by 2; " + mMul.invoke(fromCopyConstructor, 2));
Method mNor = ClassReflection.getMethod(Vector2.class, "nor");
println("Normalized: " + mNor.invoke(fromCopyConstructor));
Vector2 fieldCopy = new Vector2();
Field fx = ClassReflection.getField(Vector2.class, "x");
Field fy = ClassReflection.getField(Vector2.class, "y");
fx.set(fieldCopy, fx.get(fromCopyConstructor));
fy.set(fieldCopy, fy.get(fromCopyConstructor));
println("Copied field by field: " + fieldCopy);
Json json = new Json();
String jsonString = json.toJson(fromCopyConstructor);
Vector2 fromJson = json.fromJson(Vector2.class, jsonString);
println("JSON serialized: " + jsonString);
println("JSON deserialized: " + fromJson);
fromJson.x += 1;
fromJson.y += 1;
println("JSON deserialized + 1/1: " + fromJson);
Object array = ArrayReflection.newInstance(int.class, 5);
ArrayReflection.set(array, 0, 42);
println("Array int: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
array = ArrayReflection.newInstance(String.class, 5);
ArrayReflection.set(array, 0, "test string");
println("Array String: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
} catch (Exception e) {
message = "FAILED: " + e.getMessage() + "\n";
message += e.getClass();
}
}
use of com.badlogic.gdx.utils.Json in project Entitas-Java by Rubentxu.
the class ProfileManagerGDX method persist.
@Override
public void persist(P profile) {
FileHandle profileDataFile = Gdx.files.local(preferencesManager.PROFILE_DATA_FILE);
Json json = new Json();
String profileAsText = json.toJson(profile);
profileAsText = Base64Coder.encodeString(profileAsText);
profileDataFile.writeString(profileAsText, false);
}
use of com.badlogic.gdx.utils.Json in project Entitas-Java by Rubentxu.
the class ProfileManagerGDX method retrieveProfile.
@Override
public P retrieveProfile() {
FileHandle profileDataFile = Gdx.files.local(preferencesManager.PROFILE_DATA_FILE);
Json json = new Json();
if (profileDataFile.exists()) {
try {
String profileAsText = profileDataFile.readString().trim();
if (profileAsText.matches("^[A-Za-z0-9/+=]+$")) {
profileAsText = Base64Coder.decodeString(profileAsText);
}
profile = (P) json.fromJson(profile.getClass(), profileAsText);
} catch (Exception e) {
FileHandle initProfileDataFile = Gdx.files.internal(preferencesManager.INIT_PROFILE_DATA_FILE);
profile = (P) json.fromJson(profile.getClass(), initProfileDataFile.readString().trim());
persist(profile);
}
} else {
FileHandle initProfileDataFile = Gdx.files.internal(preferencesManager.INIT_PROFILE_DATA_FILE);
profile = (P) json.fromJson(profile.getClass(), initProfileDataFile.readString().trim());
persist(profile);
}
return profile;
}
use of com.badlogic.gdx.utils.Json in project bdx by GoranM.
the class Mesh method serialized.
public String serialized() {
HashMap<String, Float[]> out = new HashMap<String, Float[]>();
Float[] d;
for (int i = 0; i < materials.size(); i++) {
d = new Float[getVertexCount(i) * Bdx.VERT_STRIDE];
for (int v = 0; v < getVertexCount(i); v++) {
int vi = v * 8;
Vector3f p = vertPos(i, v);
d[vi] = p.x;
d[vi + 1] = p.y;
d[vi + 2] = p.z;
p = vertNor(i, v);
d[vi + 3] = p.x;
d[vi + 4] = p.y;
d[vi + 5] = p.z;
Vector2f u = vertUV(i, v);
d[vi + 6] = u.x;
d[vi + 7] = u.y;
}
out.put(materials.get(i).name(), d);
}
return new Json().toJson(out);
}
use of com.badlogic.gdx.utils.Json in project gdx-skineditor by cobolfoo.
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) {
// 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) {
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, type);
} 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);
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;
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);
}
// the font to.
if (scaledSize != -1)
font.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);
TintedDrawable td = new TintedDrawable();
td.name = name;
td.color = color;
td.drawable = newDrawable(name, color);
return td;
// return newDrawable(name, color);
}
});
return json;
}
Aggregations