javafx用颜色序列化包装器

问题描述 投票:-1回答:1

编辑:我正在尝试使用这些数据字段序列化一个类:

    private int radius;
    private int strokeWidth;
    private String colorName;
    private CannonBallDrawModeEnum cannonBallDrawModeEnum;

[有些方法可以将colorName转换为Color。在我进行序列化之前,我调用了一种方法来将colorName转换回String,以确保没有问题。序列化后,我在文本文件中注意到了标记<color/>。删除标签后,可以毫无问题地反序列化该类。如何停止将此标签序列化?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<serializeWrapper>
    <cannonBallData>
        <cannonBallDrawModeEnum>CIRCLE</cannonBallDrawModeEnum>
        </color>                     //<--------------This line does not belong.
        <colorName>RED</colorName>
        <radius>40</radius>
        <strokeWidth>1</strokeWidth>
    </cannonBallData>
    <pyramidData>
        <height>4</height>
        <name>MyPyramid_1</name>
        <stackingOrder>LeftJustified</stackingOrder>
    </pyramidData>
</serializeWrapper>
javafx serialization colors deserialization wrapper
1个回答
0
投票

由于使用的是JAXB,因此应使用XmlAdapterColor映射到例如String,反之亦然。这是一个例子:

Main.java:

import java.io.File;
import javafx.scene.paint.Color;
import javax.xml.bind.JAXB;

public class Main {

  public static void main(String[] args) {
    File file = new File("entity.xml").getAbsoluteFile();
    System.out.println("XML FILE       : " + file);

    Entity entity = new Entity();
    entity.setRadius(24);
    entity.setStrokeWidth(2);
    entity.setColor(Color.RED);

    System.out.println("BEFORE MARSHAL : " + entity);
    JAXB.marshal(entity, file);
    System.out.println("AFTER UNMARSHAL: " + JAXB.unmarshal(file, Entity.class));
  }
}

Entity.java:

import javafx.scene.paint.Color;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Entity {

  private Color color;
  private int radius;
  private int strokeWidth;

  @XmlElement
  @XmlJavaTypeAdapter(ColorXmlAdapter.class) // apply adapter
  public Color getColor() {
    return color;
  }

  public void setColor(Color color) {
    this.color = color;
  }

  @XmlElement
  public int getRadius() {
    return radius;
  }

  public void setRadius(int radius) {
    this.radius = radius;
  }

  @XmlElement
  public int getStrokeWidth() {
    return strokeWidth;
  }

  public void setStrokeWidth(int strokeWidth) {
    this.strokeWidth = strokeWidth;
  }

  @Override
  public String toString() {
    return "Entity{radius=" + radius + ", strokeWidth=" + strokeWidth + ", color=" + color + "}";
  }
}

ColorXmlAdapter.java:

import javafx.scene.paint.Color;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class ColorXmlAdapter extends XmlAdapter<String, Color> {

  @Override
  public Color unmarshal(String v) throws Exception {
    return Color.valueOf(v); // capable of parsing hex color strings
  }

  @Override
  public String marshal(Color v) throws Exception {
    int red = toInt(v.getRed()) << 24;
    int green = toInt(v.getGreen()) << 16;
    int blue = toInt(v.getBlue()) << 8;
    int alpha = toInt(v.getOpacity());

    // Convert to hex color string
    int rgba = red + green + blue + alpha;
    return String.format("#%08X", rgba);
  }

  private static int toInt(double rgbaComponent) {
    return (int) Math.round(255 * rgbaComponent);
  }
}

上面将输出以下XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entity>
    <color>#FF0000FF</color>
    <radius>24</radius>
    <strokeWidth>2</strokeWidth>
</entity>
© www.soinside.com 2019 - 2024. All rights reserved.