Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3356580
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:31:48+00:00 2026-05-18T02:31:48+00:00

I have a legacy class, with a lot of public double fields. All double

  • 0

I have a legacy class, with a lot of public double fields. All double fields are initialized with Double.MAX_VALUE to indicate that they are empty. (The legacy serialization is coded to ignore the field and not serialize if field is equals to Double.MAX_VALUE).

We are now trying to serialize this class to Xml using JAXB Marshaller. It is working fine, except that we want to prevent generating Xml for fields which equal Double.MAX_VALUE.

We aren’t using a separate JAXB schema, just marking up our classes with various javax.xml.bind.annotation Annotations. If a schema is used, you can add a <javaType> element to specify a custom DataType converter. Is there any way to do this using Annotations or programmatically?

After trying approach recommended below, I still can’t get XmlAdapter picked up:

@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=EmptyDoubleValueHandler.class, type=Double.class), @XmlJavaTypeAdapter(value=EmptyDoubleValueHandler.class, type=double.class)})
package tta.penstock.data.iserver;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

My top level class is: tta.penstock.data.iserver.OrderBlotter, which contains a list of tta.penstock.data.iserver.OrderResponseWrappers which extends com.eztech.OrderResponse. All the double fields are contained in com.eztech.OrderResponse.

My unit test code does the following:

JAXBContext context = JAXBContext.newInstance(new Class[] { OrderBlotter.class, OrderResponseWrapper.class, OrderResponse.class});

Marshaller marshaller = context.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(blotter, stringWriter);
System.out.println("result xml=\n" + stringWriter.toString());

But the double values still don’t get handled by the XmlAdapter. I know I’m missing something basic, but I’m not sure what it is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:31:48+00:00Added an answer on May 18, 2026 at 2:31 am

    You could use an XmlAdapter:

    • http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html

    The XmlAdapter

    package example;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class DoubleAdapter extends XmlAdapter<Double, Double>{
    
        @Override
        public Double unmarshal(Double v) throws Exception {
            return v;
        }
    
        @Override
        public Double marshal(Double v) throws Exception {
           if(Double.MAX_VALUE == v) {
               return null;
           } else {
               return v;
           }
        }
    
    }
    

    The Model Object

    package example;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Root {
    
        @XmlJavaTypeAdapter(DoubleAdapter.class)
        public Double maxDouble = Double.MAX_VALUE;
    
        @XmlJavaTypeAdapter(DoubleAdapter.class)
        public Double aDouble = 123d;
    
    }
    

    Demo Code

    package example;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(new Root(), System.out);
        }
    
    }
    

    UPDATE

    StaxMan’s suggestion is a good one. If you specify the following package level annotation you can avoid the need of individually annotating all the Double properties

    package-info.java

    @XmlJavaTypeAdapters({
        @XmlJavaTypeAdapter(type=Double.class, value=DoubleAdapter.class)
    })
    package example;
    
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy class that I quickly want to write a couple of
I have a legacy class that is rather complex to maintain: class OldClass {
We have a legacy 3rd party program that is failing with the error Class
I have legacy class that writes some results to a file using StreamWriter ,
We have a legacy class that uses an indexer to allow people to add
I have a lot of legacy code that uses a function pointer as an
I have a legacy class that the class itself is not a generic but
I have legacy DB that store dates that means no-date as 9999-21-31, The column
I have a legacy stored procedure that I'm trying to modify. This is a
I have a legacy intranet web app that was written for IE7 and contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.