Xstream - 字符串数组

发布于 2024-11-05 17:32:42 字数 1212 浏览 5 评论 0原文

首先,我不是在寻求解决方案,而是寻求一种思考方式。

我得到了一些需要序列化以便稍后查看的数据...我知道该怎么做..但是字段名称是问题...

数据结构包含:(

Name of the Field
CoordX
CoordY
Value

大约有.. 20 个不同的字段。我需要检查 biiiggggg 字符串日志...)

我可以使用单个 String[][] 或多个 String[] ...正如我所说..问题是它在 XML 上的显示方式...

如果我执行单个数组列表多维 -> [][] 我得到了这个

<teste>
      <string-array>
        <string>fieldName</string>
        <string>x</string>
        <string>y</string>
        <string>value</string>
      </string-array>
      <string-array>
        <string>fieldName</string>
        <string>x</string>
        <string>y</string>
        <string>value</string>
      </string-array>
</teste>

如果我执行单个字符串[],我可以将字符串的名称作为字段名称

<Fieldname>      
    <string>X</string>
    <string>y</string>
    <string>Value</string>
</Fieldname>

我看到正常的别名适用于所有字段(@XStreamImplicit(itemFieldName =“part”))并且不能解决我的问题..

如果在另一边进行反序列化时,按行检查日志而不按字段检查日志(我知道第1行是字段名称,第2行是x..等),那么它可能毫无价值。

所以..你们觉得怎么样?

First things first, I'm no asking for a solution but a way to think.

I got some data that I need to serialize to check out later... I know how to do it.. but the fields names are the problem...

The data structure contains:

Name of the Field
CoordX
CoordY
Value

(There are like.. 20 different fields. I need to check a biiiggggg string log...)

I could use a single String[][] or several Strings[] ... as I said.. the problem is how it appears on the XML...

If I do a single arraylist multidimensional -> [][] I got this

<teste>
      <string-array>
        <string>fieldName</string>
        <string>x</string>
        <string>y</string>
        <string>value</string>
      </string-array>
      <string-array>
        <string>fieldName</string>
        <string>x</string>
        <string>y</string>
        <string>value</string>
      </string-array>
</teste>

And if I do a single string[] I can put the name of the String as the field name

<Fieldname>      
    <string>X</string>
    <string>y</string>
    <string>Value</string>
</Fieldname>

I saw that normal alias is for ALL fields (@XStreamImplicit(itemFieldName="part")) and that don't solve my problem..

It could be worthless if on the other side when I do deserialization, check the log by line and no by field (I know line 1 is field name, line 2 is x..etc)..

So.. what do you guys think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2024-11-12 17:32:42

我通常根据“字段”的内容设计序列化。假设您的名称和值字段相对较小,您当然可以将它们序列化

<list>
  <field name="foo" cordX="12324" cordY="1324" value="value of field foo" />
  <field name="foo" cordX="12324" cordY="1324" value="value of field foo" />
</list>

,如果值很长,那么您不想将其序列化为属性,而是作为常规字段值,如下所示:

<list>
  <field name="foo" cordX="12324" cordY="1324">The long value </field>
  <field name="foo" cordX="12324" cordY="1324">Even longer field value...</field>
</list>

您可以使用XStream 像这样:

public class Run {
    public static void main(String[] args) {
        XStream xs = new XStream(new DomDriver());
        xs.processAnnotations(new Class[] { Field.class, Container.class });
        Container c = new Container();
        c.addField("boo", 1,2, "desc");
        c.addField("boo", 1,2, "desc");
        String serialized = xs.toXML(c);
        System.out.println(serialized);

        // deserialize
        Container newContainer = (Container) xs.fromXML(serialized);
        if (newContainer.fields.size() != 2) {
            System.out.println("Not deserialized as expected...");
        }

        // if you don't want "container"
        xs.alias("mylist", List.class);
        System.out.println(xs.toXML(c.fields));
    }
}

您在其中定义容器和字段,如下所示:

@XStreamAlias("mylistofitems")
public class Container {
    public List<Field> fields;

    public void addField(String name, int x, int y, String desc) {
        if (fields == null) fields = new ArrayList<Field>();
        fields.add(new Field(name, x,y, desc));

    }
}

@XStreamAlias("field")
public class Field {
    public Field() {}
    public Field(String name, int x, int y, String desc) {
        this.name = name;
        cordX = x;
        cordY = y;
        value = desc;
    }
    @XStreamAsAttribute
    private String name;
    @XStreamAsAttribute
    private int cordX;
    @XStreamAsAttribute
    private int cordY;
    @XStreamAsAttribute
    private String value;
}

程序给出以下输出:

<mylistofitems>
  <fields>
    <field name="boo" cordX="1" cordY="2" value="desc"/>
    <field name="boo" cordX="1" cordY="2" value="desc"/>
  </fields>
</mylistofitems>
<mylist>
  <field name="boo" cordX="1" cordY="2" value="desc"/>
  <field name="boo" cordX="1" cordY="2" value="desc"/>
</mylist>

希望这会有所帮助。

I usually design serialization depending on the contents of my "fields". Say that your name and value fields are relatively small, you could serialize them as

<list>
  <field name="foo" cordX="12324" cordY="1324" value="value of field foo" />
  <field name="foo" cordX="12324" cordY="1324" value="value of field foo" />
</list>

of course, if value is long, then you don't want to serialize it as attribute, but as a regular field value like so:

<list>
  <field name="foo" cordX="12324" cordY="1324">The long value </field>
  <field name="foo" cordX="12324" cordY="1324">Even longer field value...</field>
</list>

You can achieve this using XStream like so:

public class Run {
    public static void main(String[] args) {
        XStream xs = new XStream(new DomDriver());
        xs.processAnnotations(new Class[] { Field.class, Container.class });
        Container c = new Container();
        c.addField("boo", 1,2, "desc");
        c.addField("boo", 1,2, "desc");
        String serialized = xs.toXML(c);
        System.out.println(serialized);

        // deserialize
        Container newContainer = (Container) xs.fromXML(serialized);
        if (newContainer.fields.size() != 2) {
            System.out.println("Not deserialized as expected...");
        }

        // if you don't want "container"
        xs.alias("mylist", List.class);
        System.out.println(xs.toXML(c.fields));
    }
}

where you define your Container and Field like so:

@XStreamAlias("mylistofitems")
public class Container {
    public List<Field> fields;

    public void addField(String name, int x, int y, String desc) {
        if (fields == null) fields = new ArrayList<Field>();
        fields.add(new Field(name, x,y, desc));

    }
}

@XStreamAlias("field")
public class Field {
    public Field() {}
    public Field(String name, int x, int y, String desc) {
        this.name = name;
        cordX = x;
        cordY = y;
        value = desc;
    }
    @XStreamAsAttribute
    private String name;
    @XStreamAsAttribute
    private int cordX;
    @XStreamAsAttribute
    private int cordY;
    @XStreamAsAttribute
    private String value;
}

The program gives this output:

<mylistofitems>
  <fields>
    <field name="boo" cordX="1" cordY="2" value="desc"/>
    <field name="boo" cordX="1" cordY="2" value="desc"/>
  </fields>
</mylistofitems>
<mylist>
  <field name="boo" cordX="1" cordY="2" value="desc"/>
  <field name="boo" cordX="1" cordY="2" value="desc"/>
</mylist>

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文