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

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

Introduction

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

Prototype

public void close() throws IOException 

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 {//  w ww.  java2 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.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();//from  w ww  .java2s .  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 ww .  j a  v a2s.  com*/
    }
    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();
}