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

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

Introduction

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

Prototype

public void setTypeName(String typeName) 

Source Link

Document

Sets the name of the JSON field to store the Java class name or class tag when required to avoid ambiguity during deserialization.

Usage

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  .j a  v a2 s. co 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: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);
        }//  w ww  .jav  a2 s  .  c o m
    };
    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;
}