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 411382 | Differences between
and this patch

Collapse All | Expand All

(-)a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/json/JsonGeneratorResult.java (+34 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2013 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.oxm.json;
14
15
import javax.json.stream.JsonGenerator;
16
17
import org.eclipse.persistence.internal.oxm.record.ExtendedResult;
18
import org.eclipse.persistence.oxm.record.JsonGeneratorRecord;
19
20
public class JsonGeneratorResult extends ExtendedResult{
21
22
    private JsonGenerator generator;
23
    
24
    public JsonGeneratorResult(JsonGenerator generator){
25
        this.generator = generator;
26
    }
27
28
    @Override
29
    public org.eclipse.persistence.oxm.record.MarshalRecord createRecord() {
30
        JsonGeneratorRecord record = new JsonGeneratorRecord(generator);
31
        return record;
32
    }    
33
34
}
(-)a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonBuilderRecord.java (-10 / +17 lines)
Lines 159-165 public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { Link Here
159
             position = rootLevel;
159
             position = rootLevel;
160
        } else {            
160
        } else {            
161
            if(isLastEventStart){
161
            if(isLastEventStart){
162
                position.setComplex(true);           
162
                setComplex(position, true);           
163
            }            
163
            }            
164
            Level level = new Level(true, position);            
164
            Level level = new Level(true, position);            
165
            position = level;
165
            position = level;
Lines 172-177 public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { Link Here
172
         popAndSetInParentBuilder();    
172
         popAndSetInParentBuilder();    
173
    }
173
    }
174
    
174
    
175
    private void setComplex(Level level, boolean complex){
176
        boolean isAlreadyComplex = level.isComplex;
177
        level.setComplex(complex);
178
        if(complex && !isAlreadyComplex){
179
            if(complex && level.jsonObjectBuilder == null){
180
                level.jsonObjectBuilder = Json.createObjectBuilder();
181
            }
182
        }
183
    }
184
    
175
    @Override    
185
    @Override    
176
    public void openStartElement(XPathFragment xPathFragment, NamespaceResolver namespaceResolver) {
186
    public void openStartElement(XPathFragment xPathFragment, NamespaceResolver namespaceResolver) {
177
        super.openStartElement(xPathFragment, namespaceResolver);
187
        super.openStartElement(xPathFragment, namespaceResolver);
Lines 180-186 public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { Link Here
180
            
190
            
181
            if(isLastEventStart){ 
191
            if(isLastEventStart){ 
182
                //this means 2 startevents in a row so the last this is a complex object
192
                //this means 2 startevents in a row so the last this is a complex object
183
                position.setComplex(true);                                
193
                setComplex(position, true);                                
184
            }
194
            }
185
                      
195
                      
186
            String keyName = getKeyName(xPathFragment);
196
            String keyName = getKeyName(xPathFragment);
Lines 283-293 public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { Link Here
283
                 
293
                 
284
                 if(xPathFragment.isSelfFragment() || xPathFragment.nameIsText()){
294
                 if(xPathFragment.isSelfFragment() || xPathFragment.nameIsText()){
285
                     String keyName = position.getKeyName();
295
                     String keyName = position.getKeyName();
286
                     position.setComplex(false);
296
                     setComplex(position, false);
287
                     position.parentLevel.getJsonObjectBuilder().add(keyName, Json.createArrayBuilder());                     
297
                     position.parentLevel.getJsonObjectBuilder().add(keyName, Json.createArrayBuilder());                     
288
                 }else{ 
298
                 }else{ 
289
                     if(isLastEventStart){
299
                     if(isLastEventStart){                         
290
                         position.setComplex(true);                  
300
                         setComplex(position, true);
291
                     }                 
301
                     }                 
292
                     String keyName =  getKeyName(xPathFragment);
302
                     String keyName =  getKeyName(xPathFragment);
293
                     if(keyName != null){
303
                     if(keyName != null){
Lines 329-335 public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { Link Here
329
    public void endElement(XPathFragment xPathFragment,NamespaceResolver namespaceResolver) {
339
    public void endElement(XPathFragment xPathFragment,NamespaceResolver namespaceResolver) {
330
        if(position != null){
340
        if(position != null){
331
            if(isLastEventStart){
341
            if(isLastEventStart){
332
                position.setComplex(true);
342
                setComplex(position, true);
333
            }
343
            }
334
            if(position.isComplex){
344
            if(position.isComplex){
335
                popAndSetInParentBuilder();
345
                popAndSetInParentBuilder();
Lines 446-452 public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { Link Here
446
        String keyName = position.getKeyName();
456
        String keyName = position.getKeyName();
447
        if(!position.isComplex){           
457
        if(!position.isComplex){           
448
            currentLevel = position.parentLevel;
458
            currentLevel = position.parentLevel;
449
            currentLevel.setComplex(true);          
459
            setComplex(currentLevel, true);          
450
        }
460
        }
451
        if(currentLevel.isCollection()){
461
        if(currentLevel.isCollection()){
452
            currentLevel.setEmptyCollection(false);
462
            currentLevel.setEmptyCollection(false);
Lines 803-811 public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { Link Here
803
813
804
        public void setComplex(boolean isComplex) {
814
        public void setComplex(boolean isComplex) {
805
            this.isComplex = isComplex;
815
            this.isComplex = isComplex;
806
            if(isComplex && jsonObjectBuilder == null){
807
                jsonObjectBuilder = Json.createObjectBuilder();
808
            }
809
        }
816
        }
810
817
811
    }
818
    }
(-)a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonGeneratorRecord.java (+763 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2013 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.oxm.record;
14
15
import java.io.IOException;
16
import java.io.StringWriter;
17
import java.math.BigDecimal;
18
import java.math.BigInteger;
19
import java.util.List;
20
21
import javax.json.stream.JsonGenerator;
22
import javax.xml.namespace.QName;
23
24
import org.eclipse.persistence.exceptions.XMLMarshalException;
25
import org.eclipse.persistence.internal.core.helper.CoreClassConstants;
26
import org.eclipse.persistence.internal.core.helper.CoreConversionManager;
27
import org.eclipse.persistence.internal.oxm.CharacterEscapeHandler;
28
import org.eclipse.persistence.internal.oxm.Constants;
29
import org.eclipse.persistence.internal.oxm.ConversionManager;
30
import org.eclipse.persistence.internal.oxm.NamespaceResolver;
31
import org.eclipse.persistence.internal.oxm.ObjectBuilder;
32
import org.eclipse.persistence.internal.oxm.Root;
33
import org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper;
34
import org.eclipse.persistence.internal.oxm.XMLMarshaller;
35
import org.eclipse.persistence.internal.oxm.XPathFragment;
36
import org.eclipse.persistence.internal.oxm.XMLConversionManager;
37
import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
38
import org.eclipse.persistence.internal.oxm.record.ExtendedContentHandler;
39
import org.eclipse.persistence.internal.oxm.record.XMLFragmentReader;
40
import org.w3c.dom.Attr;
41
import org.w3c.dom.Node;
42
import org.xml.sax.Attributes;
43
import org.xml.sax.Locator;
44
import org.xml.sax.SAXException;
45
import org.xml.sax.ext.LexicalHandler;
46
47
public class JsonGeneratorRecord extends MarshalRecord <XMLMarshaller> {
48
49
    private Level position;    
50
    private JsonGenerator jsonGenerator;
51
    private CharacterEscapeHandler characterEscapeHandler;
52
53
    private String attributePrefix;
54
    private boolean isRootArray;
55
    private static final String NULL="null";
56
    private boolean isLastEventStart;
57
        
58
    public JsonGeneratorRecord(JsonGenerator generator){
59
        super();       
60
        this.jsonGenerator = generator;
61
    }
62
    
63
    /**
64
     * INTERNAL:
65
     */
66
    public void setMarshaller(XMLMarshaller marshaller) {
67
        super.setMarshaller(marshaller);
68
        attributePrefix = marshaller.getAttributePrefix();
69
        if (marshaller.getValueWrapper() != null) {
70
            textWrapperFragment = new XPathFragment();
71
            textWrapperFragment.setLocalName(marshaller.getValueWrapper());
72
        }
73
        characterEscapeHandler = marshaller.getCharacterEscapeHandler();
74
    }
75
    
76
    @Override
77
    public void startDocument(String encoding, String version) {      
78
        if(isRootArray){  
79
            if(position == null){
80
                startCollection();
81
            }                            
82
            position.setEmptyCollection(false); 
83
            Level newLevel = new Level(false, position);
84
            position = newLevel;
85
            isLastEventStart = true;
86
        }else{
87
            Level rootLevel = new Level(false, null);
88
            position = rootLevel;
89
            setComplex(position, true);
90
        }
91
    }
92
93
    @Override
94
    public void endDocument() {        
95
        if(position != null){
96
            jsonGenerator.writeEnd(); 
97
            position = position.parentLevel;
98
        }
99
    }
100
    
101
    private void popAndSetInParentBuilder(){
102
        if(!(position.isCollection && position.isEmptyCollection() && position.getKeyName() == null)){
103
            jsonGenerator.writeEnd(); 
104
        }
105
        position = position.parentLevel;      
106
    }    
107
    
108
    public void startCollection() {
109
        if(position == null){
110
             isRootArray = true;              
111
             Level rootLevel = new Level(true, null); 
112
             position = rootLevel;
113
             jsonGenerator.writeStartArray();
114
        } else {            
115
            if(isLastEventStart){
116
                setComplex(position, true);         
117
            }            
118
            Level level = new Level(true, position);            
119
            position = level;
120
        }      
121
        isLastEventStart = false;
122
    }
123
    
124
    @Override
125
    public void endCollection() {        
126
         popAndSetInParentBuilder();    
127
    }
128
129
    private void setComplex(Level level, boolean complex){
130
        boolean isAlreadyComplex = level.isComplex;
131
        level.setComplex(complex);
132
        if(complex && !isAlreadyComplex){
133
            Level parentLevel = level.parentLevel;
134
            if((parentLevel != null && parentLevel.isCollection && !parentLevel.isEmptyCollection()) || level.keyName == null){
135
                jsonGenerator.writeStartObject();                    
136
            }else{
137
                jsonGenerator.writeStartObject(level.keyName);
138
            }
139
        }
140
    }
141
    
142
    @Override    
143
    public void openStartElement(XPathFragment xPathFragment, NamespaceResolver namespaceResolver) {
144
        super.openStartElement(xPathFragment, namespaceResolver);
145
        if(position != null){
146
            Level newLevel = new Level(false, position);            
147
            
148
            if(isLastEventStart){                              
149
                //this means 2 startevents in a row so the last this is a complex object
150
                setComplex(position, true);
151
            }
152
                      
153
            String keyName = getKeyName(xPathFragment);
154
           
155
            if(position.isCollection && position.isEmptyCollection() ){
156
                position.setKeyName(keyName);
157
                jsonGenerator.writeStartArray(keyName);
158
                position.setEmptyCollection(false);
159
            }else{
160
                newLevel.setKeyName(keyName);    
161
            }
162
            position = newLevel;   
163
            isLastEventStart = true;
164
        }
165
    }
166
    
167
    protected String getKeyName(XPathFragment xPathFragment){
168
        String keyName = xPathFragment.getLocalName(); 
169
       
170
        if(isNamespaceAware()){
171
            if(xPathFragment.getNamespaceURI() != null){
172
                String prefix = null;
173
                if(getNamespaceResolver() !=null){
174
                    prefix = getNamespaceResolver().resolveNamespaceURI(xPathFragment.getNamespaceURI());
175
                } else if(namespaceResolver != null){
176
                    prefix = namespaceResolver.resolveNamespaceURI(xPathFragment.getNamespaceURI());
177
                }
178
                if(prefix != null && !prefix.equals(Constants.EMPTY_STRING)){
179
                    keyName = prefix + getNamespaceSeparator() +  keyName;                           
180
                }
181
            }
182
        } 
183
        if(xPathFragment.isAttribute() && attributePrefix != null){
184
            keyName = attributePrefix + keyName;
185
        }
186
187
        return keyName;
188
    }
189
190
    public void attribute(XPathFragment xPathFragment, NamespaceResolver namespaceResolver,  Object value, QName schemaType){
191
        if(xPathFragment.getNamespaceURI() != null && xPathFragment.getNamespaceURI() == javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI){
192
            return;
193
        }
194
        xPathFragment.setAttribute(true);
195
        openStartElement(xPathFragment, namespaceResolver);
196
        characters(schemaType, value, null, false, true);
197
        endElement(xPathFragment, namespaceResolver);
198
    }
199
    
200
    /**
201
     * INTERNAL:
202
     */
203
    @Override
204
    public void marshalWithoutRootElement(ObjectBuilder treeObjectBuilder, Object object, Descriptor descriptor, Root root, boolean isXMLRoot){
205
        if(treeObjectBuilder != null){
206
            addXsiTypeAndClassIndicatorIfRequired(descriptor, null, descriptor.getDefaultRootElementField(), root, object, isXMLRoot, true);
207
            treeObjectBuilder.marshalAttributes(this, object, session);
208
        }         
209
     }
210
    
211
    /**
212
     * INTERNAL:
213
     * The character used to separate the prefix and uri portions when namespaces are present 
214
     * @since 2.4
215
     */
216
    public char getNamespaceSeparator(){        
217
        return marshaller.getNamespaceSeparator();
218
    }
219
    
220
    /**
221
     * INTERNAL:
222
     * The optional fragment used to wrap the text() mappings
223
     * @since 2.4
224
     */
225
    public XPathFragment getTextWrapperFragment() {
226
        return textWrapperFragment;
227
    }
228
    
229
    @Override
230
    public boolean isWrapperAsCollectionName() {
231
        return marshaller.isWrapperAsCollectionName();
232
    }
233
    
234
    @Override
235
    public void element(XPathFragment frag) {
236
        isLastEventStart = false;
237
    }
238
    
239
    /**
240
     * Handle marshal of an empty collection.  
241
     * @param xPathFragment
242
     * @param namespaceResolver
243
     * @param openGrouping if grouping elements should be marshalled for empty collections
244
     * @return
245
     */    
246
    public boolean emptyCollection(XPathFragment xPathFragment, NamespaceResolver namespaceResolver, boolean openGrouping) {
247
248
         if(marshaller.isMarshalEmptyCollections()){
249
             super.emptyCollection(xPathFragment, namespaceResolver, true);
250
             
251
             if (null != xPathFragment) {
252
                 
253
                 if(xPathFragment.isSelfFragment() || xPathFragment.nameIsText()){
254
                     String keyName = position.getKeyName();
255
                     setComplex(position, false);
256
                     jsonGenerator.writeStartArray(keyName);
257
                     jsonGenerator.writeEnd();
258
                 }else{ 
259
                     if(isLastEventStart){
260
                         setComplex(position, true);
261
                     }                 
262
                     String keyName =  getKeyName(xPathFragment);
263
                     if(keyName != null){
264
                         jsonGenerator.writeStartArray(keyName);
265
                         jsonGenerator.writeEnd();
266
                     }
267
                 }
268
                 isLastEventStart = false;   
269
             }
270
                  
271
             return true;
272
         }else{
273
             return super.emptyCollection(xPathFragment, namespaceResolver, openGrouping);
274
         }
275
    }
276
277
    @Override
278
    public void attribute(XPathFragment xPathFragment,NamespaceResolver namespaceResolver, String value) {
279
        attribute(xPathFragment, namespaceResolver, value, null);
280
    }
281
282
    @Override
283
    public void attribute(String namespaceURI, String localName, String qName, String value) {
284
        XPathFragment xPathFragment = new XPathFragment();
285
        xPathFragment.setNamespaceURI(namespaceURI);
286
        xPathFragment.setAttribute(true);
287
        xPathFragment.setLocalName(localName);
288
289
        openStartElement(xPathFragment, namespaceResolver);
290
        characters(null, value, null, false, true);
291
292
        endElement(xPathFragment, namespaceResolver);
293
        
294
    }
295
296
    @Override
297
    public void closeStartElement() {}
298
299
    @Override
300
    public void endElement(XPathFragment xPathFragment,NamespaceResolver namespaceResolver) {
301
        if(position != null){            
302
            if(isLastEventStart){
303
                setComplex(position, true);
304
            }
305
            if(position.isComplex){
306
                popAndSetInParentBuilder();
307
            }else{
308
                position = position.parentLevel;
309
            }            
310
            isLastEventStart = false;
311
        }
312
    }
313
314
    @Override
315
    public void characters(String value) {
316
        writeValue(value, null, false);
317
    }
318
319
    @Override
320
    public void characters(QName schemaType, Object value, String mimeType, boolean isCDATA){          
321
        characters(schemaType, value, mimeType, isCDATA, false);
322
     }
323
    
324
    public void characters(QName schemaType, Object value, String mimeType, boolean isCDATA, boolean isAttribute){
325
        if(mimeType != null) {
326
            if(value instanceof List){
327
               value = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesListForBinaryValues((List)value, marshaller, mimeType);
328
           }else{
329
330
            value = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(value, marshaller, mimeType).getData();
331
           }
332
        }         
333
        if(schemaType != null && Constants.QNAME_QNAME.equals(schemaType)){
334
            String convertedValue = getStringForQName((QName)value);
335
            writeValue(convertedValue, null, isAttribute);
336
        } 
337
        else if(value.getClass() == String.class){          
338
            //if schemaType is set and it's a numeric or boolean type don't treat as a string
339
            if(schemaType != null && isNumericOrBooleanType(schemaType)){
340
                Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType);
341
                Object convertedValue = ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, theClass, schemaType);
342
                writeValue(convertedValue, schemaType, isAttribute);
343
            }else if(isCDATA){
344
                cdata((String)value);
345
            }else{
346
                writeValue((String)value, null, isAttribute);                
347
            }
348
       }else{
349
           Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType);          
350
351
           if(schemaType == null || theClass == null){
352
               if(value.getClass() == CoreClassConstants.BOOLEAN || CoreClassConstants.NUMBER.isAssignableFrom(value.getClass())){
353
                   writeValue(value, schemaType, isAttribute);
354
               }else{
355
                   String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType));
356
                   writeValue(convertedValue, schemaType, isAttribute);
357
               }
358
           }else if(schemaType != null && !isNumericOrBooleanType(schemaType)){
359
               //if schemaType exists and is not boolean or number do write quotes (convert to string)
360
               String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType));
361
               writeValue(convertedValue, schemaType, isAttribute);
362
           } else if(isCDATA){
363
               String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType));
364
               cdata(convertedValue);
365
           }else{
366
               writeValue(value, schemaType, isAttribute);           
367
           }
368
       }        
369
    }
370
    
371
    
372
    private boolean isNumericOrBooleanType(QName schemaType){
373
        if(schemaType == null){
374
            return false;
375
        }else if(schemaType.equals(Constants.BOOLEAN_QNAME)
376
                || schemaType.equals(Constants.INTEGER_QNAME)
377
                || schemaType.equals(Constants.INT_QNAME)
378
                || schemaType.equals(Constants.BYTE_QNAME)
379
                || schemaType.equals(Constants.DECIMAL_QNAME)
380
                || schemaType.equals(Constants.FLOAT_QNAME)
381
                || schemaType.equals(Constants.DOUBLE_QNAME)
382
                || schemaType.equals(Constants.SHORT_QNAME)
383
                || schemaType.equals(Constants.LONG_QNAME)
384
                || schemaType.equals(Constants.NEGATIVE_INTEGER_QNAME)
385
                || schemaType.equals(Constants.NON_NEGATIVE_INTEGER_QNAME)
386
                || schemaType.equals(Constants.NON_POSITIVE_INTEGER_QNAME)
387
                || schemaType.equals(Constants.POSITIVE_INTEGER_QNAME)
388
                || schemaType.equals(Constants.UNSIGNED_BYTE_QNAME)
389
                || schemaType.equals(Constants.UNSIGNED_INT_QNAME)
390
                || schemaType.equals(Constants.UNSIGNED_LONG_QNAME)
391
                || schemaType.equals(Constants.UNSIGNED_SHORT_QNAME)
392
        ){
393
            return true;
394
        }
395
        return false;
396
    }
397
    
398
    public void writeValue(Object value, QName schemaType, boolean isAttribute) {
399
        
400
        if (characterEscapeHandler != null && value instanceof String) {
401
            try {
402
                StringWriter stringWriter = new StringWriter();
403
                characterEscapeHandler.escape(((String)value).toCharArray(), 0, ((String)value).length(), isAttribute, stringWriter);
404
                value = stringWriter.toString();
405
            } catch (IOException e) {
406
                throw XMLMarshalException.marshalException(e);
407
            }
408
        }
409
        
410
        boolean textWrapperOpened = false;                       
411
        if(!isLastEventStart){
412
             openStartElement(textWrapperFragment, namespaceResolver);
413
             textWrapperOpened = true;
414
        }
415
      
416
        Level currentLevel = position;
417
        String keyName = position.getKeyName();
418
        if(!position.isComplex){           
419
            currentLevel = position.parentLevel;         
420
        }
421
        addValue(currentLevel, keyName, value, schemaType);
422
        isLastEventStart = false;
423
        if(textWrapperOpened){    
424
             endElement(textWrapperFragment, namespaceResolver);
425
        }    
426
    }
427
    private void addValue(Level currentLevel, String keyName, Object value, QName schemaType){        
428
        if(currentLevel.isCollection()){
429
            currentLevel.setEmptyCollection(false);
430
            addValueToArray(value, schemaType);            
431
        } else {
432
            addValueToObject(keyName, value, schemaType);
433
            
434
        }
435
    }
436
    
437
    private void addValueToObject(String keyName, Object value, QName schemaType){
438
        
439
        if(value == NULL){
440
            jsonGenerator.writeNull(keyName);            
441
        }else if(value instanceof Integer){
442
            jsonGenerator.write(keyName, (Integer)value);             
443
        }else if(value instanceof BigDecimal){            
444
            jsonGenerator.write(keyName, (BigDecimal)value);
445
        }else if(value instanceof BigInteger){
446
            jsonGenerator.write(keyName, (BigInteger)value);
447
        }else if(value instanceof Boolean){
448
            jsonGenerator.write(keyName, (Boolean)value);
449
        }else if(value instanceof Character){
450
            jsonGenerator.write(keyName, (Character)value);;  
451
        }else if(value instanceof Double){
452
            jsonGenerator.write(keyName, (Double)value);
453
        }else if(value instanceof Float){
454
            jsonGenerator.write(keyName, (Float)value);
455
        }else if(value instanceof Long){
456
            jsonGenerator.write(keyName, (Long)value);
457
        }else if(value instanceof String){
458
            jsonGenerator.write(keyName, (String)value);
459
        }else{
460
            String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType));
461
            Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType);          
462
            if((schemaType == null || theClass == null) && (CoreClassConstants.NUMBER.isAssignableFrom(value.getClass()))){
463
                //if it's still a number and falls through the cracks we dont want "" around the value
464
                    BigDecimal convertedNumberValue = ((BigDecimal) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.BIGDECIMAL, schemaType));
465
                    jsonGenerator.write(keyName, (BigDecimal)convertedNumberValue);
466
            }else{
467
                jsonGenerator.write(keyName, convertedValue);                
468
            }
469
                
470
        }
471
    }
472
    
473
    private void addValueToArray(Object value, QName schemaType){
474
        if(value == NULL){            
475
            jsonGenerator.writeNull();
476
        }else if(value instanceof Integer){
477
            jsonGenerator.write((Integer)value);             
478
        }else if(value instanceof BigDecimal){
479
            jsonGenerator.write((BigDecimal)value);
480
        }else if(value instanceof BigInteger){
481
            jsonGenerator.write((BigInteger)value);
482
        }else if(value instanceof Boolean){                            
483
            jsonGenerator.write((Boolean)value);
484
        }else if(value instanceof Character){
485
            jsonGenerator.write((Character)value);
486
        }else if(value instanceof Double){
487
            jsonGenerator.write((Double)value);
488
        }else if(value instanceof Float){
489
            jsonGenerator.write((Float)value);
490
        }else if(value instanceof Long){
491
            jsonGenerator.write((Long)value);
492
        }else if(value instanceof String){
493
            jsonGenerator.write((String)value);            
494
        }else{
495
            String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType));
496
            Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType);          
497
            if((schemaType == null || theClass == null) && (CoreClassConstants.NUMBER.isAssignableFrom(value.getClass()))){
498
                //if it's still a number and falls through the cracks we dont want "" around the value
499
                    BigDecimal convertedNumberValue = ((BigDecimal) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.BIGDECIMAL, schemaType));
500
                    jsonGenerator.write((BigDecimal)convertedNumberValue);            
501
502
            }else{
503
                jsonGenerator.write(convertedValue);                
504
            }
505
        }
506
    }
507
    
508
    @Override
509
    public void cdata(String value) {
510
        characters(value);        
511
    }
512
513
    @Override
514
    public void node(Node node, NamespaceResolver resolver, String uri, String name) {
515
       
516
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
517
            Attr attr = (Attr) node;
518
            String resolverPfx = null;
519
            if (getNamespaceResolver() != null) {
520
                resolverPfx = this.getNamespaceResolver().resolveNamespaceURI(attr.getNamespaceURI());
521
            }
522
            String namespaceURI = attr.getNamespaceURI();
523
            // If the namespace resolver contains a prefix for the attribute's URI,
524
            // use it instead of what is set on the attribute
525
            if (resolverPfx != null) {
526
                attribute(attr.getNamespaceURI(), Constants.EMPTY_STRING, resolverPfx+Constants.COLON+attr.getLocalName(), attr.getNodeValue());
527
            } else {
528
                attribute(attr.getNamespaceURI(), Constants.EMPTY_STRING, attr.getName(), attr.getNodeValue());
529
                // May need to declare the URI locally
530
                if (attr.getNamespaceURI() != null) {
531
                    attribute(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, Constants.EMPTY_STRING, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON + attr.getPrefix(), attr.getNamespaceURI());
532
                    this.getNamespaceResolver().put(attr.getPrefix(), attr.getNamespaceURI());
533
                }
534
            }
535
        } else if (node.getNodeType() == Node.TEXT_NODE) {
536
            writeValue(node.getNodeValue(), null, false);
537
        } else {
538
            try {
539
                JsonObjectBuilderRecordContentHandler wrcHandler = new JsonObjectBuilderRecordContentHandler();
540
                
541
                XMLFragmentReader xfragReader = new XMLFragmentReader(namespaceResolver);
542
                xfragReader.setContentHandler(wrcHandler);
543
                xfragReader.setProperty("http://xml.org/sax/properties/lexical-handler", wrcHandler);
544
                xfragReader.parse(node, uri, name);
545
            } catch (SAXException sex) {
546
                throw XMLMarshalException.marshalException(sex);
547
            }
548
        }
549
        
550
    }        
551
    
552
    protected String getStringForQName(QName qName){
553
        if(null == qName) {
554
            return null;
555
        }
556
        CoreConversionManager xmlConversionManager = getSession().getDatasourcePlatform().getConversionManager();
557
558
        return (String) xmlConversionManager.convertObject(qName, String.class);       
559
    }
560
561
    /**
562
     * INTERNAL:
563
     */
564
     public void namespaceDeclarations(NamespaceResolver namespaceResolver) {
565
     }
566
567
     public void namespaceDeclaration(String prefix, String namespaceURI){
568
     }
569
     
570
     public void defaultNamespaceDeclaration(String defaultNamespace){
571
     }
572
     
573
    /**
574
     * INTERNAL:
575
     */
576
     public void nilComplex(XPathFragment xPathFragment, NamespaceResolver namespaceResolver){
577
         XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver);
578
         closeStartGroupingElements(groupingFragment);
579
         openStartElement(xPathFragment, namespaceResolver);
580
         characters(NULL);
581
         endElement(xPathFragment, namespaceResolver);
582
     }
583
584
    /**
585
     * INTERNAL:
586
     */
587
     public void nilSimple(NamespaceResolver namespaceResolver){
588
         XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver);         
589
         characters(NULL);        
590
         closeStartGroupingElements(groupingFragment);
591
     }
592
593
     /**
594
      * Used when an empty simple value should be written
595
      * @since EclipseLink 2.4
596
      */
597
     public void emptySimple(NamespaceResolver namespaceResolver){
598
         nilSimple(namespaceResolver);
599
     }
600
     
601
     public void emptyAttribute(XPathFragment xPathFragment,NamespaceResolver namespaceResolver){
602
         XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver);
603
         openStartElement(xPathFragment, namespaceResolver);
604
         characters(NULL);
605
         endElement(xPathFragment, namespaceResolver);
606
         closeStartGroupingElements(groupingFragment);
607
     }
608
609
     /**
610
      * Used when an empty complex item should be written
611
      * @since EclipseLink 2.4
612
      */
613
     public void emptyComplex(XPathFragment xPathFragment, NamespaceResolver namespaceResolver){
614
         XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver);
615
         closeStartGroupingElements(groupingFragment);
616
         openStartElement(xPathFragment, namespaceResolver);
617
         endElement(xPathFragment, namespaceResolver);
618
     }
619
    
620
    
621
     
622
     /**
623
      * This class will typically be used in conjunction with an XMLFragmentReader.
624
      * The XMLFragmentReader will walk a given XMLFragment node and report events
625
      * to this class - the event's data is then written to the enclosing class'
626
      * writer.
627
      *
628
      * @see org.eclipse.persistence.internal.oxm.record.XMLFragmentReader
629
      */
630
     protected class JsonObjectBuilderRecordContentHandler implements ExtendedContentHandler, LexicalHandler {
631
632
         JsonObjectBuilderRecordContentHandler() {
633
         }
634
635
         // --------------------- CONTENTHANDLER METHODS --------------------- //
636
         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
637
                 XPathFragment xPathFragment = new XPathFragment(localName);
638
                 xPathFragment.setNamespaceURI(namespaceURI);
639
                 openStartElement(xPathFragment, namespaceResolver);
640
                 handleAttributes(atts);
641
         }
642
643
         public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
644
             XPathFragment xPathFragment = new XPathFragment(localName);
645
             xPathFragment.setNamespaceURI(namespaceURI);
646
             
647
             JsonGeneratorRecord.this.endElement(xPathFragment, namespaceResolver);        
648
         }
649
650
         public void startPrefixMapping(String prefix, String uri) throws SAXException {
651
         }
652
653
         public void characters(char[] ch, int start, int length) throws SAXException {
654
             String characters = new String (ch, start, length);
655
             characters(characters);
656
         }
657
658
         public void characters(CharSequence characters) throws SAXException {           
659
             JsonGeneratorRecord.this.characters(characters.toString());      
660
         }
661
662
         // --------------------- LEXICALHANDLER METHODS --------------------- //
663
         public void comment(char[] ch, int start, int length) throws SAXException {
664
         }
665
666
         public void startCDATA() throws SAXException {
667
         }
668
669
         public void endCDATA() throws SAXException {
670
         }
671
672
         // --------------------- CONVENIENCE METHODS --------------------- //
673
            protected void handleAttributes(Attributes atts) {
674
             for (int i=0, attsLength = atts.getLength(); i<attsLength; i++) {
675
                 String qName = atts.getQName(i);
676
                 if((qName != null && (qName.startsWith(javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON) || qName.equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)))) {
677
                     continue;
678
                 }
679
                 attribute(atts.getURI(i), atts.getLocalName(i), qName, atts.getValue(i));
680
             }
681
         }
682
683
         protected void writeComment(char[] chars, int start, int length) {        
684
         }
685
        
686
         protected void writeCharacters(char[] chars, int start, int length) {
687
             try {
688
                 characters(chars, start, length);
689
             } catch (SAXException e) {
690
                 throw XMLMarshalException.marshalException(e);
691
             }           
692
         }
693
         // --------------- SATISFY CONTENTHANDLER INTERFACE --------------- //
694
         public void endPrefixMapping(String prefix) throws SAXException {}
695
         public void processingInstruction(String target, String data) throws SAXException {}
696
         public void setDocumentLocator(Locator locator) {}
697
         public void startDocument() throws SAXException {}
698
         public void endDocument() throws SAXException {}
699
         public void skippedEntity(String name) throws SAXException {}
700
         public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
701
702
         // --------------- SATISFY LEXICALHANDLER INTERFACE --------------- //
703
         public void startEntity(String name) throws SAXException {}
704
         public void endEntity(String name) throws SAXException {}
705
         public void startDTD(String name, String publicId, String systemId) throws SAXException {}
706
         public void endDTD() throws SAXException {}
707
         @Override
708
         public void setNil(boolean isNil) {}
709
710
     }
711
712
     
713
     /**
714
     * Instances of this class are used to maintain state about the current
715
     * level of the JSON message being marshalled.
716
     */
717
    protected static class Level {
718
        
719
        private boolean isCollection;
720
        private boolean emptyCollection;
721
        private String keyName;                
722
        private boolean isComplex;
723
        private Level parentLevel;
724
        
725
        public Level(boolean isCollection, Level parentLevel) {
726
            setCollection(isCollection);
727
            emptyCollection = true;
728
            this.parentLevel = parentLevel;
729
        }
730
731
        public boolean isCollection() {
732
            return isCollection;
733
        }
734
735
        public void setCollection(boolean isCollection) {
736
            this.isCollection = isCollection;            
737
        }
738
739
        public String getKeyName() {
740
            return keyName;
741
        }
742
743
        public void setKeyName(String keyName) {
744
            this.keyName = keyName;
745
        }
746
      
747
        public boolean isEmptyCollection() {
748
            return emptyCollection;
749
        }
750
751
        public void setEmptyCollection(boolean emptyCollection) {
752
            this.emptyCollection = emptyCollection;
753
        }
754
        public boolean isComplex() {
755
            return isComplex;
756
        }
757
758
        public void setComplex(boolean isComplex) {
759
            this.isComplex = isComplex;                     
760
        }        
761
    }
762
763
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/JAXBWithJSONTestCases.java (-1 / +22 lines)
Lines 28-33 import javax.json.JsonObjectBuilder; Link Here
28
import javax.json.JsonReader;
28
import javax.json.JsonReader;
29
import javax.json.JsonStructure;
29
import javax.json.JsonStructure;
30
import javax.json.JsonWriter;
30
import javax.json.JsonWriter;
31
import javax.json.stream.JsonGenerator;
31
import javax.json.stream.JsonParser;
32
import javax.json.stream.JsonParser;
32
import javax.xml.bind.JAXBElement;
33
import javax.xml.bind.JAXBElement;
33
import javax.xml.bind.Marshaller;
34
import javax.xml.bind.Marshaller;
Lines 41-46 import org.eclipse.persistence.jaxb.JAXBContext; Link Here
41
import org.eclipse.persistence.jaxb.MarshallerProperties;
42
import org.eclipse.persistence.jaxb.MarshallerProperties;
42
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
43
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
43
import org.eclipse.persistence.oxm.MediaType;
44
import org.eclipse.persistence.oxm.MediaType;
45
import org.eclipse.persistence.oxm.json.JsonGeneratorResult;
44
import org.eclipse.persistence.oxm.json.JsonObjectBuilderResult;
46
import org.eclipse.persistence.oxm.json.JsonObjectBuilderResult;
45
import org.eclipse.persistence.oxm.json.JsonStructureSource;
47
import org.eclipse.persistence.oxm.json.JsonStructureSource;
46
import org.eclipse.persistence.testing.jaxb.JAXBTestCases.MyStreamSchemaOutputResolver;
48
import org.eclipse.persistence.testing.jaxb.JAXBTestCases.MyStreamSchemaOutputResolver;
Lines 314-319 public abstract class JAXBWithJSONTestCases extends JAXBTestCases { Link Here
314
        log(sw.toString());
316
        log(sw.toString());
315
        compareStringToControlFile("testJSONMarshalToStringWriter_FORMATTED", sw.toString(), getWriteControlJSONFormatted(),shouldRemoveWhitespaceFromControlDocJSON());
317
        compareStringToControlFile("testJSONMarshalToStringWriter_FORMATTED", sw.toString(), getWriteControlJSONFormatted(),shouldRemoveWhitespaceFromControlDocJSON());
316
    }
318
    }
319
    
317
    public void testJSONMarshalToBuilderResult() throws Exception{
320
    public void testJSONMarshalToBuilderResult() throws Exception{
318
        getJSONMarshaller().setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
321
        getJSONMarshaller().setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
319
322
Lines 333-341 public abstract class JAXBWithJSONTestCases extends JAXBTestCases { Link Here
333
        writer.close();
336
        writer.close();
334
        
337
        
335
        log(sw.toString());
338
        log(sw.toString());
336
        compareStringToControlFile("**testJSONMarshalToStringWriter**", sw.toString());
339
        compareStringToControlFile("**testJSONMarshalToBuilderResult**", sw.toString());
337
    }
340
    }
338
341
342
    public void testJSONMarshalToGeneratorResult() throws Exception{
343
        getJSONMarshaller().setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
344
345
        StringWriter sw = new StringWriter();
346
        JsonGenerator generator= Json.createGenerator(sw);
347
        JsonGeneratorResult result = new JsonGeneratorResult(generator);
348
        try{
349
            getJSONMarshaller().marshal(getWriteControlObject(), result);
350
        } catch(Exception e) {
351
            assertMarshalException(e);
352
            return;
353
        }
354
        generator.flush();
355
        log(sw.toString());
356
        compareStringToControlFile("**testJSONMarshalToGeneratorResult**", sw.toString());
357
    }
358
359
    
339
    protected void compareStringToControlFile(String test, String testString) {
360
    protected void compareStringToControlFile(String test, String testString) {
340
    	compareStringToControlFile(test, testString, getWriteControlJSON());
361
    	compareStringToControlFile(test, testString, getWriteControlJSON());
341
    }
362
    }
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/externalizedmetadata/mappings/choicecollection/ChoiceCollectionMappingTestCases.java (-1 / +3 lines)
Lines 148-152 public class ChoiceCollectionMappingTestCases extends JAXBWithJSONTestCases { Link Here
148
    public void testJSONMarshalToBuilderResult() throws Exception{
148
    public void testJSONMarshalToBuilderResult() throws Exception{
149
        //Currently not supported, 101.1 will get written as 101.0999984741211 
149
        //Currently not supported, 101.1 will get written as 101.0999984741211 
150
    }
150
    }
151
   
151
    public void testJSONMarshalToGeneratorResult() throws Exception{        
152
        //Currently not supported, 101.1 will get written as 101.0999984741211 
153
    }
152
}
154
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/JSONTestCases.java (+14 lines)
Lines 28-33 import javax.json.JsonObject; Link Here
28
import javax.json.JsonObjectBuilder;
28
import javax.json.JsonObjectBuilder;
29
import javax.json.JsonWriter;
29
import javax.json.JsonWriter;
30
import javax.json.JsonWriterFactory;
30
import javax.json.JsonWriterFactory;
31
import javax.json.stream.JsonGenerator;
31
import javax.xml.bind.JAXBContext;
32
import javax.xml.bind.JAXBContext;
32
import javax.xml.bind.JAXBElement;
33
import javax.xml.bind.JAXBElement;
33
import javax.xml.bind.Marshaller;
34
import javax.xml.bind.Marshaller;
Lines 38-43 import org.eclipse.persistence.jaxb.MarshallerProperties; Link Here
38
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
39
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
39
import org.eclipse.persistence.oxm.XMLContext;
40
import org.eclipse.persistence.oxm.XMLContext;
40
import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult;
41
import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult;
42
import org.eclipse.persistence.oxm.json.JsonGeneratorResult;
41
import org.eclipse.persistence.oxm.json.JsonObjectBuilderResult;
43
import org.eclipse.persistence.oxm.json.JsonObjectBuilderResult;
42
import org.eclipse.persistence.testing.oxm.OXTestCase;
44
import org.eclipse.persistence.testing.oxm.OXTestCase;
43
45
Lines 225-230 public abstract class JSONTestCases extends OXTestCase{ Link Here
225
           log(sw.toString());
227
           log(sw.toString());
226
           compareStringToControlFile("**testJSONMarshalToBuilderResult**", sw.toString());
228
           compareStringToControlFile("**testJSONMarshalToBuilderResult**", sw.toString());
227
       }
229
       }
230
    
231
    public void testJSONMarshalToGeneratorResult() throws Exception{
232
        
233
        StringWriter sw = new StringWriter();
234
        JsonGenerator jsonGenerator = Json.createGenerator(sw);
235
        JsonGeneratorResult result = new JsonGeneratorResult(jsonGenerator);
236
        jsonMarshaller.marshal(getWriteControlObject(), result);
237
        jsonGenerator.flush();
238
        
239
        log(sw.toString());
240
        compareStringToControlFile("**testJSONMarshalToBuilderResult**", sw.toString());
241
    }
228
	   
242
	   
229
	 public void testJSONMarshalToOutputStream_FORMATTED() throws Exception{
243
	 public void testJSONMarshalToOutputStream_FORMATTED() throws Exception{
230
	    jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
244
	    jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/characters/UsAsciiTestCases.java (-1 / +5 lines)
Lines 52-59 public class UsAsciiTestCases extends JSONMarshalUnmarshalTestCases { Link Here
52
        return control;
52
        return control;
53
    }
53
    }
54
    
54
    
55
    public void marshalToObjectBuilderResult() throws Exception{
55
    public void testJSONMarshalToBuilderResult() throws Exception{
56
        //Currently not supported
56
        //Currently not supported
57
    }
57
    }
58
58
59
    public void testJSONMarshalToGeneratorResult() throws Exception{
60
        //Currently not supported
61
62
    }
59
}
63
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementJSONPaddingTestCases.java (+2 lines)
Lines 49-52 public class JAXBElementJSONPaddingTestCases extends JAXBWithJSONTestCases{ Link Here
49
	public void testJSONMarshalToBuilderResult() throws Exception{     
49
	public void testJSONMarshalToBuilderResult() throws Exception{     
50
    }
50
    }
51
    
51
    
52
    public void testJSONMarshalToGeneratorResult() throws Exception{
53
    }
52
}
54
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementListJSONPaddingTestCases.java (+3 lines)
Lines 60-63 public class JAXBElementListJSONPaddingTestCases extends JSONTestCases{ Link Here
60
	
60
	
61
	public void testJSONMarshalToBuilderResult() throws Exception{     
61
	public void testJSONMarshalToBuilderResult() throws Exception{     
62
    }
62
    }
63
	
64
	public void testJSONMarshalToGeneratorResult() throws Exception{     
65
    }
63
}
66
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullNameTestCases.java (+3 lines)
Lines 45-49 public class JSONWithNullNameTestCases extends JAXBWithJSONTestCases{ Link Here
45
	
45
	
46
	public void testJSONMarshalToBuilderResult() throws Exception{     
46
	public void testJSONMarshalToBuilderResult() throws Exception{     
47
    }
47
    }
48
	
49
	public void testJSONMarshalToGeneratorResult() throws Exception{     
50
    }
48
    
51
    
49
}
52
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullObjectTestCases.java (+3 lines)
Lines 49-54 public class JSONWithNullObjectTestCases extends JAXBWithJSONTestCases{ Link Here
49
	public void testJSONMarshalToBuilderResult() throws Exception{     
49
	public void testJSONMarshalToBuilderResult() throws Exception{     
50
    }
50
    }
51
	
51
	
52
	public void testJSONMarshalToGeneratorResult() throws Exception{     
53
    }
54
	
52
    @Override
55
    @Override
53
    public void assertMarshalException(Exception exception) throws Exception {
56
    public void assertMarshalException(Exception exception) throws Exception {
54
        Exception nestedException = (Exception) exception.getCause();
57
        Exception nestedException = (Exception) exception.getCause();
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleListTestCases.java (+3 lines)
Lines 58-63 public class JSONWithPaddingSimpleListTestCases extends JSONTestCases{ Link Here
58
	public void testJSONMarshalToBuilderResult() throws Exception{     
58
	public void testJSONMarshalToBuilderResult() throws Exception{     
59
    }
59
    }
60
	
60
	
61
	public void testJSONMarshalToGeneratorResult() throws Exception{     
62
    }
63
	
61
//	public boolean shouldRemoveWhitespaceFromControlDocJSON(){
64
//	public boolean shouldRemoveWhitespaceFromControlDocJSON(){
62
	//	return false;
65
	//	return false;
63
	//}
66
	//}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleTestCases.java (+3 lines)
Lines 51-55 public class JSONWithPaddingSimpleTestCases extends JAXBWithJSONTestCases{ Link Here
51
	
51
	
52
	public void testJSONMarshalToBuilderResult() throws Exception{     
52
	public void testJSONMarshalToBuilderResult() throws Exception{     
53
    }
53
    }
54
	
55
	public void testJSONMarshalToGeneratorResult() throws Exception{     
56
    }	
54
    
57
    
55
}
58
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithUnsetNameTestCases.java (+2 lines)
Lines 46-49 public class JSONWithUnsetNameTestCases extends JAXBWithJSONTestCases{ Link Here
46
	public void testJSONMarshalToBuilderResult() throws Exception{     
46
	public void testJSONMarshalToBuilderResult() throws Exception{     
47
    }
47
    }
48
    
48
    
49
	public void testJSONMarshalToGeneratorResult() throws Exception{     
50
    }
49
}
51
}
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/rootlevellist/JsonObjectInArrayBuilderTestCases.java (+31 lines)
Lines 13-23 Link Here
13
package org.eclipse.persistence.testing.jaxb.json.rootlevellist;
13
package org.eclipse.persistence.testing.jaxb.json.rootlevellist;
14
14
15
import java.io.StringWriter;
15
import java.io.StringWriter;
16
import java.util.ArrayList;
17
import java.util.List;
16
18
17
import javax.json.Json;
19
import javax.json.Json;
18
import javax.json.JsonArray;
20
import javax.json.JsonArray;
19
import javax.json.JsonArrayBuilder;
21
import javax.json.JsonArrayBuilder;
20
import javax.json.JsonWriter;
22
import javax.json.JsonWriter;
23
import javax.json.stream.JsonGenerator;
21
import javax.xml.bind.JAXBContext;
24
import javax.xml.bind.JAXBContext;
22
import javax.xml.bind.Marshaller;
25
import javax.xml.bind.Marshaller;
23
26
Lines 26-31 import org.eclipse.persistence.jaxb.JAXBMarshaller; Link Here
26
import org.eclipse.persistence.jaxb.MarshallerProperties;
29
import org.eclipse.persistence.jaxb.MarshallerProperties;
27
import org.eclipse.persistence.oxm.MediaType;
30
import org.eclipse.persistence.oxm.MediaType;
28
import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult;
31
import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult;
32
import org.eclipse.persistence.oxm.json.JsonGeneratorResult;
29
import org.eclipse.persistence.testing.oxm.OXTestCase;
33
import org.eclipse.persistence.testing.oxm.OXTestCase;
30
34
31
public class JsonObjectInArrayBuilderTestCases extends OXTestCase {
35
public class JsonObjectInArrayBuilderTestCases extends OXTestCase {
Lines 64-69 public class JsonObjectInArrayBuilderTestCases extends OXTestCase { Link Here
64
        String controlString = "[{\"name\":\"FOO\"},{\"name\":\"FOO2\"}]";
68
        String controlString = "[{\"name\":\"FOO\"},{\"name\":\"FOO2\"}]";
65
        assertEquals(controlString, sw.toString());        
69
        assertEquals(controlString, sw.toString());        
66
    }
70
    }
71
72
    public void testMarshalToGeneratorResult() throws Exception{
73
        JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithoutXmlRootElementRoot.class}, null);
74
        Marshaller jsonMarshaller = ctx.createMarshaller();
75
        jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
76
        jsonMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
77
        
78
        StringWriter sw = new StringWriter();
79
        JsonGenerator jsonGenerator = Json.createGenerator(sw);
80
        JsonGeneratorResult result = new JsonGeneratorResult(jsonGenerator);    
81
        
82
        WithoutXmlRootElementRoot foo = new WithoutXmlRootElementRoot();
83
        foo.setName("FOO");
84
        
85
        WithoutXmlRootElementRoot foo2 = new WithoutXmlRootElementRoot();
86
        foo2.setName("FOO2");
87
        
88
        List<WithoutXmlRootElementRoot> things = new ArrayList<WithoutXmlRootElementRoot>();
89
        things.add(foo);
90
        things.add(foo2);
91
        jsonMarshaller.marshal(things, result);
92
        jsonGenerator.flush();
93
        
94
        log(sw.toString());
95
        String controlString = "[{\"name\":\"FOO\"},{\"name\":\"FOO2\"}]";
96
        assertEquals(controlString, sw.toString());        
97
    }
67
    
98
    
68
    public void testMarshalToArrayBuilderResultWithRoot() throws Exception{
99
    public void testMarshalToArrayBuilderResultWithRoot() throws Exception{
69
        JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithXmlRootElementRoot.class}, null);
100
        JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithXmlRootElementRoot.class}, null);
(-)a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlvariablenode/XmlVariableNodeNullValueTestCases.java (-162 / +7 lines)
Lines 63-229 public class XmlVariableNodeNullValueTestCases extends JAXBWithJSONTestCases{ Link Here
63
		return r;
63
		return r;
64
	}
64
	}
65
65
66
	public void objectToXMLStringWriter(Object objectToWrite) throws Exception {
66
	public void assertMarshalException(Exception exception) throws Exception {
67
		try{
67
	    Throwable nested = exception.getCause();// getLinkedException();
68
     		super.objectToXMLStringWriter(objectToWrite);
68
        assertTrue(nested instanceof XMLMarshalException);
69
		}catch(JAXBException e){
69
        if(((XMLMarshalException)nested).getErrorCode() == XMLMarshalException.NULL_VALUE_NOT_ALLOWED_FOR_VARIABLE){
70
			Throwable nested = e.getLinkedException();
70
            return;
71
			assertTrue(nested instanceof XMLMarshalException);
71
        }    
72
			assertEquals(XMLMarshalException.NULL_VALUE_NOT_ALLOWED_FOR_VARIABLE,((XMLMarshalException)nested).getErrorCode());
72
        fail("Expected exception did not occur.");
73
			return;
74
		}
75
		fail("An XMLMarshalException should have occurred");
76
	}
77
	
78
    public void testJSONMarshalToOutputStream() throws Exception{
79
    	try{
80
     		super.testJSONMarshalToOutputStream();
81
		}catch(JAXBException e){
82
			if(verifyException(e)){
83
		    	return;
84
		    }
85
		}
86
		fail("An XMLMarshalException should have occurred");
87
    }
88
    
89
    public void testJSONMarshalToBuilderResult() throws Exception{
90
        try{
91
            super.testJSONMarshalToBuilderResult();
92
        }catch(JAXBException e){
93
            if(verifyException(e)){
94
                return;
95
            }
96
        }
97
        fail("An XMLMarshalException should have occurred");
98
    }
99
    
100
    public void testJSONMarshalToStringWriter_FORMATTED() throws Exception{
101
    	try{
102
     		super.testJSONMarshalToStringWriter_FORMATTED();
103
		}catch(JAXBException e){
104
			if(verifyException(e)){
105
		    	return;
106
		    }
107
		}
108
		fail("An XMLMarshalException should have occurred");
109
    }
110
    
111
    public void testJSONMarshalToOutputStream_FORMATTED() throws Exception{
112
    	try{
113
     		super.testJSONMarshalToOutputStream_FORMATTED();
114
		}catch(JAXBException e){
115
			if(verifyException(e)){
116
		    	return;
117
		    }
118
		}
119
		fail("An XMLMarshalException should have occurred");
120
    }
121
    
122
    public void testJSONMarshalToStringWriter() throws Exception{
123
    	try{
124
     		super.testJSONMarshalToStringWriter();
125
		}catch(JAXBException e){
126
			if(verifyException(e)){
127
		    	return;
128
		    }
129
		}
130
		fail("An XMLMarshalException should have occurred");
131
    }
132
    
133
    public void testObjectToOutputStream() throws Exception {
134
    	try{
135
     		super.testObjectToOutputStream();
136
		}catch(JAXBException e){
137
			if(verifyException(e)){
138
		    	return;
139
		    }
140
		}
141
		fail("An XMLMarshalException should have occurred");
142
    }
143
    
144
    private boolean verifyException(JAXBException e){
145
    	Throwable nested = e.getLinkedException();
146
		assertTrue(nested instanceof XMLMarshalException);
147
		if(((XMLMarshalException)nested).getErrorCode() == XMLMarshalException.NULL_VALUE_NOT_ALLOWED_FOR_VARIABLE){
148
			return true;
149
		}
150
		if(((XMLMarshalException)nested).getErrorCode() == XMLMarshalException.MARSHAL_EXCEPTION){
151
			Throwable internalException = ((XMLMarshalException)nested).getInternalException(); 
152
			assertTrue(internalException instanceof XMLMarshalException);
153
			if(((XMLMarshalException)internalException).getErrorCode() == XMLMarshalException.NULL_VALUE_NOT_ALLOWED_FOR_VARIABLE){
154
				return true;
155
			}
156
		}
157
		return false;
158
    }
159
    public void testObjectToOutputStreamASCIIEncoding() throws Exception {
160
    	try{
161
     		super.testObjectToOutputStreamASCIIEncoding();
162
		}catch(JAXBException e){			
163
		    if(verifyException(e)){
164
		    	return;
165
		    }
166
		}
167
		fail("An XMLMarshalException should have occurred");
168
   
169
    }
170
    
171
    public void testObjectToXMLDocument() throws Exception {
172
    	try{
173
     		super.testObjectToXMLDocument();
174
		}catch(JAXBException e){
175
			if(verifyException(e)){
176
		    	return;
177
		    }
178
		}
179
		fail("An XMLMarshalException should have occurred");
180
   
181
    }
182
183
    public void testObjectToXMLStreamWriter() throws Exception {
184
    	try{
185
     		super.testObjectToXMLStreamWriter();
186
		}catch(JAXBException e){
187
			if(verifyException(e)){
188
		    	return;
189
		    }
190
		}
191
		fail("An XMLMarshalException should have occurred");
192
   
193
    }
194
    
195
    public void testObjectToXMLStreamWriterRecord() throws Exception {
196
    	try{
197
     		super.testObjectToXMLStreamWriterRecord();
198
		}catch(JAXBException e){
199
			if(verifyException(e)){
200
		    	return;
201
		    }
202
		}
203
		fail("An XMLMarshalException should have occurred");
204
   
205
    }
206
    
207
    public void testObjectToXMLEventWriter() throws Exception {
208
    	try{
209
     		super.testObjectToXMLEventWriter();
210
		}catch(JAXBException e){
211
			if(verifyException(e)){
212
		    	return;
213
		    }
214
		}
215
		fail("An XMLMarshalException should have occurred");
216
   
217
    }
73
    }
218
    public void testObjectToContentHandler() throws Exception {
219
    	try{
220
     		super.testObjectToContentHandler();
221
		}catch(JAXBException e){
222
			if(verifyException(e)){
223
		    	return;
224
		    }
225
		}
226
		fail("An XMLMarshalException should have occurred");
227
   
228
    }    
229
}
74
}

Return to bug 411382