Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 341354

Summary: Encoding property on marshaller not used
Product: z_Archived Reporter: Kasper Thuno <kat>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: eclipselink.oxm-inbox, kat
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Kasper Thuno CLA 2011-03-30 09:30:16 EDT
Build Identifier: 2.2.0

A Marshaller is instantiated like this:

m = getContext().createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(Marshaller.JAXB_ENCODING, GlobalParams.DEFAULT_ENCODING);

where GlobalParams.DEFAULT_ENCODING is "UTF-8".

When altering objects and using the marshall process, the content of the XML file is garbled.

Debugging the issue leads me to this function in the JAXBMarshaller class:

    public void marshal(Object object, File file) throws JAXBException {
        try {
            java.io.FileWriter writer = new java.io.FileWriter(file);
            try {
                marshal(object, writer);
            } finally {
                writer.close();
            }
        } catch(Exception ex) {
            throw new MarshalException(ex);
        }
    }

When inspecting the FileWriter instance I can see that the encoding is set to MS1252 (danish charset) which is an ISO8859 equivalent and is the system default encoding.

I have set the System property file.encoding to "UTF-8".

Please advice!

Reproducible: Always

Steps to Reproduce:
1.Choose a different system charset than the one used in the XML files
2.Construct a marshaller
3.Marshal
Comment 1 Kasper Thuno CLA 2011-03-30 10:04:14 EDT
Using this works:

OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(out), GlobalParams.DEFAULT_ENCODING);
		
getMarshaller().marshal(o, writer);
Comment 2 Blaise Doughan CLA 2011-03-30 10:15:15 EDT
The behaviour observed by Kasper is the expected behaviour.  When marshalling to an instance of Writer the encoding is set on the Writer and cannot be changed by JAXB:

import java.io.OutputStreamWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class MarshalDemo {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("Jane Doe");

        Address address = new Address();
        address.setStreet("123 A Street");
        address.setCity("Any Town");

        JAXBContext jc = JAXBContext.newInstance(Customer.class);
        
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-16");

        marshaller.marshal(customer, System.out); // Will marshal to UTF-16

        OutputStreamWriter osw = new OutputStreamWriter(System.out);
        System.out.println(osw.getEncoding());
        marshaller.marshal(customer, osw);  // For me will marshal to Cp1252

        marshaller.marshal(customer, new OutputStreamWriter(System.out, "UTF-16")); // Will marshal to UTF-16 
    }

}
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:20:48 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink