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

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

Introduction

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

Prototype

public void setOutputType(OutputType outputType) 

Source Link

Document

Sets the type of JSON output.

Usage

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  ww w . j a  va  2  s .  c o 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);
}