Example usage for com.badlogic.gdx.utils JsonWriter JsonWriter

List of usage examples for com.badlogic.gdx.utils JsonWriter JsonWriter

Introduction

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

Prototype

public JsonWriter(Writer writer) 

Source Link

Usage

From source file:com.andgate.ikou.io.LevelService.java

License:Open Source License

public static void write(final Level level) throws IOException {
    // Shrinking causes json to write output forever.
    // FIXME: Compress the level :)
    //level.shrink();

    String levelFileName = level.getName() + Constants.LEVEL_EXTENSION;
    FileHandle levelFile = Gdx.files.external(Constants.LEVELS_EXTERNAL_PATH + levelFileName);

    OutputStream levelOut = new GZIPOutputStream(levelFile.write(false));
    Writer tmpWriter = new BufferedWriter(new OutputStreamWriter(levelOut));
    JsonWriter jsonWriter = new JsonWriter(tmpWriter);

    try {//  www.  ja v a 2 s .c  o m
        levelOut.write(level.getFloors().length - 1);
        Gson gson = new Gson();
        gson.toJson(level, jsonWriter);
    } finally {
        jsonWriter.close();
    }
}

From source file:com.github.fauu.helix.manager.AreaManager.java

License:Open Source License

public void create(String name, int width, int length) {
    Json json = new Json();

    IntVector2 dimensions = new IntVector2(width, length);

    FileHandle file = Gdx.files.internal("area/" + name + ".json");

    try {// ww  w .  j a  v a2  s  . c om
        json.setWriter(new JsonWriter(new FileWriter(file.file())));
    } catch (IOException e) {
        e.printStackTrace();
    }

    json.writeObjectStart();
    json.writeValue("width", dimensions.x);
    json.writeValue("length", dimensions.y);
    json.writeArrayStart("tiles");
    for (int y = 0; y < dimensions.y; y++) {
        for (int x = 0; x < dimensions.x; x++) {
            json.writeObjectStart();
            json.writeValue("permissions", TilePermission.LEVEL0.toString());
            json.writeObjectEnd();
        }
    }
    json.writeArrayEnd();
    json.writeObjectEnd();

    try {
        json.getWriter().close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.github.fauu.helix.manager.AreaManager.java

License:Open Source License

public void save() {
    Json json = new Json();

    String name = nameMapper.get(area).get();

    FileHandle file = Gdx.files.internal("area/" + name + ".json");

    IntVector2 dimensions = dimensionsMapper.get(area).get();

    AreaType type = areaTypeMapper.get(area).get();

    try {//from   w w  w  .  j a v  a  2  s  .c  om
        json.setWriter(new JsonWriter(new FileWriter(file.file())));
    } catch (IOException e) {
        e.printStackTrace();
    }

    json.writeObjectStart();
    json.writeValue("width", dimensions.x);
    json.writeValue("length", dimensions.y);
    json.writeValue("type", type);
    json.writeArrayStart("tiles");
    Tile[][] tiles = tilesMapper.get(area).get();
    for (int y = 0; y < dimensions.y; y++) {
        for (int x = 0; x < dimensions.x; x++) {
            Tile tile = tiles[y][x];

            json.writeObjectStart();

            json.writeValue("permissions", tile.getPermissions().toString());

            if (tile.getAreaPassage() != null) {
                TileAreaPassage passage = tile.getAreaPassage();

                json.writeObjectStart("passage");

                json.writeValue("area", passage.getTargetAreaName());

                json.writeObjectStart("position");
                json.writeValue("x", passage.getTargetCoords().x);
                json.writeValue("y", passage.getTargetCoords().y);
                json.writeObjectEnd();

                json.writeObjectEnd();
            }

            json.writeObjectEnd();
        }
    }
    json.writeArrayEnd();
    json.writeObjectEnd();

    try {
        json.getWriter().close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.libgdx.skin.editor.utils.scene2d.CustomSkin.java

License:Apache License

/** Store all resources in the specified skin JSON file. */
public boolean save(FileHandle skinFile) {
    StringWriter jsonText = new StringWriter();

    Json json = new Json();
    json.setWriter(new JsonWriter(jsonText));

    // ????? type
    json.writeObjectStart();/*w w  w. ja v  a2  s  .  c o  m*/
    for (Class<? extends Object> resType : saveResoureTypes) {
        ObjectMap<String, ? extends Object> typeResources = super.getAll(resType);
        if (emptyMap(typeResources)) {
            continue;
        }
        //  type 
        json.writeObjectStart(resType.getName());
        write1ResType(typeResources, resType, json);
        json.writeObjectEnd();

    }
    json.writeObjectEnd();

    PrettyPrintSettings settings = new PrettyPrintSettings();
    settings.outputType = OutputType.minimal;
    settings.singleLineColumns = 50;

    // ?
    skinFile.writeString(json.prettyPrint(jsonText.toString(), settings), false);
    return true;
}

From source file:com.ray3k.skincomposer.data.JsonData.java

License:Open Source License

public void writeFile(FileHandle fileHandle) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(stringWriter);
    jsonWriter.setOutputType(OutputType.minimal);
    Json json = new Json();
    json.setWriter(jsonWriter);/*from  w  w  w  .  ja  v a  2s . co m*/
    json.writeObjectStart();

    //fonts
    if (fonts.size > 0) {
        json.writeObjectStart(BitmapFont.class.getName());
        for (FontData font : fonts) {
            json.writeObjectStart(font.getName());
            json.writeValue("file", font.file.name());
            json.writeObjectEnd();
        }
        json.writeObjectEnd();
    }

    //colors
    if (colors.size > 0) {
        json.writeObjectStart(Color.class.getName());
        for (ColorData color : colors) {
            json.writeObjectStart(color.getName());
            json.writeValue("r", color.color.r);
            json.writeValue("g", color.color.g);
            json.writeValue("b", color.color.b);
            json.writeValue("a", color.color.a);
            json.writeObjectEnd();
        }
        json.writeObjectEnd();
    }

    //tinted drawables
    Array<DrawableData> tintedDrawables = new Array<>();
    for (DrawableData drawable : main.getProjectData().getAtlasData().getDrawables()) {
        if (drawable.tint != null || drawable.tintName != null) {
            tintedDrawables.add(drawable);
        }
    }
    if (tintedDrawables.size > 0) {
        json.writeObjectStart(TintedDrawable.class.getName());
        for (DrawableData drawable : tintedDrawables) {
            json.writeObjectStart(drawable.name);
            json.writeValue("name", DrawableData.proper(drawable.file.name()));
            if (drawable.tint != null) {
                json.writeObjectStart("color");
                json.writeValue("r", drawable.tint.r);
                json.writeValue("g", drawable.tint.g);
                json.writeValue("b", drawable.tint.b);
                json.writeValue("a", drawable.tint.a);
                json.writeObjectEnd();
            } else if (drawable.tintName != null) {
                json.writeValue("color", drawable.tintName);
            }
            json.writeObjectEnd();
        }
        json.writeObjectEnd();
    }

    //styles
    Array<Array<StyleData>> valuesArray = classStyleMap.values().toArray();
    for (int i = 0; i < Main.STYLE_CLASSES.length; i++) {
        Class clazz = Main.STYLE_CLASSES[i];
        Array<StyleData> styles = valuesArray.get(i);

        //check if any style has the mandatory fields necessary to write
        boolean hasMandatoryStyles = true;
        for (StyleData style : styles) {
            if (!style.hasMandatoryFields() || style.hasAllNullFields()) {
                hasMandatoryStyles = false;
                break;
            }
        }

        if (hasMandatoryStyles) {
            json.writeObjectStart(clazz.getName());
            for (StyleData style : styles) {
                if (style.hasMandatoryFields() && !style.hasAllNullFields()) {
                    json.writeObjectStart(style.name);
                    for (StyleProperty property : style.properties.values()) {

                        //if not optional, null, or zero
                        if (!property.optional || property.value != null && !(property.value instanceof Number
                                && MathUtils.isZero((float) (double) property.value))) {
                            json.writeValue(property.name, property.value);
                        }
                    }
                    json.writeObjectEnd();
                }
            }
            json.writeObjectEnd();
        }
    }

    json.writeObjectEnd();
    fileHandle.writeString(json.prettyPrint(stringWriter.toString()), false);
}

From source file:com.steelkiwi.patheditor.proj.ProjectDataConverter.java

License:Apache License

private static void saveScreenToJSON(String projPath, ScreenData scrData) throws Exception {
    String path = projPath + PATH_SEPARATOR + scrData.getJsonPath();
    File scrFile = new File(path);

    if (scrFile.exists()) {
        scrFile.delete();/* w w  w  . j  av a2s.c  o  m*/
    }
    scrFile.createNewFile();

    StringWriter strWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(strWriter);
    jsonWriter.object().object("screen").set("name", scrData.getName()).set("width", scrData.getWidth())
            .set("height", scrData.getHeight()).set("xmlPath", scrData.getXmlPath())
            .set("jsonPath", scrData.getJsonPath());

    if ((scrData.getBgImage() != null)) {
        jsonWriter.object("bg").set("name", scrData.getBgImage().name).set("type",
                (getWidgetType(scrData.getBgImage()) != null) ? getWidgetType(scrData.getBgImage()).ordinal()
                        : -1)
                .set("texturePath",
                        FileUtils.getElementRelativePath(projPath,
                                ((GdxImage) scrData.getBgImage()).getTexPath()))
                .set("scaleX", scrData.getBgImage().scaleX).set("scaleY", scrData.getBgImage().scaleY)
                .set("x", scrData.getBgImage().x).set("y", scrData.getBgImage().y)
                .set("angle", scrData.getBgImage().rotation).pop();
    }

    if ((scrData.getPath() != null) && (scrData.getPath().getPath() != null)) {
        jsonWriter.object("path").set("xmlPath", scrData.getPath().getXmlPath())
                .set("jsonPath", scrData.getPath().getJsonPath()).pop();
    }

    jsonWriter.pop();
    jsonWriter.pop();
    jsonWriter.close();

    FileWriter writer = new FileWriter(new File(path));
    writer.write(strWriter.toString());
    writer.close();
}

From source file:com.steelkiwi.patheditor.proj.ProjectDataConverter.java

License:Apache License

private static void savePathToJSON(String projPath, GdxPath gdxPath) throws Exception {
    String path = projPath + gdxPath.getJsonPath();
    File pathFile = new File(path);

    if (pathFile.exists()) {
        pathFile.delete();//from   w w w. ja  va2s  .  c o  m
    }
    pathFile.createNewFile();

    StringWriter strWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(strWriter);
    jsonWriter.object().object("path").set("name", gdxPath.getName()).set("pointsCnt", gdxPath.getPointsCnt())
            .set("controlColor", gdxPath.getControlColor()).set("segmentColor", gdxPath.getSegmentColor())
            .set("selectColor", gdxPath.getSelectColor()).set("xmlPath", gdxPath.getXmlPath())
            .set("jsonPath", gdxPath.getJsonPath());

    if ((gdxPath.getPath() != null) && (gdxPath.getPath().getPathVerticesCount() > 0)) {
        jsonWriter.array("controlVertices");
        Vector3 controlVertex;
        for (int i = 0; i < gdxPath.getControlPath().size(); i++) {
            controlVertex = gdxPath.getControlPath().get(i);
            jsonWriter.object().set("id", i).set("x", controlVertex.x).set("y", controlVertex.y).pop();
        }
        jsonWriter.pop();

        jsonWriter.array("vertices");
        PathVertex vertex;
        for (int i = 0; i < gdxPath.getPath().getPathVerticesCount(); i++) {
            vertex = gdxPath.getPath().getPathVertexByIndex(i);
            jsonWriter.object().set("id", i).set("x", vertex.getPosition().x).set("y", vertex.getPosition().y)
                    .set("tanX", vertex.getTangentNornal().x).set("tanY", vertex.getTangentNornal().y)
                    .set("angle", vertex.getAngle()).pop();
        }
        jsonWriter.pop();
    }

    jsonWriter.pop();
    jsonWriter.pop();
    jsonWriter.close();

    FileWriter writer = new FileWriter(new File(path));
    writer.write(strWriter.toString());
    writer.close();
}

From source file:mt.Json.java

License:Apache License

/** Sets the writer where JSON output will go. This is only necessary when not using the toJson methods. */
public void setWriter(Writer writer) {
    if (!(writer instanceof JsonWriter))
        writer = new JsonWriter(writer);
    this.writer = (JsonWriter) writer;
    this.writer.setOutputType(outputType);
}