Package com.badlogic.gdx.utils

Examples of com.badlogic.gdx.utils.Json$Serializer


  }

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

    Json json = new Json();
    json.setTypeName(null);
    json.setUsePrototypes(false);

    class AliasSerializer implements Serializer {
      final ObjectMap<String, ?> map;

      public AliasSerializer (ObjectMap<String, ?> map) {
        this.map = map;
      }

      public void write (Json json, Object object, Class valueType) {
        for (Entry<String, ?> entry : map.entries()) {
          if (entry.value.equals(object)) {
            json.writeValue(entry.key);
            return;
          }
        }
        throw new SerializationException(object.getClass().getSimpleName() + " not found: " + object);
      }

      public Object read (Json json, Object jsonData, Class type) {
        String name = (String)jsonData;
        Object object = map.get(name);
        if (object == null) {
          ObjectMap<String, Object> regions = data.resources.get(TextureRegion.class);
          if (regions != null) {
            object = regions.get(name);
            if (object != null) object = new NinePatch((TextureRegion)object)
          }
          if (object == null)
            throw new SerializationException("Skin has a " + type.getSimpleName()
              + " that could not be found in the resources: " + jsonData);
        }
        return object;
      }
    }

    json.setSerializer(Skin.class, new Serializer<Skin>() {
      public void write (Json json, Skin skin, Class valueType) {
        json.writeObjectStart();
        json.writeValue("resources", skin.data.resources);
        for (Entry<Class, ObjectMap<String, Object>> entry : data.resources.entries())
          json.setSerializer(entry.key, new AliasSerializer(entry.value));
        json.writeField(skin, "styles");
        json.writeObjectEnd();
      }

      public Skin read (Json json, Object jsonData, Class ignored) {
        ObjectMap map = (ObjectMap)jsonData;
        readTypeMap(json, (ObjectMap)map.get("resources"), true);
        for (Entry<Class, ObjectMap<String, Object>> entry : data.resources.entries())
          json.setSerializer(entry.key, new AliasSerializer(entry.value));
        readTypeMap(json, (ObjectMap)map.get("styles"), false);
        return skin;
      }

      private void readTypeMap (Json json, ObjectMap<String, ObjectMap> typeToValueMap, boolean isResource) {
        if (typeToValueMap == null)
          throw new SerializationException("Skin file is missing a \"" + (isResource ? "resources" : "styles")
            + "\" section.");
        for (Entry<String, ObjectMap> typeEntry : typeToValueMap.entries()) {
          Class type;
          try {
            type = Class.forName(typeEntry.key);
          } catch (ClassNotFoundException ex) {
            throw new SerializationException(ex);
          }
          ObjectMap<String, ObjectMap> valueMap = (ObjectMap)typeEntry.value;
          for (Entry<String, ObjectMap> valueEntry : valueMap.entries()) {
            try {
              if (isResource)
                addResource(valueEntry.key, json.readValue(type, valueEntry.value));
              else
                addStyle(valueEntry.key, json.readValue(type, valueEntry.value));
            } catch (Exception ex) {
              throw new SerializationException("Error reading " + type.getSimpleName() + ": " + valueEntry.key, ex);
            }
          }
        }
      }
    });

    json.setSerializer(TextureRegion.class, new Serializer<TextureRegion>() {
      public void write (Json json, TextureRegion region, Class valueType) {
        json.writeObjectStart();
        json.writeValue("x", region.getRegionX());
        json.writeValue("y", region.getRegionY());
        json.writeValue("width", region.getRegionWidth());
        json.writeValue("height", region.getRegionHeight());
        json.writeObjectEnd();
      }

      public TextureRegion read (Json json, Object jsonData, Class type) {
        int x = json.readValue("x", int.class, jsonData);
        int y = json.readValue("y", int.class, jsonData);
        int width = json.readValue("width", int.class, jsonData);
        int height = json.readValue("height", int.class, jsonData);
        return new TextureRegion(skin.data.texture, x, y, width, height);
      }
    });

    json.setSerializer(BitmapFont.class, new Serializer<BitmapFont>() {
      public void write (Json json, BitmapFont font, Class valueType) {
        json.writeValue(font.getData().getFontFile().toString().replace('\\', '/'));
      }

      public BitmapFont read (Json json, Object jsonData, Class type) {
        String path = json.readValue(String.class, jsonData);
        FileHandle file = skinFile.parent().child(path);
        if (!file.exists()) file = Gdx.files.internal(path);
        return new BitmapFont(file, false);
      }
    });

    json.setSerializer(NinePatch.class, new Serializer<NinePatch>() {
      public void write (Json json, NinePatch ninePatch, Class valueType) {
        json.writeValue(ninePatch.getPatches());
      }

      public NinePatch read (Json json, Object jsonData, Class type) {
        return new NinePatch(json.readValue(TextureRegion[].class, jsonData));
      }
    });

    return json;
  }
View Full Code Here


     *
     * @param fileHandle
     *         The JSON file.
     */
    public void load(FileHandle fileHandle) {
        Json json = new Json();
        ObjectMap<String, JsonValue> obj = json.fromJson(ObjectMap.class, fileHandle);
        if(obj != null) {
            for(ObjectMap.Entry<String, JsonValue> entry : obj.entries()) {
                if(entry.value == null) continue;
                EntityTemplate entityTemplate = new EntityTemplate(componentNamespace);
                entityTemplate.read(json, entry.value);
View Full Code Here

     *
     * @param manifestFileHandle
     *         The manifest file handle.
     */
    public void loadManifest(FileHandle manifestFileHandle) {
        Json json = new Json();
        ObjectMap<String, JsonValue> assetGroups = json.fromJson(ObjectMap.class, manifestFileHandle);
        for(ObjectMap.Entry<String, JsonValue> entry : assetGroups.entries()) {
            AssetGroup assetGroup = new AssetGroup();
            assetGroup.read(json, entry.value);
            assetGroupMap.put(entry.key, assetGroup);
        }
View Full Code Here

  }

  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);
      }
    };
    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);
      }
    });

    return json;
View Full Code Here

  @Override
  public void loadAsync (AssetManager manager, String fileName, FileHandle file, ParticleEffectLoadParameter parameter) {}

  @Override
  public Array<AssetDescriptor> getDependencies (String fileName, FileHandle file, ParticleEffectLoadParameter parameter) {
    Json json = new Json();
    ResourceData<ParticleEffect> data = json.fromJson(ResourceData.class, file);
    Array<AssetData> assets = null;
    synchronized (items) {
      ObjectMap.Entry<String, ResourceData<ParticleEffect>> entry = new ObjectMap.Entry<String, ResourceData<ParticleEffect>>();
      entry.key = fileName;
      entry.value = data;
View Full Code Here

          batch.save(parameter.manager, data);
      }
    }
   
    //save
    Json json = new Json();
    json.toJson(data, parameter.file);
  }
View Full Code Here

  public Array<NetworkShip> enemies = new Array<NetworkShip>();
 
  static Network instance;
 
  private Network() {
    json = new Json();
    connectToServer();
  }
View Full Code Here

  }

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

    final Json json = new Json();
    json.setTypeName(null);
    json.setUsePrototypes(false);

    // Writes names of resources instead of objects.
    class AliasWriter implements Serializer {
      final ObjectMap<String, ?> map;

      public AliasWriter (Class type) {
        map = resources.get(type);
      }

      public void write (Json json, Object object, Class valueType) {
        for (Entry<String, ?> entry : map.entries()) {
          if (entry.value.equals(object)) {
            json.writeValue(entry.key);
            return;
          }
        }
        throw new SerializationException(object.getClass().getSimpleName() + " not found: " + object);
      }

      public Object read (Json json, Object jsonData, Class type) {
        throw new UnsupportedOperationException();
      }
    }

    json.setSerializer(Skin.class, new Serializer<Skin>() {
      public void write (Json json, Skin skin, Class valueType) {
        json.writeObjectStart();
        json.writeValue("resources", skin.resources);
        for (Entry<Class, ObjectMap<String, Object>> entry : resources.entries())
          json.setSerializer(entry.key, new AliasWriter(entry.key));
        json.writeField(skin, "styles");
        json.writeObjectEnd();
      }

      public Skin read (Json json, Object jsonData, Class ignored) {
        ObjectMap map = (ObjectMap)jsonData;
        readTypeMap(json, (ObjectMap)map.get("resources"), true);
        readTypeMap(json, (ObjectMap)map.get("styles"), false);
        return skin;
      }

      private void readTypeMap (Json json, ObjectMap<String, ObjectMap> typeToValueMap, boolean isResource) {
        if (typeToValueMap == null)
          throw new SerializationException("Skin file is missing a \"" + (isResource ? "resources" : "styles")
            + "\" section.");
        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, isResource);
          } catch (ClassNotFoundException ex) {
            throw new SerializationException(ex);
          }
        }
      }

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

    json.setSerializer(TextureRegion.class, new Serializer<TextureRegion>() {
      public void write (Json json, TextureRegion region, Class valueType) {
        json.writeObjectStart();
        json.writeValue("x", region.getRegionX());
        json.writeValue("y", region.getRegionY());
        json.writeValue("width", region.getRegionWidth());
        json.writeValue("height", region.getRegionHeight());
        json.writeObjectEnd();
      }

      public TextureRegion read (Json json, Object jsonData, Class type) {
        if (jsonData instanceof String) return getResource((String)jsonData, TextureRegion.class);
        int x = json.readValue("x", int.class, jsonData);
        int y = json.readValue("y", int.class, jsonData);
        int width = json.readValue("width", int.class, jsonData);
        int height = json.readValue("height", int.class, jsonData);
        return new TextureRegion(skin.texture, x, y, width, height);
      }
    });

    json.setSerializer(BitmapFont.class, new Serializer<BitmapFont>() {
      public void write (Json json, BitmapFont font, Class valueType) {
        json.writeObjectStart();
        json.writeValue("file", font.getData().getFontFile().toString().replace('\\', '/'));
        json.writeObjectEnd();
      }

      public BitmapFont read (Json json, Object jsonData, Class type) {
        if (jsonData instanceof String) return getResource((String)jsonData, BitmapFont.class);
        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 {
          if (skin.hasResource(regionName, TextureRegion.class))
            return new BitmapFont(fontFile, skin.getResource(regionName, TextureRegion.class), 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(NinePatch.class, new Serializer<NinePatch>() {
      public void write (Json json, NinePatch ninePatch, Class valueType) {
        TextureRegion[] patches = ninePatch.getPatches();
        boolean singlePatch = patches[0] == null && patches[1] == null && patches[2] == null && patches[3] == null
          && patches[4] != null && patches[5] == null && patches[6] == null && patches[7] == null && patches[8] == null;
        if (ninePatch.getColor() != null) {
          json.writeObjectStart();
          json.writeValue("color", ninePatch.getColor());
          if (singlePatch)
            json.writeValue("region", patches[4]);
          else
            json.writeValue("regions", patches);
          json.writeObjectEnd();
        } else {
          if (singlePatch)
            json.writeValue(patches[4]);
          else
            json.writeValue(patches);
        }
      }

      public NinePatch read (Json json, Object jsonData, Class type) {
        if (jsonData instanceof String) return getResource((String)jsonData, NinePatch.class);
        if (jsonData instanceof Array) {
          TextureRegion[] regions = json.readValue(TextureRegion[].class, jsonData);
          if (regions.length == 1) return new NinePatch(regions[0]);
          return new NinePatch(regions);
        } else {
          ObjectMap map = (ObjectMap)jsonData;
          NinePatch ninePatch;
          if (map.containsKey("regions"))
            ninePatch = new NinePatch(json.readValue("regions", TextureRegion[].class, jsonData));
          else if (map.containsKey("region"))
            ninePatch = new NinePatch(json.readValue("region", TextureRegion.class, jsonData));
          else
            ninePatch = new NinePatch(json.readValue(TextureRegion.class, jsonData));
          // throw new SerializationException("Missing ninepatch regions: " + map);
          if (map.containsKey("color")) ninePatch.setColor(json.readValue("color", Color.class, jsonData));
          return ninePatch;
        }
      }
    });

    json.setSerializer(Color.class, new Serializer<Color>() {
      public void write (Json json, Color color, Class valueType) {
        json.writeObjectStart();
        json.writeFields(color);
        json.writeObjectEnd();
      }

      public Color read (Json json, Object jsonData, Class type) {
        if (jsonData instanceof String) return getResource((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(TintedNinePatch.class, new Serializer() {
      public void write (Json json, Object tintedPatch, Class valueType) {
        json.writeObjectStart();
        json.writeField(tintedPatch, "name");
        json.writeField(tintedPatch, "color");
        json.writeObjectEnd();
      }

      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 new NinePatch(getResource(name, NinePatch.class), color);
      }
    });

    return json;
View Full Code Here

    HiveOutputFormat<?, ?> hiveOutputFormat = null;
    Class<? extends Writable> outputClass = null;
    boolean isCompressed = conf.getCompressed();
    TableDesc tableInfo = conf.getTableInfo();
    try {
      Serializer serializer = (Serializer) tableInfo.getDeserializerClass().newInstance();
      serializer.initialize(null, tableInfo.getProperties());
      outputClass = serializer.getSerializedClass();
      hiveOutputFormat = conf.getTableInfo().getOutputFileFormatClass().newInstance();
    } catch (SerDeException e) {
      throw new HiveException(e);
    } catch (InstantiationException e) {
      throw new HiveException(e);
View Full Code Here

    HiveOutputFormat<?, ?> hiveOutputFormat = null;
    Class<? extends Writable> outputClass = null;
    boolean isCompressed = conf.getCompressed();
    TableDesc tableInfo = conf.getTableInfo();
    try {
      Serializer serializer = (Serializer) tableInfo.getDeserializerClass().newInstance();
      serializer.initialize(null, tableInfo.getProperties());
      outputClass = serializer.getSerializedClass();
      hiveOutputFormat = conf.getTableInfo().getOutputFileFormatClass().newInstance();
    } catch (SerDeException e) {
      throw new HiveException(e);
    } catch (InstantiationException e) {
      throw new HiveException(e);
View Full Code Here

TOP

Related Classes of com.badlogic.gdx.utils.Json$Serializer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.