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

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

Introduction

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

Prototype

public void setUsePrototypes(boolean usePrototypes) 

Source Link

Document

When true, field values that are identical to a newly constructed instance are not written.

Usage

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

License:Open Source License

public void save(FileHandle file) {
    moveImportedFiles(saveFile, file);//www  .  ja v a 2  s .com

    saveFile = file;
    putRecentFile(file.path());
    Json json = new Json(JsonWriter.OutputType.minimal);
    json.setUsePrototypes(false);
    file.writeString(json.prettyPrint(this), false);
    setChangesSaved(true);
}

From source file:org.ege.widget.StyleAtlas.java

License:Apache License

protected Json getJsonLoader(final FileHandle skinFile) {
    final Skin skin = this;

    final Json json = new Json() {
        public <T> T readValue(Class<T> type, Class elementType, Object jsonData) {
            // If the JSON is a string but the type is not, look up the actual value by name.
            if (jsonData instanceof String && !CharSequence.class.isAssignableFrom(type))
                return get((String) jsonData, type);
            return super.readValue(type, elementType, jsonData);
        }//from  w w w  .java 2  s. c  o m
    };

    json.setTypeName(null);
    json.setUsePrototypes(false);

    /*   ------------------------------------------------   */

    json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {
        public Skin read(Json json, Object jsonData, Class ignored) {
            ObjectMap<String, ObjectMap> typeToValueMap = (ObjectMap) jsonData;
            for (Entry<String, ObjectMap> typeEntry : typeToValueMap.entries()) {
                String className = typeEntry.key;
                ObjectMap<String, ObjectMap> valueMap = (ObjectMap) typeEntry.value;
                try {
                    readNamedObjects(json, Class.forName(className), valueMap);
                } catch (ClassNotFoundException ex) {
                    throw new SerializationException(ex);
                }
            }
            return skin;
        }

        private void readNamedObjects(Json json, Class type, ObjectMap<String, ObjectMap> valueMap) {
            Class addType = type == TintedDrawable.class ? Drawable.class : type;
            for (Entry<String, ObjectMap> valueEntry : valueMap.entries()) {
                String name = valueEntry.key;
                Object object = json.readValue(type, valueEntry.value);
                if (object == null)
                    continue;
                try {
                    add(name, object, addType);
                } catch (Exception ex) {
                    throw new SerializationException(
                            "Error reading " + type.getSimpleName() + ": " + valueEntry.key, ex);
                }
            }
        }
    });

    /*   ------------------------------------------------   */

    json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {
        public BitmapFont read(Json json, Object jsonData, Class type) {
            String path = json.readValue("file", String.class, jsonData);

            FileHandle fontFile = skinFile.parent().child(path);
            if (!fontFile.exists())
                fontFile = Gdx.files.internal(path);
            if (!fontFile.exists())
                throw new SerializationException("Font file not found: " + fontFile);

            // Use a region with the same name as the font, else use a PNG file in the same directory as the FNT file.
            String regionName = fontFile.nameWithoutExtension();
            try {
                TextureRegion region = skin.optional(regionName, TextureRegion.class);
                if (region != null)
                    return new BitmapFont(fontFile, region, false);
                else {
                    FileHandle imageFile = fontFile.parent().child(regionName + ".png");
                    if (imageFile.exists())
                        return new BitmapFont(fontFile, imageFile, false);
                    else
                        return new BitmapFont(fontFile, false);
                }
            } catch (RuntimeException ex) {
                throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
            }
        }
    });

    /*   ------------------------------------------------   */

    json.setSerializer(Color.class, new ReadOnlySerializer<Color>() {
        public Color read(Json json, Object jsonData, Class type) {
            if (jsonData instanceof String)
                return get((String) jsonData, Color.class);
            ObjectMap map = (ObjectMap) jsonData;
            float r = json.readValue("r", float.class, 0f, jsonData);
            float g = json.readValue("g", float.class, 0f, jsonData);
            float b = json.readValue("b", float.class, 0f, jsonData);
            float a = json.readValue("a", float.class, 1f, jsonData);
            return new Color(r, g, b, a);
        }
    });

    /*   ------------------------------------------------   */

    json.setSerializer(TintedDrawable.class, new ReadOnlySerializer() {
        public Object read(Json json, Object jsonData, Class type) {
            String name = json.readValue("name", String.class, jsonData);
            Color color = json.readValue("color", Color.class, jsonData);
            return newDrawable(name, color);
        }
    });

    /*   ------------------------------------------------   */

    //      json.setSerializer(Orientation.class, new Serializer<Orientation>() {
    //
    //         @Override
    //         public void write (Json json, Orientation object, Class knownType) {
    //            json.writeObjectStart();
    //            switch (object) {
    //               case HORIZONTAL:
    //                  json.writeValue("orientation", "HORIZONTAL");
    //                  break;
    //               case VERTICAL:
    //                  json.writeValue("orientation", "VERTICAL");
    //                  break;
    //               case LANDSCAPE:
    //                  json.writeValue("orientation", "LANDSCAPE");
    //                  break;
    //               case PORTRAIT:
    //                  json.writeValue("orientation", "PORTRAIT");
    //                  break;
    //            }
    //            json.writeObjectEnd();
    //         }
    //
    //         @Override
    //         public Orientation read (Json json, Object jsonData, Class type) {
    //            String ori = (String)jsonData;
    //            ori = ori.toLowerCase();
    //            if(ori.equals("landscape"))
    //               return Orientation.LANDSCAPE;
    //            else if(ori.equals("portrait"))
    //               return Orientation.PORTRAIT;
    //            else if(ori.equals("vertical"))
    //               return Orientation.VERTICAL;
    //            return Orientation.HORIZONTAL;
    //         }
    //         
    //      });

    /*   ------------------------------------------------   */

    json.setSerializer(Attributes.class, new ReadOnlySerializer<Attributes>() {
        @Override
        public Attributes read(Json json, Object jsonData, Class type) {
            float startX = json.readValue("startX", float.class, (float) 0, jsonData);
            float startY = json.readValue("startY", float.class, (float) 0, jsonData);
            float dstX = json.readValue("x", float.class, jsonData);
            float dstY = json.readValue("y", float.class, jsonData);
            float width = json.readValue("width", float.class, jsonData);
            float height = json.readValue("height", float.class, jsonData);
            Attributes attr = new Attributes();
            attr.startX = startX;
            attr.startY = startY;
            attr.x = dstX;
            attr.y = dstY;
            attr.width = width;
            attr.height = height;
            return attr;
        }
    });

    /*   ------------------------------------------------   */

    json.setSerializer(ScalingSet.class, new ReadOnlySerializer<ScalingSet>() {
        @Override
        public ScalingSet read(Json json, Object jsonData, Class type) {
            float layoutx = json.readValue("layoutx", float.class, jsonData);
            float layouty = json.readValue("layouty", float.class, jsonData);
            float layoutwidth = json.readValue("layoutwidth", float.class, jsonData);
            float layoutheight = json.readValue("layoutheight", float.class, jsonData);
            float whratio = json.readValue("whratio", float.class, jsonData);
            float hwratio = json.readValue("hwratio", float.class, jsonData);
            return new ScalingSet().set(layoutx, layouty, layoutwidth, layoutheight, whratio, hwratio);
        }
    });

    /*   ------------------------------------------------   */

    json.setSerializer(Vector2.class, new ReadOnlySerializer<Vector2>() {
        @Override
        public Vector2 read(Json json, Object jsonData, Class type) {
            return new Vector2(json.readValue("x", float.class, jsonData),
                    json.readValue("y", float.class, jsonData));
        }
    });
    return json;
}

From source file:org.matheusdev.util.Config.java

License:Open Source License

public void write() {
    FileWriter writer = null;//from   w  ww. j  a va  2 s .c  o  m
    try {
        Json json = new Json();
        json.setIgnoreUnknownFields(false);
        json.setUsePrototypes(false);
        json.setOutputType(OutputType.javascript);
        writer = new FileWriter(new File(configfile));
        writer.write(json.prettyPrint(toJsonDOM(), 100));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:stu.tnt.gdx.widget.StyleAtlas.java

License:Apache License

protected Json getJsonLoader(final FileHandle skinFile) {
    final Skin skin = this;

    final Json json = new Json() {
        public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) {
            // If the JSON is a string but the type is not, look up the
            // actual value by name.
            if (jsonData.isString() && !ClassReflection.isAssignableFrom(CharSequence.class, type))
                return get(jsonData.asString(), type);
            return super.readValue(type, elementType, jsonData);
        }/*www. j  a  v a 2  s  .com*/
    };
    json.setTypeName(null);
    json.setUsePrototypes(false);

    /* ------------------------------------------------ */

    json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {
        public Skin read(Json json, JsonValue typeToValueMap, Class ignored) {
            for (JsonValue valueMap = typeToValueMap.child(); valueMap != null; valueMap = valueMap.next()) {
                try {
                    readNamedObjects(json, ClassReflection.forName(valueMap.name()), valueMap);
                } catch (ReflectionException ex) {
                    throw new SerializationException(ex);
                }
            }
            return skin;
        }

        private void readNamedObjects(Json json, Class type, JsonValue valueMap) {
            Class addType = type == TintedDrawable.class ? Drawable.class : type;
            for (JsonValue valueEntry = valueMap.child(); valueEntry != null; valueEntry = valueEntry.next()) {
                Object object = json.readValue(type, valueEntry);
                if (object == null)
                    continue;
                try {
                    add(valueEntry.name(), object, addType);
                } catch (Exception ex) {
                    throw new SerializationException(
                            "Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name(),
                            ex);
                }
            }
        }
    });

    json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {
        public BitmapFont read(Json json, JsonValue jsonData, Class type) {
            String path = json.readValue("file", String.class, jsonData);

            FileHandle fontFile = skinFile.parent().child(path);
            if (!fontFile.exists())
                fontFile = Gdx.files.internal(path);
            if (!fontFile.exists())
                throw new SerializationException("Font file not found: " + fontFile);

            // Use a region with the same name as the font, else use
            // a PNG file in the same directory as the FNT file.
            String regionName = fontFile.nameWithoutExtension();
            try {
                TextureRegion region = skin.optional(regionName, TextureRegion.class);
                if (region != null)
                    return new BitmapFont(fontFile, region, false);
                else {
                    FileHandle imageFile = fontFile.parent().child(regionName + ".png");
                    if (imageFile.exists())
                        return new BitmapFont(fontFile, imageFile, false);
                    else
                        return new BitmapFont(fontFile, false);
                }
            } catch (RuntimeException ex) {
                throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
            }
        }
    });

    json.setSerializer(Color.class, new ReadOnlySerializer<Color>() {
        public Color read(Json json, JsonValue jsonData, Class type) {
            if (jsonData.isString())
                return get(jsonData.asString(), Color.class);
            String hex = json.readValue("hex", String.class, (String) null, jsonData);
            if (hex != null)
                return Color.valueOf(hex);
            float r = json.readValue("r", float.class, 0f, jsonData);
            float g = json.readValue("g", float.class, 0f, jsonData);
            float b = json.readValue("b", float.class, 0f, jsonData);
            float a = json.readValue("a", float.class, 1f, jsonData);
            return new Color(r, g, b, a);
        }
    });

    json.setSerializer(TintedDrawable.class, new ReadOnlySerializer() {
        public Object read(Json json, JsonValue jsonData, Class type) {
            String name = json.readValue("name", String.class, jsonData);
            Color color = json.readValue("color", Color.class, jsonData);
            return newDrawable(name, color);
        }
    });

    /* ------------------------------------------------ */

    json.setSerializer(Attributes.class, new ReadOnlySerializer<Attributes>() {
        @Override
        public Attributes read(Json json, JsonValue jsonData, Class type) {
            float startX = json.readValue("startX", float.class, (float) 0, jsonData);
            float startY = json.readValue("startY", float.class, (float) 0, jsonData);
            float dstX = json.readValue("x", float.class, jsonData);
            float dstY = json.readValue("y", float.class, jsonData);
            float width = json.readValue("width", float.class, jsonData);
            float height = json.readValue("height", float.class, jsonData);
            Attributes attr = new Attributes();
            attr.startX = startX;
            attr.startY = startY;
            attr.x = dstX;
            attr.y = dstY;
            attr.width = width;
            attr.height = height;
            return attr;
        }
    });

    /* ------------------------------------------------ */

    json.setSerializer(ScalingSet.class, new ReadOnlySerializer<ScalingSet>() {
        @Override
        public ScalingSet read(Json json, JsonValue jsonData, Class type) {
            float layoutx = json.readValue("layoutx", float.class, jsonData);
            float layouty = json.readValue("layouty", float.class, jsonData);
            float layoutwidth = json.readValue("layoutwidth", float.class, jsonData);
            float layoutheight = json.readValue("layoutheight", float.class, jsonData);
            float whratio = json.readValue("whratio", float.class, jsonData);
            float hwratio = json.readValue("hwratio", float.class, jsonData);
            return new ScalingSet().set(layoutx, layouty, layoutwidth, layoutheight, whratio, hwratio);
        }
    });

    /* ------------------------------------------------ */

    json.setSerializer(Vector2.class, new ReadOnlySerializer<Vector2>() {
        @Override
        public Vector2 read(Json json, JsonValue jsonData, Class type) {
            return new Vector2(json.readValue("x", float.class, jsonData),
                    json.readValue("y", float.class, jsonData));
        }
    });
    return json;
}

From source file:vault.clockwork.Config.java

License:Open Source License

/**
 * Load configuration file./*from  w ww  . ja va 2 s .  com*/
 * @param filename File to read.
 * @param recreate Recreate the configuration file on reading fail.
 * @return Newly loaded configuration.
 */
static public Config load(String filename, boolean recreate) {
    Json json = new Json(JsonWriter.OutputType.javascript);
    FileHandle file = Gdx.files.local(filename);

    json.setUsePrototypes(false);

    // load json configuration file
    Config cfg = file.exists() ? json.fromJson(Config.class, file) : null;

    if (cfg == null) {
        cfg = new Config();

        if (recreate) {
            System.err.println("Config file recreating...");
            Config.save(cfg, filename);
        }
    }

    return cfg;
}

From source file:vault.clockwork.Config.java

License:Open Source License

/**
 * Save the configuration to the file.//from   w  ww  .jav a 2s  . com
 * @param cfg
 * @param filename 
 */
static public void save(Config cfg, String filename) {
    Json json = new Json(JsonWriter.OutputType.javascript);
    FileHandle file = Gdx.files.local(filename);

    json.setUsePrototypes(false);

    // save json configuration file
    file.writeString(json.prettyPrint(cfg), false);
}

From source file:vault.clockwork.editor.PropHolder.java

License:Open Source License

/**
 * Returns the json parser instance.//from  w w  w.j  a v  a  2 s. c o  m
 * @return Newly instanced json coder/decoder.
 */
private static Json getJson() {
    Json json = new Json(JsonWriter.OutputType.javascript);
    json.setUsePrototypes(false);
    json.setIgnoreUnknownFields(true);
    return json;
}