Example usage for com.badlogic.gdx.utils Json fromJson

List of usage examples for com.badlogic.gdx.utils Json fromJson

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils Json fromJson.

Prototype

public <T> T fromJson(Class<T> type, Class elementType, String json) 

Source Link

Usage

From source file:es.eucm.ead.buildtools.GenerateFieldClasses.java

License:Open Source License

private static String buildCode(Files files, Json json, String originJsonSchemaPath, String targetPackageName,
        String targetDirectory, boolean mainClass) {
    FileHandle fh = files.internal(originJsonSchemaPath);

    JsonValue next = json.fromJson(null, null, fh);
    next = next.child();/*  w  w  w . java 2  s . c  o m*/

    String fieldsCode = "";
    String headerCode = "";
    String targetClassName = "";
    while ((next = next.next()) != null) {
        if (next.name().equals("properties")) {
            JsonValue nextProperty = next.child();
            fieldsCode += buildFields(nextProperty);
            break;
        } else if (next.name().equals("extends")) {
            String relativeParentJsonSchemaPath = next.child().asString();
            // Calculate directory to find parent class
            String parentJsonSchemaPath = originJsonSchemaPath.substring(0,
                    originJsonSchemaPath.lastIndexOf("/") + 1) + relativeParentJsonSchemaPath;
            fieldsCode += buildCode(files, json, parentJsonSchemaPath, null, null, false);
        } else if (next.name().equals("javaType") && mainClass) {
            String javaType = next.asString();
            String originalClassName = javaType.substring(javaType.lastIndexOf(".") + 1, javaType.length());
            targetClassName = originalClassName + "Fields";
            headerCode = getClassHeader(originalClassName, targetPackageName, targetClassName);
        }
    }

    if (mainClass) {
        if (fieldsCode.length() > 0) {
            System.out.println("Generating code for class " + targetClassName);
            String classCode = headerCode + fieldsCode + "}";
            writeClass(files, classCode, targetPackageName, targetClassName, targetDirectory);
            return classCode;
        } else {
            System.out.println("Skipping class " + targetClassName + " (no properties)");
            return "";
        }
    } else {
        return fieldsCode;
    }
}

From source file:es.eucm.ead.editor.exporter.ExporterApplication.java

License:Open Source License

/**
 * Iterates recursively through the given {@code directory} loading any
 * {@link ModelEntity} found, which is placed into the {@code entities}. To
 * determine if a file is an entity, it just checks that it has json
 * extension./*from w w  w .j a  v  a  2 s .co  m*/
 * 
 * @param json
 *            The {@link Json} object provided by LibGDX to parse json files
 *            into ModelEntities.
 * @param directory
 *            The directory that may contain {@link ModelEntity}s. If it is
 *            {@code null} or it is not a directory, a
 *            {@link RuntimeException} is thrown.
 * @param entities
 *            The map loaded entities are stored into.
 * @throws RuntimeException
 *             If {@code directory} is not valid
 */
private static void loadAllEntities(Json json, FileHandle directory, Map<String, Object> entities,
        String prefix) {
    if (directory == null || !directory.exists() || !directory.isDirectory())
        throw new RuntimeException(
                "The directory provided is not valid (null, does not exist or it is not a directory): "
                        + (directory != null ? directory.file().getAbsolutePath() : null));

    for (FileHandle child : directory.list()) {
        if (child.isDirectory()) {
            loadAllEntities(json, child, entities, prefix + child.name() + "/");
        } else if (JsonExtension.hasJsonExtension(child.extension())
                && !child.name().toLowerCase().equals(ModelStructure.DESCRIPTOR_FILE)) {
            ModelEntity newScene = json.fromJson(ModelEntity.class, null, child);
            entities.put(prefix + child.name(), newScene);
        }

    }
}