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

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

Introduction

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

Prototype

public void setWriter(Writer writer) 

Source Link

Document

Sets the writer where JSON output will be written.

Usage

From source file:com.bladecoder.engine.actions.DisableActionAction.java

License:Apache License

public void setAction(Action a) {
    action = a;//from   w w w  . j  a  va2  s  .  c  o  m
    Json json = new Json();
    StringWriter buffer = new StringWriter();
    json.setWriter(buffer);
    ActionUtils.writeJson(a, json);
    serializedAction = buffer.toString();
}

From source file:com.bladecoder.engine.model.World.java

License:Apache License

public void saveWorldDesc(FileHandle file) throws IOException {

    float scale = EngineAssetManager.getInstance().getScale();

    Json json = new Json();
    json.setOutputType(OutputType.javascript);

    SerializationHelper.getInstance().setMode(Mode.MODEL);

    json.setWriter(new StringWriter());

    json.writeObjectStart();/* w w  w  .  j  a v a 2  s . c om*/
    json.writeValue("width", width / scale);
    json.writeValue("height", height / scale);
    json.writeValue("initChapter", initChapter);
    verbs.write(json);
    json.writeObjectEnd();

    String s = null;

    if (EngineLogger.debugMode())
        s = json.prettyPrint(json.getWriter().getWriter().toString());
    else
        s = json.getWriter().getWriter().toString();

    Writer w = file.writer(false, "UTF-8");
    w.write(s);
    w.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 {/* w  ww.j av a2 s  .  c  o  m*/
        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 {//w w  w  .  ja  va 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();//from w  ww  . j a  v  a2  s.  com
    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);
    json.writeObjectStart();//  www . j  av a  2s.c  om

    //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:io.piotrjastrzebski.dungen.DungeonGenerator.java

License:Apache License

public String toJson(JsonWriter.OutputType outputType, boolean pretty) {
    Json json = new Json(outputType);
    StringWriter writer = new StringWriter();
    json.setWriter(writer);
    json.writeObjectStart();//  w ww.j a v  a2 s  .c  o m
    json.writeValue("grid-size", settings.getGridSize());
    json.writeArrayStart("rooms");
    for (Room room : rooms) {
        if (room.isUnused())
            continue;
        json.writeObjectStart();
        json.writeValue("id", room.id);
        if (room.isMain) {
            json.writeValue("type", "main");
        } else if (room.isHallway) {
            json.writeValue("type", "hallway");
        } else if (room.isExtra) {
            json.writeValue("type", "extra");
        }
        json.writeValue("bounds", room.bounds);
        json.writeObjectEnd();
    }
    json.writeArrayEnd();
    json.writeArrayStart("paths");
    for (HallwayPath path : paths) {
        json.writeObjectStart();
        json.writeValue("id", path.id);
        json.writeValue("start-room", path.roomA.id);
        json.writeValue("end-room", path.roomB.id);
        json.writeValue("start", path.start);
        if (path.hasBend) {
            json.writeValue("mid", path.bend);
        }
        json.writeValue("end", path.end);
        json.writeArrayStart("overlaps");
        for (Room room : path.overlap) {
            json.writeValue(room.id);
        }
        json.writeArrayEnd();
        json.writeObjectEnd();
    }
    json.writeArrayEnd();
    json.writeObjectEnd();

    if (pretty) {
        return json.prettyPrint(writer.toString());
    } else {
        return writer.toString();
    }
}