Aliasing/CustomConverter to add a field as attribute to another field

12 views
Skip to first unread message

René Brandenburger

unread,
Aug 15, 2023, 8:52:51 AM8/15/23
to XStream User
we have been using xstream happily for years, but now we reached a point, where we need to cut down on xml message sizes generated by xstream 1.4.20.
the actual xml generated looks something like this, with about 80 to 100 triplets (generated code)
<Person>
  <firstName>Jonathan</firstName>
  <firstNameHasBeenSet>true</firstNameHasBeenSet>
  <firstNameHasBeenChanged>false</firstNameHasBeenChanged>
  <lastName>Arbuckle</lastName>
  <lastNameHasBeenSet>true</lastNameHasBeenSet>
  <lastNameHasBeenChanged>false</lastNameHasBeenChanged>
  ...
  <fieldname>...</fieldname>
  <fieldnameHasBeenSet>...</fieldnameHasBeenSet>
  <fieldnameHasBeenChanged>...</fieldnameHasBeenChanged>
   ...
</Person>
what we intend to get is
<Person>
  <firstName set="true" changed="false">Jonathan</firstName>
  <lastName set="true" changed="false">Arbuckle</lastName>
  ...
  <fieldname set="..." changed="...">...</fieldname>
   ...
</Person>
i tried with aliasing and attribute aliasing, but i was only able to add the attributes to the Person tag.
Is there a way to achieve this by configuration or extending the existing reflection converters?
Regards
René

Jörg Schaible

unread,
Aug 15, 2023, 4:40:24 PM8/15/23
to XStream User
Hi René
This is not possible for such a data structure as you might have assumed. You
have two options:

1/ Write a custom converter for Person that does what you want. See the
Converter tutorial, it is not too hard.

2/ Restructure your class to something like (assuming you don not access the
fields directly in your code):

class Person {
private StatefulField firstName;
private StatefulField lastName;
...
// getter / setters
}

or with a Map:

class Person {
private EnumMap<FieldName, StatefulField> fields;
// getter / setters
}

In both cases you can configure XStream to process the structure you're looking
for.

Regards,
Jörg


René Brandenburger

unread,
Aug 21, 2023, 7:34:22 AM8/21/23
to XStream User
Hi Jörg,
thanks for the quick reply.
as 2/ is not an option (generated code) i will have a look into custom converters.


Regards
René

Jörg Schaible

unread,
Aug 21, 2023, 11:49:53 AM8/21/23
to XStream User
On Monday, 21. August 2023, 13:34:22 CEST René Brandenburger wrote:
> Hi Jörg,
> thanks for the quick reply.
> as 2/ is not an option (generated code) i will have a look into custom
> converters.

Well, actually you can use reflection in your custom converter as well to
minimize the code to a great extend... ;-)

Regards,
Jörg


Reply all
Reply to author
Forward
0 new messages