Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 411377 | Differences between
and this patch

Collapse All | Expand All

(-)a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonObjectBuilderWriterRecord.java (-2 / +10 lines)
Lines 71-76 public class JsonObjectBuilderWriterRecord extends MarshalRecord <XMLMarshaller> Link Here
71
    public JsonObjectBuilderWriterRecord(JsonArrayBuilder jsonArrayBuilder){
71
    public JsonObjectBuilderWriterRecord(JsonArrayBuilder jsonArrayBuilder){
72
        this();
72
        this();
73
        rootJsonArrayBuilder = jsonArrayBuilder;
73
        rootJsonArrayBuilder = jsonArrayBuilder;
74
        isRootArray = true;
74
    }
75
    }
75
    
76
    
76
    /**
77
    /**
Lines 89-94 public class JsonObjectBuilderWriterRecord extends MarshalRecord <XMLMarshaller> Link Here
89
    @Override
90
    @Override
90
    public void startDocument(String encoding, String version) {      
91
    public void startDocument(String encoding, String version) {      
91
        if(isRootArray){
92
        if(isRootArray){
93
            if(position == null){
94
                startCollection();
95
            }
92
            position.setEmptyCollection(false);
96
            position.setEmptyCollection(false);
93
            
97
            
94
            Level newLevel = new Level(false, position);
98
            Level newLevel = new Level(false, position);
Lines 114-120 public class JsonObjectBuilderWriterRecord extends MarshalRecord <XMLMarshaller> Link Here
114
            }else{
118
            }else{
115
                //this is the root level list case
119
                //this is the root level list case
116
                position = position.parentLevel;
120
                position = position.parentLevel;
117
            }            
121
            }
122
            if(position !=null && isRootArray){
123
                endCollection();
124
            }
118
        }
125
        }
119
    }
126
    }
120
    
127
    
Lines 802-808 public class JsonObjectBuilderWriterRecord extends MarshalRecord <XMLMarshaller> Link Here
802
            if(isComplex && jsonObjectBuilder == null){
809
            if(isComplex && jsonObjectBuilder == null){
803
                jsonObjectBuilder = Json.createObjectBuilder();
810
                jsonObjectBuilder = Json.createObjectBuilder();
804
            }
811
            }
805
        }        
812
        }
813
806
    }
814
    }
807
815
808
}
816
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/rootlevellist/JsonObjectInArrayBuilderTestCases.java (+101 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
3
 * This program and the accompanying materials are made available under the
4
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
 * which accompanies this distribution.
6
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
 * and the Eclipse Distribution License is available at
8
 * http://www.eclipse.org/org/documents/edl-v10.php.
9
 *
10
 * Contributors:
11
 *     Denise Smith - 2.6 - initial implementation
12
 ******************************************************************************/
13
package org.eclipse.persistence.testing.jaxb.json.rootlevellist;
14
15
import java.io.StringWriter;
16
17
import javax.json.Json;
18
import javax.json.JsonArray;
19
import javax.json.JsonArrayBuilder;
20
import javax.json.JsonWriter;
21
import javax.xml.bind.JAXBContext;
22
import javax.xml.bind.Marshaller;
23
24
import org.eclipse.persistence.jaxb.JAXBContextFactory;
25
import org.eclipse.persistence.jaxb.JAXBMarshaller;
26
import org.eclipse.persistence.jaxb.MarshallerProperties;
27
import org.eclipse.persistence.oxm.MediaType;
28
import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult;
29
import org.eclipse.persistence.testing.oxm.OXTestCase;
30
31
public class JsonObjectInArrayBuilderTestCases extends OXTestCase {
32
    
33
    private static final String CONTROL_JSON = "org/eclipse/persistence/testing/jaxb/json/rootlevellist/WithoutXmlRootElementSingle.json";
34
    
35
    public JsonObjectInArrayBuilderTestCases(String name){
36
        super(name);
37
    }
38
   
39
    public void testMarshalToArrayBuilderResult() throws Exception{
40
        JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithoutXmlRootElementRoot.class}, null);
41
        Marshaller jsonMarshaller = ctx.createMarshaller();
42
        jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
43
        jsonMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
44
        
45
        JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
46
        JsonArrayBuilderResult result = new JsonArrayBuilderResult(jsonArrayBuilder);
47
        
48
        
49
        WithoutXmlRootElementRoot foo = new WithoutXmlRootElementRoot();
50
        foo.setName("FOO");
51
        
52
        jsonMarshaller.marshal(foo, result);
53
        
54
        WithoutXmlRootElementRoot foo2 = new WithoutXmlRootElementRoot();
55
        foo.setName("FOO2");
56
        
57
        jsonMarshaller.marshal(foo, result);
58
59
        JsonArray jsonArray = jsonArrayBuilder.build();          
60
        StringWriter sw = new StringWriter();
61
        JsonWriter writer= Json.createWriter(sw);
62
        writer.writeArray(jsonArray);
63
        writer.close();
64
        
65
        log(sw.toString());
66
        String controlString = "[{\"name\":\"FOO\"},{\"name\":\"FOO2\"}]";
67
        assertEquals(controlString, sw.toString());        
68
    }
69
    
70
    public void testMarshalToArrayBuilderResultWithRoot() throws Exception{
71
        JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithXmlRootElementRoot.class}, null);
72
        Marshaller jsonMarshaller = ctx.createMarshaller();
73
        jsonMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
74
        jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
75
76
        JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
77
        JsonArrayBuilderResult result = new JsonArrayBuilderResult(jsonArrayBuilder);
78
        
79
        WithXmlRootElementRoot foo = new WithXmlRootElementRoot();
80
        foo.setName("FOO");
81
        
82
        jsonMarshaller.marshal(foo, result);
83
84
        WithXmlRootElementRoot foo2 = new WithXmlRootElementRoot();
85
        foo2.setName("FOO2");
86
        jsonMarshaller.marshal(foo2, result);
87
88
        JsonArray jsonArray = jsonArrayBuilder.build();          
89
        StringWriter sw = new StringWriter();
90
        JsonWriter writer= Json.createWriter(sw);
91
        writer.writeArray(jsonArray);
92
        writer.close();
93
        
94
        
95
        log(sw.toString());
96
        String controlString = "[{\"root\":{\"name\":\"FOO\"}},{\"root\":{\"name\":\"FOO2\"}}]";
97
        assertEquals(controlString, sw.toString());        
98
    }
99
    
100
   
101
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/rootlevellist/RootLevelListTestCases.java (-1 / +1 lines)
Lines 32-38 public class RootLevelListTestCases extends TestSuite { Link Here
32
        
32
        
33
        suite.addTestSuite(WithoutXmlRootElementArrayTestCases.class);
33
        suite.addTestSuite(WithoutXmlRootElementArrayTestCases.class);
34
        suite.addTestSuite(WithXmlRootElementArrayTestCases.class);
34
        suite.addTestSuite(WithXmlRootElementArrayTestCases.class);
35
35
        suite.addTestSuite(JsonObjectInArrayBuilderTestCases.class);
36
        return suite;
36
        return suite;
37
    }
37
    }
38
38

Return to bug 411377