Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 233768 Details for
Bug 411382
JSON-P - Marshal to javax.json.stream.JsonGenerator
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Proposed changes
bug411382_trunk.patch (text/plain), 88.90 KB, created by
Denise Smith
on 2013-07-24 15:37:51 EDT
(
hide
)
Description:
Proposed changes
Filename:
MIME Type:
Creator:
Denise Smith
Created:
2013-07-24 15:37:51 EDT
Size:
88.90 KB
patch
obsolete
> .../persistence/oxm/json/JsonGeneratorResult.java | 34 ++ > .../persistence/oxm/record/JsonBuilderRecord.java | 530 ++------------------- > .../oxm/record/JsonGeneratorRecord.java | 292 ++++++++++++ > .../eclipse/persistence/oxm/record/JsonRecord.java | 500 +++++++++++++++++++ > .../testing/jaxb/JAXBWithJSONTestCases.java | 23 +- > .../ChoiceCollectionMappingTestCases.java | 4 +- > .../testing/jaxb/json/JSONTestCases.java | 14 + > .../jaxb/json/characters/UsAsciiTestCases.java | 6 +- > .../padding/JAXBElementJSONPaddingTestCases.java | 2 + > .../JAXBElementListJSONPaddingTestCases.java | 3 + > .../json/padding/JSONWithNullNameTestCases.java | 3 + > .../json/padding/JSONWithNullObjectTestCases.java | 3 + > .../JSONWithPaddingSimpleListTestCases.java | 3 + > .../padding/JSONWithPaddingSimpleTestCases.java | 3 + > .../json/padding/JSONWithUnsetNameTestCases.java | 2 + > .../JsonObjectInArrayBuilderTestCases.java | 31 ++ > .../XmlVariableNodeNullValueTestCases.java | 169 +------ > 22 files changed, 983 insertions(+), 660 deletions(-) > >diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/Version.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/Version.java >index f3fd0f8..8f458d2 100644 >--- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/Version.java >+++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/Version.java >@@ -29,17 +29,17 @@ public class Version { > // This will be used by all product components and included in exceptions. > private static String product = "Eclipse Persistence Services"; > // A three part version number (major.minor.service) >- private static final String version = "@VERSION@"; >+ private static final String version = "2.6.0"; > // A string that describes this build i.e.( vYYYYMMDD-HHMM, etc.) >- private static final String qualifier = "@QUALIFIER@"; >+ private static final String qualifier = "qualifier"; > // Should be in the format YYYYMMDD >- private static final String buildDate = "@BUILD_DATE@"; >+ private static final String buildDate = "20130724"; > // Should be in the format HHMM >- private static final String buildTime = "@BUILD_TIME@"; >+ private static final String buildTime = "1507"; > // revision of source from the repository >- private static final String buildRevision = "@BUILD_REVISION@"; >+ private static final String buildRevision = "NA"; > // Typically SNAPSHOT, Milestone name (M1,M2,etc), or RELEASE >- private static final String buildType = "@BUILD_TYPE@"; >+ private static final String buildType = "SNAPSHOT"; > > /** Keep track of JDK version in order to make some decisions about data structures. **/ > public static final int JDK_VERSION_NOT_SET = 0; >diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/json/JsonGeneratorResult.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/json/JsonGeneratorResult.java >new file mode 100644 >index 0000000..a4c3e3d >--- /dev/null >+++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/json/JsonGeneratorResult.java >@@ -0,0 +1,34 @@ >+/******************************************************************************* >+ * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. >+ * This program and the accompanying materials are made available under the >+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 >+ * which accompanies this distribution. >+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html >+ * and the Eclipse Distribution License is available at >+ * http://www.eclipse.org/org/documents/edl-v10.php. >+ * >+ * Contributors: >+ * Denise Smith - 2.6 - initial implementation >+ ******************************************************************************/ >+package org.eclipse.persistence.oxm.json; >+ >+import javax.json.stream.JsonGenerator; >+ >+import org.eclipse.persistence.internal.oxm.record.ExtendedResult; >+import org.eclipse.persistence.oxm.record.JsonGeneratorRecord; >+ >+public class JsonGeneratorResult extends ExtendedResult{ >+ >+ private JsonGenerator generator; >+ >+ public JsonGeneratorResult(JsonGenerator generator){ >+ this.generator = generator; >+ } >+ >+ @Override >+ public org.eclipse.persistence.oxm.record.MarshalRecord createRecord() { >+ JsonGeneratorRecord record = new JsonGeneratorRecord(generator); >+ return record; >+ } >+ >+} >diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonBuilderRecord.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonBuilderRecord.java >index 5227b1c..dcbf116 100644 >--- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonBuilderRecord.java >+++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonBuilderRecord.java >@@ -16,7 +16,6 @@ import java.io.IOException; > import java.io.StringWriter; > import java.math.BigDecimal; > import java.math.BigInteger; >-import java.util.List; > > import javax.json.Json; > import javax.json.JsonArrayBuilder; >@@ -25,39 +24,19 @@ import javax.xml.namespace.QName; > > import org.eclipse.persistence.exceptions.XMLMarshalException; > import org.eclipse.persistence.internal.core.helper.CoreClassConstants; >-import org.eclipse.persistence.internal.core.helper.CoreConversionManager; >-import org.eclipse.persistence.internal.oxm.CharacterEscapeHandler; >-import org.eclipse.persistence.internal.oxm.Constants; > import org.eclipse.persistence.internal.oxm.ConversionManager; > import org.eclipse.persistence.internal.oxm.NamespaceResolver; >-import org.eclipse.persistence.internal.oxm.ObjectBuilder; >-import org.eclipse.persistence.internal.oxm.Root; >-import org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper; >-import org.eclipse.persistence.internal.oxm.XMLMarshaller; > import org.eclipse.persistence.internal.oxm.XPathFragment; > import org.eclipse.persistence.internal.oxm.XMLConversionManager; >-import org.eclipse.persistence.internal.oxm.mappings.Descriptor; >-import org.eclipse.persistence.internal.oxm.record.ExtendedContentHandler; >-import org.eclipse.persistence.internal.oxm.record.XMLFragmentReader; >-import org.w3c.dom.Attr; >-import org.w3c.dom.Node; >-import org.xml.sax.Attributes; >-import org.xml.sax.Locator; >-import org.xml.sax.SAXException; >-import org.xml.sax.ext.LexicalHandler; >+import org.eclipse.persistence.oxm.record.JsonRecord.Level; > >-public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { >+public class JsonBuilderRecord extends JsonRecord { > > private Level position; > private JsonObjectBuilder rootJsonObjectBuilder; > private JsonArrayBuilder rootJsonArrayBuilder; >- private CharacterEscapeHandler characterEscapeHandler; >- >- private String attributePrefix; >- private boolean isRootArray; >- private static final String NULL="null"; >- private boolean isLastEventStart; >- >+ >+ > public JsonBuilderRecord(){ > super(); > isLastEventStart = false; >@@ -73,20 +52,7 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > rootJsonArrayBuilder = jsonArrayBuilder; > isRootArray = true; > } >- >- /** >- * INTERNAL: >- */ >- public void setMarshaller(XMLMarshaller marshaller) { >- super.setMarshaller(marshaller); >- attributePrefix = marshaller.getAttributePrefix(); >- if (marshaller.getValueWrapper() != null) { >- textWrapperFragment = new XPathFragment(); >- textWrapperFragment.setLocalName(marshaller.getValueWrapper()); >- } >- characterEscapeHandler = marshaller.getCharacterEscapeHandler(); >- } >- >+ > @Override > public void startDocument(String encoding, String version) { > if(isRootArray){ >@@ -117,15 +83,15 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > popAndSetInParentBuilder(); > }else{ > //this is the root level list case >- position = position.parentLevel; >+ position = (Level) position.parentLevel; > } > } > } > > private void popAndSetInParentBuilder(){ > Level removedLevel = position; >- Level parentLevel = position.parentLevel; >- position = position.parentLevel; >+ Level parentLevel = (Level) position.parentLevel; >+ position = (Level) position.parentLevel; > if(removedLevel.isCollection && removedLevel.isEmptyCollection() && removedLevel.keyName == null){ > return; > } >@@ -159,7 +125,7 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > position = rootLevel; > } else { > if(isLastEventStart){ >- position.setComplex(true); >+ setComplex(position, true); > } > Level level = new Level(true, position); > position = level; >@@ -172,6 +138,16 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > popAndSetInParentBuilder(); > } > >+ private void setComplex(Level level, boolean complex){ >+ boolean isAlreadyComplex = level.isComplex; >+ level.setComplex(complex); >+ if(complex && !isAlreadyComplex){ >+ if(complex && level.jsonObjectBuilder == null){ >+ level.jsonObjectBuilder = Json.createObjectBuilder(); >+ } >+ } >+ } >+ > @Override > public void openStartElement(XPathFragment xPathFragment, NamespaceResolver namespaceResolver) { > super.openStartElement(xPathFragment, namespaceResolver); >@@ -180,7 +156,7 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > > if(isLastEventStart){ > //this means 2 startevents in a row so the last this is a complex object >- position.setComplex(true); >+ setComplex(position, true); > } > > String keyName = getKeyName(xPathFragment); >@@ -194,79 +170,7 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > isLastEventStart = true; > } > } >- >- protected String getKeyName(XPathFragment xPathFragment){ >- String keyName = xPathFragment.getLocalName(); > >- if(isNamespaceAware()){ >- if(xPathFragment.getNamespaceURI() != null){ >- String prefix = null; >- if(getNamespaceResolver() !=null){ >- prefix = getNamespaceResolver().resolveNamespaceURI(xPathFragment.getNamespaceURI()); >- } else if(namespaceResolver != null){ >- prefix = namespaceResolver.resolveNamespaceURI(xPathFragment.getNamespaceURI()); >- } >- if(prefix != null && !prefix.equals(Constants.EMPTY_STRING)){ >- keyName = prefix + getNamespaceSeparator() + keyName; >- } >- } >- } >- if(xPathFragment.isAttribute() && attributePrefix != null){ >- keyName = attributePrefix + keyName; >- } >- >- return keyName; >- } >- >- public void attribute(XPathFragment xPathFragment, NamespaceResolver namespaceResolver, Object value, QName schemaType){ >- if(xPathFragment.getNamespaceURI() != null && xPathFragment.getNamespaceURI() == javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI){ >- return; >- } >- xPathFragment.setAttribute(true); >- openStartElement(xPathFragment, namespaceResolver); >- characters(schemaType, value, null, false, true); >- endElement(xPathFragment, namespaceResolver); >- } >- >- /** >- * INTERNAL: >- */ >- @Override >- public void marshalWithoutRootElement(ObjectBuilder treeObjectBuilder, Object object, Descriptor descriptor, Root root, boolean isXMLRoot){ >- if(treeObjectBuilder != null){ >- addXsiTypeAndClassIndicatorIfRequired(descriptor, null, descriptor.getDefaultRootElementField(), root, object, isXMLRoot, true); >- treeObjectBuilder.marshalAttributes(this, object, session); >- } >- } >- >- /** >- * INTERNAL: >- * The character used to separate the prefix and uri portions when namespaces are present >- * @since 2.4 >- */ >- public char getNamespaceSeparator(){ >- return marshaller.getNamespaceSeparator(); >- } >- >- /** >- * INTERNAL: >- * The optional fragment used to wrap the text() mappings >- * @since 2.4 >- */ >- public XPathFragment getTextWrapperFragment() { >- return textWrapperFragment; >- } >- >- @Override >- public boolean isWrapperAsCollectionName() { >- return marshaller.isWrapperAsCollectionName(); >- } >- >- @Override >- public void element(XPathFragment frag) { >- isLastEventStart = false; >- } >- > /** > * Handle marshal of an empty collection. > * @param xPathFragment >@@ -283,11 +187,11 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > > if(xPathFragment.isSelfFragment() || xPathFragment.nameIsText()){ > String keyName = position.getKeyName(); >- position.setComplex(false); >- position.parentLevel.getJsonObjectBuilder().add(keyName, Json.createArrayBuilder()); >+ setComplex(position, false); >+ ((Level)position.parentLevel).getJsonObjectBuilder().add(keyName, Json.createArrayBuilder()); > }else{ >- if(isLastEventStart){ >- position.setComplex(true); >+ if(isLastEventStart){ >+ setComplex(position, true); > } > String keyName = getKeyName(xPathFragment); > if(keyName != null){ >@@ -304,125 +208,19 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > } > > @Override >- public void attribute(XPathFragment xPathFragment,NamespaceResolver namespaceResolver, String value) { >- attribute(xPathFragment, namespaceResolver, value, null); >- } >- >- @Override >- public void attribute(String namespaceURI, String localName, String qName, String value) { >- XPathFragment xPathFragment = new XPathFragment(); >- xPathFragment.setNamespaceURI(namespaceURI); >- xPathFragment.setAttribute(true); >- xPathFragment.setLocalName(localName); >- >- openStartElement(xPathFragment, namespaceResolver); >- characters(null, value, null, false, true); >- >- endElement(xPathFragment, namespaceResolver); >- >- } >- >- @Override >- public void closeStartElement() {} >- >- @Override > public void endElement(XPathFragment xPathFragment,NamespaceResolver namespaceResolver) { > if(position != null){ > if(isLastEventStart){ >- position.setComplex(true); >+ setComplex(position, true); > } > if(position.isComplex){ > popAndSetInParentBuilder(); > }else{ >- position = position.parentLevel; >+ position = (Level) position.parentLevel; > } > isLastEventStart = false; > } > } >- >- @Override >- public void characters(String value) { >- writeValue(value, null, false); >- } >- >- @Override >- public void characters(QName schemaType, Object value, String mimeType, boolean isCDATA){ >- characters(schemaType, value, mimeType, isCDATA, false); >- } >- >- public void characters(QName schemaType, Object value, String mimeType, boolean isCDATA, boolean isAttribute){ >- if(mimeType != null) { >- if(value instanceof List){ >- value = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesListForBinaryValues((List)value, marshaller, mimeType); >- }else{ >- >- value = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(value, marshaller, mimeType).getData(); >- } >- } >- if(schemaType != null && Constants.QNAME_QNAME.equals(schemaType)){ >- String convertedValue = getStringForQName((QName)value); >- writeValue(convertedValue, null, isAttribute); >- } >- else if(value.getClass() == String.class){ >- //if schemaType is set and it's a numeric or boolean type don't treat as a string >- if(schemaType != null && isNumericOrBooleanType(schemaType)){ >- Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); >- Object convertedValue = ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, theClass, schemaType); >- writeValue(convertedValue, schemaType, isAttribute); >- }else if(isCDATA){ >- cdata((String)value); >- }else{ >- writeValue((String)value, null, isAttribute); >- } >- }else{ >- Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); >- >- if(schemaType == null || theClass == null){ >- if(value.getClass() == CoreClassConstants.BOOLEAN || CoreClassConstants.NUMBER.isAssignableFrom(value.getClass())){ >- writeValue(value, schemaType, isAttribute); >- }else{ >- String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType)); >- writeValue(convertedValue, schemaType, isAttribute); >- } >- }else if(schemaType != null && !isNumericOrBooleanType(schemaType)){ >- //if schemaType exists and is not boolean or number do write quotes (convert to string) >- String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType)); >- writeValue(convertedValue, schemaType, isAttribute); >- } else if(isCDATA){ >- String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType)); >- cdata(convertedValue); >- }else{ >- writeValue(value, schemaType, isAttribute); >- } >- } >- } >- >- >- private boolean isNumericOrBooleanType(QName schemaType){ >- if(schemaType == null){ >- return false; >- }else if(schemaType.equals(Constants.BOOLEAN_QNAME) >- || schemaType.equals(Constants.INTEGER_QNAME) >- || schemaType.equals(Constants.INT_QNAME) >- || schemaType.equals(Constants.BYTE_QNAME) >- || schemaType.equals(Constants.DECIMAL_QNAME) >- || schemaType.equals(Constants.FLOAT_QNAME) >- || schemaType.equals(Constants.DOUBLE_QNAME) >- || schemaType.equals(Constants.SHORT_QNAME) >- || schemaType.equals(Constants.LONG_QNAME) >- || schemaType.equals(Constants.NEGATIVE_INTEGER_QNAME) >- || schemaType.equals(Constants.NON_NEGATIVE_INTEGER_QNAME) >- || schemaType.equals(Constants.NON_POSITIVE_INTEGER_QNAME) >- || schemaType.equals(Constants.POSITIVE_INTEGER_QNAME) >- || schemaType.equals(Constants.UNSIGNED_BYTE_QNAME) >- || schemaType.equals(Constants.UNSIGNED_INT_QNAME) >- || schemaType.equals(Constants.UNSIGNED_LONG_QNAME) >- || schemaType.equals(Constants.UNSIGNED_SHORT_QNAME) >- ){ >- return true; >- } >- return false; >- } > > public void writeValue(Object value, QName schemaType, boolean isAttribute) { > >@@ -445,22 +243,25 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > Level currentLevel = position; > String keyName = position.getKeyName(); > if(!position.isComplex){ >- currentLevel = position.parentLevel; >- currentLevel.setComplex(true); >- } >- if(currentLevel.isCollection()){ >- currentLevel.setEmptyCollection(false); >- addValueToArrayBuilder(currentLevel.getJsonArrayBuilder(), value, schemaType); >- } else { >- JsonObjectBuilder builder = currentLevel.getJsonObjectBuilder(); >- addValueToObjectBuilder(builder, keyName, value, schemaType); >- } >+ currentLevel = (Level) position.parentLevel; >+ } >+ addValue(currentLevel, keyName, value, schemaType); > isLastEventStart = false; > if(textWrapperOpened){ > endElement(textWrapperFragment, namespaceResolver); > } > } > >+ private void addValue(Level currentLevel, String keyName, Object value, QName schemaType){ >+ if(currentLevel.isCollection()){ >+ currentLevel.setEmptyCollection(false); >+ addValueToArrayBuilder(currentLevel.getJsonArrayBuilder(), value, schemaType); >+ } else { >+ JsonObjectBuilder builder = currentLevel.getJsonObjectBuilder(); >+ addValueToObjectBuilder(builder, keyName, value, schemaType); >+ } >+ } >+ > private void addValueToObjectBuilder(JsonObjectBuilder jsonObjectBuilder, String keyName, Object value, QName schemaType){ > if(value == NULL){ > jsonObjectBuilder.addNull(keyName); >@@ -530,250 +331,27 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > } > } > >- @Override >- public void cdata(String value) { >- characters(value); >- } >- >- @Override >- public void node(Node node, NamespaceResolver resolver, String uri, String name) { >- >- if (node.getNodeType() == Node.ATTRIBUTE_NODE) { >- Attr attr = (Attr) node; >- String resolverPfx = null; >- if (getNamespaceResolver() != null) { >- resolverPfx = this.getNamespaceResolver().resolveNamespaceURI(attr.getNamespaceURI()); >- } >- String namespaceURI = attr.getNamespaceURI(); >- // If the namespace resolver contains a prefix for the attribute's URI, >- // use it instead of what is set on the attribute >- if (resolverPfx != null) { >- attribute(attr.getNamespaceURI(), Constants.EMPTY_STRING, resolverPfx+Constants.COLON+attr.getLocalName(), attr.getNodeValue()); >- } else { >- attribute(attr.getNamespaceURI(), Constants.EMPTY_STRING, attr.getName(), attr.getNodeValue()); >- // May need to declare the URI locally >- if (attr.getNamespaceURI() != null) { >- attribute(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, Constants.EMPTY_STRING, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON + attr.getPrefix(), attr.getNamespaceURI()); >- this.getNamespaceResolver().put(attr.getPrefix(), attr.getNamespaceURI()); >- } >- } >- } else if (node.getNodeType() == Node.TEXT_NODE) { >- writeValue(node.getNodeValue(), null, false); >- } else { >- try { >- JsonBuilderRecordContentHandler wrcHandler = new JsonBuilderRecordContentHandler(); >- >- XMLFragmentReader xfragReader = new XMLFragmentReader(namespaceResolver); >- xfragReader.setContentHandler(wrcHandler); >- xfragReader.setProperty("http://xml.org/sax/properties/lexical-handler", wrcHandler); >- xfragReader.parse(node, uri, name); >- } catch (SAXException sex) { >- throw XMLMarshalException.marshalException(sex); >- } >- } >- >- } >- >- protected String getStringForQName(QName qName){ >- if(null == qName) { >- return null; >- } >- CoreConversionManager xmlConversionManager = getSession().getDatasourcePlatform().getConversionManager(); >- >- return (String) xmlConversionManager.convertObject(qName, String.class); >- } >- >- /** >- * INTERNAL: >- */ >- public void namespaceDeclarations(NamespaceResolver namespaceResolver) { >- } >- >- public void namespaceDeclaration(String prefix, String namespaceURI){ >- } >- >- public void defaultNamespaceDeclaration(String defaultNamespace){ >- } >- >- /** >- * INTERNAL: >- */ >- public void nilComplex(XPathFragment xPathFragment, NamespaceResolver namespaceResolver){ >- XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); >- closeStartGroupingElements(groupingFragment); >- openStartElement(xPathFragment, namespaceResolver); >- characters(NULL); >- endElement(xPathFragment, namespaceResolver); >- } >- >- /** >- * INTERNAL: >- */ >- public void nilSimple(NamespaceResolver namespaceResolver){ >- XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); >- characters(NULL); >- closeStartGroupingElements(groupingFragment); >- } >- >- /** >- * Used when an empty simple value should be written >- * @since EclipseLink 2.4 >- */ >- public void emptySimple(NamespaceResolver namespaceResolver){ >- nilSimple(namespaceResolver); >- } >- >- public void emptyAttribute(XPathFragment xPathFragment,NamespaceResolver namespaceResolver){ >- XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); >- openStartElement(xPathFragment, namespaceResolver); >- characters(NULL); >- endElement(xPathFragment, namespaceResolver); >- closeStartGroupingElements(groupingFragment); >- } >- >- /** >- * Used when an empty complex item should be written >- * @since EclipseLink 2.4 >- */ >- public void emptyComplex(XPathFragment xPathFragment, NamespaceResolver namespaceResolver){ >- XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); >- closeStartGroupingElements(groupingFragment); >- openStartElement(xPathFragment, namespaceResolver); >- endElement(xPathFragment, namespaceResolver); >- } >- >- >- >- /** >- * This class will typically be used in conjunction with an XMLFragmentReader. >- * The XMLFragmentReader will walk a given XMLFragment node and report events >- * to this class - the event's data is then written to the enclosing class' >- * writer. >- * >- * @see org.eclipse.persistence.internal.oxm.record.XMLFragmentReader >- */ >- protected class JsonBuilderRecordContentHandler implements ExtendedContentHandler, LexicalHandler { >- >- JsonBuilderRecordContentHandler() { >- } >- >- // --------------------- CONTENTHANDLER METHODS --------------------- // >- public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { >- XPathFragment xPathFragment = new XPathFragment(localName); >- xPathFragment.setNamespaceURI(namespaceURI); >- openStartElement(xPathFragment, namespaceResolver); >- handleAttributes(atts); >- } >- >- public void endElement(String namespaceURI, String localName, String qName) throws SAXException { >- XPathFragment xPathFragment = new XPathFragment(localName); >- xPathFragment.setNamespaceURI(namespaceURI); >- >- JsonBuilderRecord.this.endElement(xPathFragment, namespaceResolver); >- } >- >- public void startPrefixMapping(String prefix, String uri) throws SAXException { >- } >- >- public void characters(char[] ch, int start, int length) throws SAXException { >- String characters = new String (ch, start, length); >- characters(characters); >- } >- >- public void characters(CharSequence characters) throws SAXException { >- JsonBuilderRecord.this.characters(characters.toString()); >- } >- >- // --------------------- LEXICALHANDLER METHODS --------------------- // >- public void comment(char[] ch, int start, int length) throws SAXException { >- } >- >- public void startCDATA() throws SAXException { >- } >- >- public void endCDATA() throws SAXException { >- } >- >- // --------------------- CONVENIENCE METHODS --------------------- // >- protected void handleAttributes(Attributes atts) { >- for (int i=0, attsLength = atts.getLength(); i<attsLength; i++) { >- String qName = atts.getQName(i); >- if((qName != null && (qName.startsWith(javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON) || qName.equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)))) { >- continue; >- } >- attribute(atts.getURI(i), atts.getLocalName(i), qName, atts.getValue(i)); >- } >- } >- >- protected void writeComment(char[] chars, int start, int length) { >- } >- >- protected void writeCharacters(char[] chars, int start, int length) { >- try { >- characters(chars, start, length); >- } catch (SAXException e) { >- throw XMLMarshalException.marshalException(e); >- } >- } >- // --------------- SATISFY CONTENTHANDLER INTERFACE --------------- // >- public void endPrefixMapping(String prefix) throws SAXException {} >- public void processingInstruction(String target, String data) throws SAXException {} >- public void setDocumentLocator(Locator locator) {} >- public void startDocument() throws SAXException {} >- public void endDocument() throws SAXException {} >- public void skippedEntity(String name) throws SAXException {} >- public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {} >- >- // --------------- SATISFY LEXICALHANDLER INTERFACE --------------- // >- public void startEntity(String name) throws SAXException {} >- public void endEntity(String name) throws SAXException {} >- public void startDTD(String name, String publicId, String systemId) throws SAXException {} >- public void endDTD() throws SAXException {} >- @Override >- public void setNil(boolean isNil) {} >- >- } >- >- >+ > /** > * Instances of this class are used to maintain state about the current > * level of the JSON message being marshalled. > */ >- protected static class Level { >+ protected static class Level extends JsonRecord.Level{ > >- private boolean isCollection; >- private boolean emptyCollection; >- private String keyName; > private JsonObjectBuilder jsonObjectBuilder; > private JsonArrayBuilder jsonArrayBuilder; >- private boolean isComplex; >- private Level parentLevel; > > public Level(boolean isCollection, Level parentLevel) { >- setCollection(isCollection); >- emptyCollection = true; >- this.parentLevel = parentLevel; >- } >- >- public boolean isCollection() { >- return isCollection; >+ super(isCollection, parentLevel); > } >- >+ > public void setCollection(boolean isCollection) { >- this.isCollection = isCollection; >+ super.setCollection(isCollection); > if(isCollection && jsonArrayBuilder == null){ > jsonArrayBuilder = Json.createArrayBuilder(); > } > } > >- public String getKeyName() { >- return keyName; >- } >- >- public void setKeyName(String keyName) { >- this.keyName = keyName; >- } >- > public JsonObjectBuilder getJsonObjectBuilder() { > return jsonObjectBuilder; > } >@@ -788,25 +366,7 @@ public class JsonBuilderRecord extends MarshalRecord <XMLMarshaller> { > > public void setJsonArrayBuilder(JsonArrayBuilder jsonArrayBuilder) { > this.jsonArrayBuilder = jsonArrayBuilder; >- } >- >- public boolean isEmptyCollection() { >- return emptyCollection; >- } >- >- public void setEmptyCollection(boolean emptyCollection) { >- this.emptyCollection = emptyCollection; >- } >- public boolean isComplex() { >- return isComplex; >- } >- >- public void setComplex(boolean isComplex) { >- this.isComplex = isComplex; >- if(isComplex && jsonObjectBuilder == null){ >- jsonObjectBuilder = Json.createObjectBuilder(); >- } >- } >+ } > > } > >diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonGeneratorRecord.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonGeneratorRecord.java >new file mode 100644 >index 0000000..3bfecd3 >--- /dev/null >+++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonGeneratorRecord.java >@@ -0,0 +1,292 @@ >+/******************************************************************************* >+ * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. >+ * This program and the accompanying materials are made available under the >+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 >+ * which accompanies this distribution. >+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html >+ * and the Eclipse Distribution License is available at >+ * http://www.eclipse.org/org/documents/edl-v10.php. >+ * >+ * Contributors: >+ * Denise Smith - 2.6 - initial implementation >+ ******************************************************************************/ >+package org.eclipse.persistence.oxm.record; >+ >+import java.io.IOException; >+import java.io.StringWriter; >+import java.math.BigDecimal; >+import java.math.BigInteger; >+ >+import javax.json.stream.JsonGenerator; >+import javax.xml.namespace.QName; >+ >+import org.eclipse.persistence.exceptions.XMLMarshalException; >+import org.eclipse.persistence.internal.core.helper.CoreClassConstants; >+import org.eclipse.persistence.internal.oxm.ConversionManager; >+import org.eclipse.persistence.internal.oxm.NamespaceResolver; >+import org.eclipse.persistence.internal.oxm.XPathFragment; >+import org.eclipse.persistence.internal.oxm.XMLConversionManager; >+ >+public class JsonGeneratorRecord extends JsonRecord { >+ >+ private Level position; >+ private JsonGenerator jsonGenerator; >+ >+ public JsonGeneratorRecord(JsonGenerator generator){ >+ super(); >+ this.jsonGenerator = generator; >+ } >+ >+ @Override >+ public void startDocument(String encoding, String version) { >+ if(isRootArray){ >+ if(position == null){ >+ startCollection(); >+ } >+ position.setEmptyCollection(false); >+ Level newLevel = new Level(false, position); >+ position = newLevel; >+ isLastEventStart = true; >+ }else{ >+ Level rootLevel = new Level(false, null); >+ position = rootLevel; >+ setComplex(position, true); >+ } >+ } >+ >+ @Override >+ public void endDocument() { >+ if(position != null){ >+ jsonGenerator.writeEnd(); >+ position = position.parentLevel; >+ } >+ } >+ >+ private void popAndSetInParentBuilder(){ >+ if(!(position.isCollection && position.isEmptyCollection() && position.getKeyName() == null)){ >+ jsonGenerator.writeEnd(); >+ } >+ position = position.parentLevel; >+ } >+ >+ public void startCollection() { >+ if(position == null){ >+ isRootArray = true; >+ Level rootLevel = new Level(true, null); >+ position = rootLevel; >+ jsonGenerator.writeStartArray(); >+ } else { >+ if(isLastEventStart){ >+ setComplex(position, true); >+ } >+ Level level = new Level(true, position); >+ position = level; >+ } >+ isLastEventStart = false; >+ } >+ >+ @Override >+ public void endCollection() { >+ popAndSetInParentBuilder(); >+ } >+ >+ private void setComplex(Level level, boolean complex){ >+ boolean isAlreadyComplex = level.isComplex; >+ level.setComplex(complex); >+ if(complex && !isAlreadyComplex){ >+ Level parentLevel = level.parentLevel; >+ if((parentLevel != null && parentLevel.isCollection && !parentLevel.isEmptyCollection()) || level.keyName == null){ >+ jsonGenerator.writeStartObject(); >+ }else{ >+ jsonGenerator.writeStartObject(level.keyName); >+ } >+ } >+ } >+ >+ @Override >+ public void openStartElement(XPathFragment xPathFragment, NamespaceResolver namespaceResolver) { >+ super.openStartElement(xPathFragment, namespaceResolver); >+ if(position != null){ >+ Level newLevel = new Level(false, position); >+ >+ if(isLastEventStart){ >+ //this means 2 startevents in a row so the last this is a complex object >+ setComplex(position, true); >+ } >+ >+ String keyName = getKeyName(xPathFragment); >+ >+ if(position.isCollection && position.isEmptyCollection() ){ >+ position.setKeyName(keyName); >+ jsonGenerator.writeStartArray(keyName); >+ position.setEmptyCollection(false); >+ }else{ >+ newLevel.setKeyName(keyName); >+ } >+ position = newLevel; >+ isLastEventStart = true; >+ } >+ } >+ >+ /** >+ * Handle marshal of an empty collection. >+ * @param xPathFragment >+ * @param namespaceResolver >+ * @param openGrouping if grouping elements should be marshalled for empty collections >+ * @return >+ */ >+ public boolean emptyCollection(XPathFragment xPathFragment, NamespaceResolver namespaceResolver, boolean openGrouping) { >+ >+ if(marshaller.isMarshalEmptyCollections()){ >+ super.emptyCollection(xPathFragment, namespaceResolver, true); >+ >+ if (null != xPathFragment) { >+ >+ if(xPathFragment.isSelfFragment() || xPathFragment.nameIsText()){ >+ String keyName = position.getKeyName(); >+ setComplex(position, false); >+ jsonGenerator.writeStartArray(keyName); >+ jsonGenerator.writeEnd(); >+ }else{ >+ if(isLastEventStart){ >+ setComplex(position, true); >+ } >+ String keyName = getKeyName(xPathFragment); >+ if(keyName != null){ >+ jsonGenerator.writeStartArray(keyName); >+ jsonGenerator.writeEnd(); >+ } >+ } >+ isLastEventStart = false; >+ } >+ >+ return true; >+ }else{ >+ return super.emptyCollection(xPathFragment, namespaceResolver, openGrouping); >+ } >+ } >+ @Override >+ public void endElement(XPathFragment xPathFragment,NamespaceResolver namespaceResolver) { >+ if(position != null){ >+ if(isLastEventStart){ >+ setComplex(position, true); >+ } >+ if(position.isComplex){ >+ popAndSetInParentBuilder(); >+ }else{ >+ position = position.parentLevel; >+ } >+ isLastEventStart = false; >+ } >+ } >+ >+ public void writeValue(Object value, QName schemaType, boolean isAttribute) { >+ >+ if (characterEscapeHandler != null && value instanceof String) { >+ try { >+ StringWriter stringWriter = new StringWriter(); >+ characterEscapeHandler.escape(((String)value).toCharArray(), 0, ((String)value).length(), isAttribute, stringWriter); >+ value = stringWriter.toString(); >+ } catch (IOException e) { >+ throw XMLMarshalException.marshalException(e); >+ } >+ } >+ >+ boolean textWrapperOpened = false; >+ if(!isLastEventStart){ >+ openStartElement(textWrapperFragment, namespaceResolver); >+ textWrapperOpened = true; >+ } >+ >+ Level currentLevel = position; >+ String keyName = position.getKeyName(); >+ if(!position.isComplex){ >+ currentLevel = position.parentLevel; >+ } >+ addValue(currentLevel, keyName, value, schemaType); >+ isLastEventStart = false; >+ if(textWrapperOpened){ >+ endElement(textWrapperFragment, namespaceResolver); >+ } >+ } >+ private void addValue(Level currentLevel, String keyName, Object value, QName schemaType){ >+ if(currentLevel.isCollection()){ >+ currentLevel.setEmptyCollection(false); >+ addValueToArray(value, schemaType); >+ } else { >+ addValueToObject(keyName, value, schemaType); >+ >+ } >+ } >+ >+ private void addValueToObject(String keyName, Object value, QName schemaType){ >+ >+ if(value == NULL){ >+ jsonGenerator.writeNull(keyName); >+ }else if(value instanceof Integer){ >+ jsonGenerator.write(keyName, (Integer)value); >+ }else if(value instanceof BigDecimal){ >+ jsonGenerator.write(keyName, (BigDecimal)value); >+ }else if(value instanceof BigInteger){ >+ jsonGenerator.write(keyName, (BigInteger)value); >+ }else if(value instanceof Boolean){ >+ jsonGenerator.write(keyName, (Boolean)value); >+ }else if(value instanceof Character){ >+ jsonGenerator.write(keyName, (Character)value);; >+ }else if(value instanceof Double){ >+ jsonGenerator.write(keyName, (Double)value); >+ }else if(value instanceof Float){ >+ jsonGenerator.write(keyName, (Float)value); >+ }else if(value instanceof Long){ >+ jsonGenerator.write(keyName, (Long)value); >+ }else if(value instanceof String){ >+ jsonGenerator.write(keyName, (String)value); >+ }else{ >+ String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType)); >+ Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); >+ if((schemaType == null || theClass == null) && (CoreClassConstants.NUMBER.isAssignableFrom(value.getClass()))){ >+ //if it's still a number and falls through the cracks we dont want "" around the value >+ BigDecimal convertedNumberValue = ((BigDecimal) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.BIGDECIMAL, schemaType)); >+ jsonGenerator.write(keyName, (BigDecimal)convertedNumberValue); >+ }else{ >+ jsonGenerator.write(keyName, convertedValue); >+ } >+ >+ } >+ } >+ >+ private void addValueToArray(Object value, QName schemaType){ >+ if(value == NULL){ >+ jsonGenerator.writeNull(); >+ }else if(value instanceof Integer){ >+ jsonGenerator.write((Integer)value); >+ }else if(value instanceof BigDecimal){ >+ jsonGenerator.write((BigDecimal)value); >+ }else if(value instanceof BigInteger){ >+ jsonGenerator.write((BigInteger)value); >+ }else if(value instanceof Boolean){ >+ jsonGenerator.write((Boolean)value); >+ }else if(value instanceof Character){ >+ jsonGenerator.write((Character)value); >+ }else if(value instanceof Double){ >+ jsonGenerator.write((Double)value); >+ }else if(value instanceof Float){ >+ jsonGenerator.write((Float)value); >+ }else if(value instanceof Long){ >+ jsonGenerator.write((Long)value); >+ }else if(value instanceof String){ >+ jsonGenerator.write((String)value); >+ }else{ >+ String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType)); >+ Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); >+ if((schemaType == null || theClass == null) && (CoreClassConstants.NUMBER.isAssignableFrom(value.getClass()))){ >+ //if it's still a number and falls through the cracks we dont want "" around the value >+ BigDecimal convertedNumberValue = ((BigDecimal) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.BIGDECIMAL, schemaType)); >+ jsonGenerator.write((BigDecimal)convertedNumberValue); >+ >+ }else{ >+ jsonGenerator.write(convertedValue); >+ } >+ } >+ } >+} >diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonRecord.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonRecord.java >new file mode 100644 >index 0000000..718cccc >--- /dev/null >+++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/oxm/record/JsonRecord.java >@@ -0,0 +1,500 @@ >+/******************************************************************************* >+ * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. >+ * This program and the accompanying materials are made available under the >+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 >+ * which accompanies this distribution. >+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html >+ * and the Eclipse Distribution License is available at >+ * http://www.eclipse.org/org/documents/edl-v10.php. >+ * >+ * Contributors: >+ * Denise Smith - 2.6 - initial implementation >+ ******************************************************************************/ >+package org.eclipse.persistence.oxm.record; >+ >+import java.util.List; >+ >+import javax.xml.namespace.QName; >+ >+import org.eclipse.persistence.exceptions.XMLMarshalException; >+import org.eclipse.persistence.internal.core.helper.CoreClassConstants; >+import org.eclipse.persistence.internal.core.helper.CoreConversionManager; >+import org.eclipse.persistence.internal.oxm.CharacterEscapeHandler; >+import org.eclipse.persistence.internal.oxm.Constants; >+import org.eclipse.persistence.internal.oxm.ConversionManager; >+import org.eclipse.persistence.internal.oxm.NamespaceResolver; >+import org.eclipse.persistence.internal.oxm.ObjectBuilder; >+import org.eclipse.persistence.internal.oxm.Root; >+import org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper; >+import org.eclipse.persistence.internal.oxm.XMLMarshaller; >+import org.eclipse.persistence.internal.oxm.XPathFragment; >+import org.eclipse.persistence.internal.oxm.XMLConversionManager; >+import org.eclipse.persistence.internal.oxm.mappings.Descriptor; >+import org.eclipse.persistence.internal.oxm.record.ExtendedContentHandler; >+import org.eclipse.persistence.internal.oxm.record.XMLFragmentReader; >+import org.w3c.dom.Attr; >+import org.w3c.dom.Node; >+import org.xml.sax.Attributes; >+import org.xml.sax.Locator; >+import org.xml.sax.SAXException; >+import org.xml.sax.ext.LexicalHandler; >+ >+public abstract class JsonRecord extends MarshalRecord <XMLMarshaller> { >+ >+ protected CharacterEscapeHandler characterEscapeHandler; >+ protected String attributePrefix; >+ protected boolean isRootArray; >+ protected static final String NULL="null"; >+ protected boolean isLastEventStart; >+ >+ /** >+ * INTERNAL: >+ */ >+ public void setMarshaller(XMLMarshaller marshaller) { >+ super.setMarshaller(marshaller); >+ attributePrefix = marshaller.getAttributePrefix(); >+ if (marshaller.getValueWrapper() != null) { >+ textWrapperFragment = new XPathFragment(); >+ textWrapperFragment.setLocalName(marshaller.getValueWrapper()); >+ } >+ characterEscapeHandler = marshaller.getCharacterEscapeHandler(); >+ } >+ >+ protected String getKeyName(XPathFragment xPathFragment){ >+ String keyName = xPathFragment.getLocalName(); >+ >+ if(isNamespaceAware()){ >+ if(xPathFragment.getNamespaceURI() != null){ >+ String prefix = null; >+ if(getNamespaceResolver() !=null){ >+ prefix = getNamespaceResolver().resolveNamespaceURI(xPathFragment.getNamespaceURI()); >+ } else if(namespaceResolver != null){ >+ prefix = namespaceResolver.resolveNamespaceURI(xPathFragment.getNamespaceURI()); >+ } >+ if(prefix != null && !prefix.equals(Constants.EMPTY_STRING)){ >+ keyName = prefix + getNamespaceSeparator() + keyName; >+ } >+ } >+ } >+ if(xPathFragment.isAttribute() && attributePrefix != null){ >+ keyName = attributePrefix + keyName; >+ } >+ >+ return keyName; >+ } >+ >+ public void attribute(XPathFragment xPathFragment, NamespaceResolver namespaceResolver, Object value, QName schemaType){ >+ if(xPathFragment.getNamespaceURI() != null && xPathFragment.getNamespaceURI() == javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI){ >+ return; >+ } >+ xPathFragment.setAttribute(true); >+ openStartElement(xPathFragment, namespaceResolver); >+ characters(schemaType, value, null, false, true); >+ endElement(xPathFragment, namespaceResolver); >+ } >+ >+ /** >+ * INTERNAL: >+ */ >+ @Override >+ public void marshalWithoutRootElement(ObjectBuilder treeObjectBuilder, Object object, Descriptor descriptor, Root root, boolean isXMLRoot){ >+ if(treeObjectBuilder != null){ >+ addXsiTypeAndClassIndicatorIfRequired(descriptor, null, descriptor.getDefaultRootElementField(), root, object, isXMLRoot, true); >+ treeObjectBuilder.marshalAttributes(this, object, session); >+ } >+ } >+ >+ /** >+ * INTERNAL: >+ * The character used to separate the prefix and uri portions when namespaces are present >+ * @since 2.4 >+ */ >+ public char getNamespaceSeparator(){ >+ return marshaller.getNamespaceSeparator(); >+ } >+ >+ /** >+ * INTERNAL: >+ * The optional fragment used to wrap the text() mappings >+ * @since 2.4 >+ */ >+ public XPathFragment getTextWrapperFragment() { >+ return textWrapperFragment; >+ } >+ >+ @Override >+ public boolean isWrapperAsCollectionName() { >+ return marshaller.isWrapperAsCollectionName(); >+ } >+ >+ @Override >+ public void element(XPathFragment frag) { >+ isLastEventStart = false; >+ } >+ >+ >+ @Override >+ public void attribute(XPathFragment xPathFragment,NamespaceResolver namespaceResolver, String value) { >+ attribute(xPathFragment, namespaceResolver, value, null); >+ } >+ >+ @Override >+ public void attribute(String namespaceURI, String localName, String qName, String value) { >+ XPathFragment xPathFragment = new XPathFragment(); >+ xPathFragment.setNamespaceURI(namespaceURI); >+ xPathFragment.setAttribute(true); >+ xPathFragment.setLocalName(localName); >+ >+ openStartElement(xPathFragment, namespaceResolver); >+ characters(null, value, null, false, true); >+ >+ endElement(xPathFragment, namespaceResolver); >+ >+ } >+ >+ @Override >+ public void closeStartElement() {} >+ >+ @Override >+ public void characters(String value) { >+ writeValue(value, null, false); >+ } >+ >+ @Override >+ public void characters(QName schemaType, Object value, String mimeType, boolean isCDATA){ >+ characters(schemaType, value, mimeType, isCDATA, false); >+ } >+ >+ public void characters(QName schemaType, Object value, String mimeType, boolean isCDATA, boolean isAttribute){ >+ if(mimeType != null) { >+ if(value instanceof List){ >+ value = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesListForBinaryValues((List)value, marshaller, mimeType); >+ }else{ >+ >+ value = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(value, marshaller, mimeType).getData(); >+ } >+ } >+ if(schemaType != null && Constants.QNAME_QNAME.equals(schemaType)){ >+ String convertedValue = getStringForQName((QName)value); >+ writeValue(convertedValue, null, isAttribute); >+ } >+ else if(value.getClass() == String.class){ >+ //if schemaType is set and it's a numeric or boolean type don't treat as a string >+ if(schemaType != null && isNumericOrBooleanType(schemaType)){ >+ Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); >+ Object convertedValue = ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, theClass, schemaType); >+ writeValue(convertedValue, schemaType, isAttribute); >+ }else if(isCDATA){ >+ cdata((String)value); >+ }else{ >+ writeValue((String)value, null, isAttribute); >+ } >+ }else{ >+ Class theClass = (Class) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).getDefaultXMLTypes().get(schemaType); >+ >+ if(schemaType == null || theClass == null){ >+ if(value.getClass() == CoreClassConstants.BOOLEAN || CoreClassConstants.NUMBER.isAssignableFrom(value.getClass())){ >+ writeValue(value, schemaType, isAttribute); >+ }else{ >+ String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType)); >+ writeValue(convertedValue, schemaType, isAttribute); >+ } >+ }else if(schemaType != null && !isNumericOrBooleanType(schemaType)){ >+ //if schemaType exists and is not boolean or number do write quotes (convert to string) >+ String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType)); >+ writeValue(convertedValue, schemaType, isAttribute); >+ } else if(isCDATA){ >+ String convertedValue = ((String) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.STRING, schemaType)); >+ cdata(convertedValue); >+ }else{ >+ writeValue(value, schemaType, isAttribute); >+ } >+ } >+ } >+ >+ >+ private boolean isNumericOrBooleanType(QName schemaType){ >+ if(schemaType == null){ >+ return false; >+ }else if(schemaType.equals(Constants.BOOLEAN_QNAME) >+ || schemaType.equals(Constants.INTEGER_QNAME) >+ || schemaType.equals(Constants.INT_QNAME) >+ || schemaType.equals(Constants.BYTE_QNAME) >+ || schemaType.equals(Constants.DECIMAL_QNAME) >+ || schemaType.equals(Constants.FLOAT_QNAME) >+ || schemaType.equals(Constants.DOUBLE_QNAME) >+ || schemaType.equals(Constants.SHORT_QNAME) >+ || schemaType.equals(Constants.LONG_QNAME) >+ || schemaType.equals(Constants.NEGATIVE_INTEGER_QNAME) >+ || schemaType.equals(Constants.NON_NEGATIVE_INTEGER_QNAME) >+ || schemaType.equals(Constants.NON_POSITIVE_INTEGER_QNAME) >+ || schemaType.equals(Constants.POSITIVE_INTEGER_QNAME) >+ || schemaType.equals(Constants.UNSIGNED_BYTE_QNAME) >+ || schemaType.equals(Constants.UNSIGNED_INT_QNAME) >+ || schemaType.equals(Constants.UNSIGNED_LONG_QNAME) >+ || schemaType.equals(Constants.UNSIGNED_SHORT_QNAME) >+ ){ >+ return true; >+ } >+ return false; >+ } >+ >+ public abstract void writeValue(Object value, QName schemaType, boolean isAttribute); >+ >+ @Override >+ public void cdata(String value) { >+ characters(value); >+ } >+ >+ @Override >+ public void node(Node node, NamespaceResolver resolver, String uri, String name) { >+ >+ if (node.getNodeType() == Node.ATTRIBUTE_NODE) { >+ Attr attr = (Attr) node; >+ String resolverPfx = null; >+ if (getNamespaceResolver() != null) { >+ resolverPfx = this.getNamespaceResolver().resolveNamespaceURI(attr.getNamespaceURI()); >+ } >+ String namespaceURI = attr.getNamespaceURI(); >+ // If the namespace resolver contains a prefix for the attribute's URI, >+ // use it instead of what is set on the attribute >+ if (resolverPfx != null) { >+ attribute(attr.getNamespaceURI(), Constants.EMPTY_STRING, resolverPfx+Constants.COLON+attr.getLocalName(), attr.getNodeValue()); >+ } else { >+ attribute(attr.getNamespaceURI(), Constants.EMPTY_STRING, attr.getName(), attr.getNodeValue()); >+ // May need to declare the URI locally >+ if (attr.getNamespaceURI() != null) { >+ attribute(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, Constants.EMPTY_STRING, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON + attr.getPrefix(), attr.getNamespaceURI()); >+ this.getNamespaceResolver().put(attr.getPrefix(), attr.getNamespaceURI()); >+ } >+ } >+ } else if (node.getNodeType() == Node.TEXT_NODE) { >+ writeValue(node.getNodeValue(), null, false); >+ } else { >+ try { >+ JsonRecordContentHandler wrcHandler = new JsonRecordContentHandler(); >+ >+ XMLFragmentReader xfragReader = new XMLFragmentReader(namespaceResolver); >+ xfragReader.setContentHandler(wrcHandler); >+ xfragReader.setProperty("http://xml.org/sax/properties/lexical-handler", wrcHandler); >+ xfragReader.parse(node, uri, name); >+ } catch (SAXException sex) { >+ throw XMLMarshalException.marshalException(sex); >+ } >+ } >+ >+ } >+ >+ protected String getStringForQName(QName qName){ >+ if(null == qName) { >+ return null; >+ } >+ CoreConversionManager xmlConversionManager = getSession().getDatasourcePlatform().getConversionManager(); >+ >+ return (String) xmlConversionManager.convertObject(qName, String.class); >+ } >+ >+ /** >+ * INTERNAL: >+ */ >+ public void namespaceDeclarations(NamespaceResolver namespaceResolver) { >+ } >+ >+ public void namespaceDeclaration(String prefix, String namespaceURI){ >+ } >+ >+ public void defaultNamespaceDeclaration(String defaultNamespace){ >+ } >+ >+ /** >+ * INTERNAL: >+ */ >+ public void nilComplex(XPathFragment xPathFragment, NamespaceResolver namespaceResolver){ >+ XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); >+ closeStartGroupingElements(groupingFragment); >+ openStartElement(xPathFragment, namespaceResolver); >+ characters(NULL); >+ endElement(xPathFragment, namespaceResolver); >+ } >+ >+ /** >+ * INTERNAL: >+ */ >+ public void nilSimple(NamespaceResolver namespaceResolver){ >+ XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); >+ characters(NULL); >+ closeStartGroupingElements(groupingFragment); >+ } >+ >+ /** >+ * Used when an empty simple value should be written >+ * @since EclipseLink 2.4 >+ */ >+ public void emptySimple(NamespaceResolver namespaceResolver){ >+ nilSimple(namespaceResolver); >+ } >+ >+ public void emptyAttribute(XPathFragment xPathFragment,NamespaceResolver namespaceResolver){ >+ XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); >+ openStartElement(xPathFragment, namespaceResolver); >+ characters(NULL); >+ endElement(xPathFragment, namespaceResolver); >+ closeStartGroupingElements(groupingFragment); >+ } >+ >+ /** >+ * Used when an empty complex item should be written >+ * @since EclipseLink 2.4 >+ */ >+ public void emptyComplex(XPathFragment xPathFragment, NamespaceResolver namespaceResolver){ >+ XPathFragment groupingFragment = openStartGroupingElements(namespaceResolver); >+ closeStartGroupingElements(groupingFragment); >+ openStartElement(xPathFragment, namespaceResolver); >+ endElement(xPathFragment, namespaceResolver); >+ } >+ >+ >+ >+ /** >+ * This class will typically be used in conjunction with an XMLFragmentReader. >+ * The XMLFragmentReader will walk a given XMLFragment node and report events >+ * to this class - the event's data is then written to the enclosing class' >+ * writer. >+ * >+ * @see org.eclipse.persistence.internal.oxm.record.XMLFragmentReader >+ */ >+ protected class JsonRecordContentHandler implements ExtendedContentHandler, LexicalHandler { >+ >+ JsonRecordContentHandler() { >+ } >+ >+ // --------------------- CONTENTHANDLER METHODS --------------------- // >+ public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { >+ XPathFragment xPathFragment = new XPathFragment(localName); >+ xPathFragment.setNamespaceURI(namespaceURI); >+ openStartElement(xPathFragment, namespaceResolver); >+ handleAttributes(atts); >+ } >+ >+ public void endElement(String namespaceURI, String localName, String qName) throws SAXException { >+ XPathFragment xPathFragment = new XPathFragment(localName); >+ xPathFragment.setNamespaceURI(namespaceURI); >+ >+ JsonRecord.this.endElement(xPathFragment, namespaceResolver); >+ } >+ >+ public void startPrefixMapping(String prefix, String uri) throws SAXException { >+ } >+ >+ public void characters(char[] ch, int start, int length) throws SAXException { >+ String characters = new String (ch, start, length); >+ characters(characters); >+ } >+ >+ public void characters(CharSequence characters) throws SAXException { >+ JsonRecord.this.characters(characters.toString()); >+ } >+ >+ // --------------------- LEXICALHANDLER METHODS --------------------- // >+ public void comment(char[] ch, int start, int length) throws SAXException { >+ } >+ >+ public void startCDATA() throws SAXException { >+ } >+ >+ public void endCDATA() throws SAXException { >+ } >+ >+ // --------------------- CONVENIENCE METHODS --------------------- // >+ protected void handleAttributes(Attributes atts) { >+ for (int i=0, attsLength = atts.getLength(); i<attsLength; i++) { >+ String qName = atts.getQName(i); >+ if((qName != null && (qName.startsWith(javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON) || qName.equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)))) { >+ continue; >+ } >+ attribute(atts.getURI(i), atts.getLocalName(i), qName, atts.getValue(i)); >+ } >+ } >+ >+ protected void writeComment(char[] chars, int start, int length) { >+ } >+ >+ protected void writeCharacters(char[] chars, int start, int length) { >+ try { >+ characters(chars, start, length); >+ } catch (SAXException e) { >+ throw XMLMarshalException.marshalException(e); >+ } >+ } >+ // --------------- SATISFY CONTENTHANDLER INTERFACE --------------- // >+ public void endPrefixMapping(String prefix) throws SAXException {} >+ public void processingInstruction(String target, String data) throws SAXException {} >+ public void setDocumentLocator(Locator locator) {} >+ public void startDocument() throws SAXException {} >+ public void endDocument() throws SAXException {} >+ public void skippedEntity(String name) throws SAXException {} >+ public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {} >+ >+ // --------------- SATISFY LEXICALHANDLER INTERFACE --------------- // >+ public void startEntity(String name) throws SAXException {} >+ public void endEntity(String name) throws SAXException {} >+ public void startDTD(String name, String publicId, String systemId) throws SAXException {} >+ public void endDTD() throws SAXException {} >+ @Override >+ public void setNil(boolean isNil) {} >+ >+ } >+ >+ >+ /** >+ * Instances of this class are used to maintain state about the current >+ * level of the JSON message being marshalled. >+ */ >+ protected static class Level { >+ >+ protected boolean isCollection; >+ protected boolean emptyCollection; >+ protected String keyName; >+ protected boolean isComplex; >+ protected Level parentLevel; >+ >+ public Level(boolean isCollection, Level parentLevel) { >+ setCollection(isCollection); >+ emptyCollection = true; >+ this.parentLevel = parentLevel; >+ } >+ >+ public boolean isCollection() { >+ return isCollection; >+ } >+ >+ public void setCollection(boolean isCollection) { >+ this.isCollection = isCollection; >+ } >+ >+ public String getKeyName() { >+ return keyName; >+ } >+ >+ public void setKeyName(String keyName) { >+ this.keyName = keyName; >+ } >+ >+ public boolean isEmptyCollection() { >+ return emptyCollection; >+ } >+ >+ public void setEmptyCollection(boolean emptyCollection) { >+ this.emptyCollection = emptyCollection; >+ } >+ public boolean isComplex() { >+ return isComplex; >+ } >+ >+ public void setComplex(boolean isComplex) { >+ this.isComplex = isComplex; >+ } >+ >+ } >+ >+} >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/JAXBWithJSONTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/JAXBWithJSONTestCases.java >index 1bf1d26..9fbbbd7 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/JAXBWithJSONTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/JAXBWithJSONTestCases.java >@@ -28,6 +28,7 @@ import javax.json.JsonObjectBuilder; > import javax.json.JsonReader; > import javax.json.JsonStructure; > import javax.json.JsonWriter; >+import javax.json.stream.JsonGenerator; > import javax.json.stream.JsonParser; > import javax.xml.bind.JAXBElement; > import javax.xml.bind.Marshaller; >@@ -41,6 +42,7 @@ import org.eclipse.persistence.jaxb.JAXBContext; > import org.eclipse.persistence.jaxb.MarshallerProperties; > import org.eclipse.persistence.jaxb.UnmarshallerProperties; > import org.eclipse.persistence.oxm.MediaType; >+import org.eclipse.persistence.oxm.json.JsonGeneratorResult; > import org.eclipse.persistence.oxm.json.JsonObjectBuilderResult; > import org.eclipse.persistence.oxm.json.JsonStructureSource; > import org.eclipse.persistence.testing.jaxb.JAXBTestCases.MyStreamSchemaOutputResolver; >@@ -314,6 +316,7 @@ public abstract class JAXBWithJSONTestCases extends JAXBTestCases { > log(sw.toString()); > compareStringToControlFile("testJSONMarshalToStringWriter_FORMATTED", sw.toString(), getWriteControlJSONFormatted(),shouldRemoveWhitespaceFromControlDocJSON()); > } >+ > public void testJSONMarshalToBuilderResult() throws Exception{ > getJSONMarshaller().setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); > >@@ -333,9 +336,27 @@ public abstract class JAXBWithJSONTestCases extends JAXBTestCases { > writer.close(); > > log(sw.toString()); >- compareStringToControlFile("**testJSONMarshalToStringWriter**", sw.toString()); >+ compareStringToControlFile("**testJSONMarshalToBuilderResult**", sw.toString()); > } > >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ getJSONMarshaller().setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); >+ >+ StringWriter sw = new StringWriter(); >+ JsonGenerator generator= Json.createGenerator(sw); >+ JsonGeneratorResult result = new JsonGeneratorResult(generator); >+ try{ >+ getJSONMarshaller().marshal(getWriteControlObject(), result); >+ } catch(Exception e) { >+ assertMarshalException(e); >+ return; >+ } >+ generator.flush(); >+ log(sw.toString()); >+ compareStringToControlFile("**testJSONMarshalToGeneratorResult**", sw.toString()); >+ } >+ >+ > protected void compareStringToControlFile(String test, String testString) { > compareStringToControlFile(test, testString, getWriteControlJSON()); > } >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/externalizedmetadata/mappings/choicecollection/ChoiceCollectionMappingTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/externalizedmetadata/mappings/choicecollection/ChoiceCollectionMappingTestCases.java >index 92f8b90..349d8e3 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/externalizedmetadata/mappings/choicecollection/ChoiceCollectionMappingTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/externalizedmetadata/mappings/choicecollection/ChoiceCollectionMappingTestCases.java >@@ -148,5 +148,7 @@ public class ChoiceCollectionMappingTestCases extends JAXBWithJSONTestCases { > public void testJSONMarshalToBuilderResult() throws Exception{ > //Currently not supported, 101.1 will get written as 101.0999984741211 > } >- >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ //Currently not supported, 101.1 will get written as 101.0999984741211 >+ } > } >\ No newline at end of file >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/JSONTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/JSONTestCases.java >index c4bc79e..718443d 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/JSONTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/JSONTestCases.java >@@ -28,6 +28,7 @@ import javax.json.JsonObject; > import javax.json.JsonObjectBuilder; > import javax.json.JsonWriter; > import javax.json.JsonWriterFactory; >+import javax.json.stream.JsonGenerator; > import javax.xml.bind.JAXBContext; > import javax.xml.bind.JAXBElement; > import javax.xml.bind.Marshaller; >@@ -38,6 +39,7 @@ import org.eclipse.persistence.jaxb.MarshallerProperties; > import org.eclipse.persistence.jaxb.UnmarshallerProperties; > import org.eclipse.persistence.oxm.XMLContext; > import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult; >+import org.eclipse.persistence.oxm.json.JsonGeneratorResult; > import org.eclipse.persistence.oxm.json.JsonObjectBuilderResult; > import org.eclipse.persistence.testing.oxm.OXTestCase; > >@@ -225,6 +227,18 @@ public abstract class JSONTestCases extends OXTestCase{ > log(sw.toString()); > compareStringToControlFile("**testJSONMarshalToBuilderResult**", sw.toString()); > } >+ >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ >+ StringWriter sw = new StringWriter(); >+ JsonGenerator jsonGenerator = Json.createGenerator(sw); >+ JsonGeneratorResult result = new JsonGeneratorResult(jsonGenerator); >+ jsonMarshaller.marshal(getWriteControlObject(), result); >+ jsonGenerator.flush(); >+ >+ log(sw.toString()); >+ compareStringToControlFile("**testJSONMarshalToBuilderResult**", sw.toString()); >+ } > > public void testJSONMarshalToOutputStream_FORMATTED() throws Exception{ > jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/characters/UsAsciiTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/characters/UsAsciiTestCases.java >index 079608b..d9a0c9c 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/characters/UsAsciiTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/characters/UsAsciiTestCases.java >@@ -52,8 +52,12 @@ public class UsAsciiTestCases extends JSONMarshalUnmarshalTestCases { > return control; > } > >- public void marshalToObjectBuilderResult() throws Exception{ >+ public void testJSONMarshalToBuilderResult() throws Exception{ > //Currently not supported > } > >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ //Currently not supported >+ >+ } > } >\ No newline at end of file >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementJSONPaddingTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementJSONPaddingTestCases.java >index aa1c544..3d10a8b 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementJSONPaddingTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementJSONPaddingTestCases.java >@@ -49,4 +49,6 @@ public class JAXBElementJSONPaddingTestCases extends JAXBWithJSONTestCases{ > public void testJSONMarshalToBuilderResult() throws Exception{ > } > >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ } > } >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementListJSONPaddingTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementListJSONPaddingTestCases.java >index 2ce04f6..97bc42f 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementListJSONPaddingTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JAXBElementListJSONPaddingTestCases.java >@@ -60,4 +60,7 @@ public class JAXBElementListJSONPaddingTestCases extends JSONTestCases{ > > public void testJSONMarshalToBuilderResult() throws Exception{ > } >+ >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ } > } >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullNameTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullNameTestCases.java >index 2ab553b..af283eb 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullNameTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullNameTestCases.java >@@ -45,5 +45,8 @@ public class JSONWithNullNameTestCases extends JAXBWithJSONTestCases{ > > public void testJSONMarshalToBuilderResult() throws Exception{ > } >+ >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ } > > } >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullObjectTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullObjectTestCases.java >index 6bc77c6..db92940 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullObjectTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithNullObjectTestCases.java >@@ -49,6 +49,9 @@ public class JSONWithNullObjectTestCases extends JAXBWithJSONTestCases{ > public void testJSONMarshalToBuilderResult() throws Exception{ > } > >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ } >+ > @Override > public void assertMarshalException(Exception exception) throws Exception { > Exception nestedException = (Exception) exception.getCause(); >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleListTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleListTestCases.java >index 6a485ec..a40943b 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleListTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleListTestCases.java >@@ -58,6 +58,9 @@ public class JSONWithPaddingSimpleListTestCases extends JSONTestCases{ > public void testJSONMarshalToBuilderResult() throws Exception{ > } > >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ } >+ > // public boolean shouldRemoveWhitespaceFromControlDocJSON(){ > // return false; > //} >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleTestCases.java >index 3f9ae6a..165040f 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithPaddingSimpleTestCases.java >@@ -51,5 +51,8 @@ public class JSONWithPaddingSimpleTestCases extends JAXBWithJSONTestCases{ > > public void testJSONMarshalToBuilderResult() throws Exception{ > } >+ >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ } > > } >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithUnsetNameTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithUnsetNameTestCases.java >index 26c0e38..c586a23 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithUnsetNameTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/padding/JSONWithUnsetNameTestCases.java >@@ -46,4 +46,6 @@ public class JSONWithUnsetNameTestCases extends JAXBWithJSONTestCases{ > public void testJSONMarshalToBuilderResult() throws Exception{ > } > >+ public void testJSONMarshalToGeneratorResult() throws Exception{ >+ } > } >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/rootlevellist/JsonObjectInArrayBuilderTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/rootlevellist/JsonObjectInArrayBuilderTestCases.java >index 7072e45..7b95816 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/rootlevellist/JsonObjectInArrayBuilderTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/json/rootlevellist/JsonObjectInArrayBuilderTestCases.java >@@ -13,11 +13,14 @@ > package org.eclipse.persistence.testing.jaxb.json.rootlevellist; > > import java.io.StringWriter; >+import java.util.ArrayList; >+import java.util.List; > > import javax.json.Json; > import javax.json.JsonArray; > import javax.json.JsonArrayBuilder; > import javax.json.JsonWriter; >+import javax.json.stream.JsonGenerator; > import javax.xml.bind.JAXBContext; > import javax.xml.bind.Marshaller; > >@@ -26,6 +29,7 @@ import org.eclipse.persistence.jaxb.JAXBMarshaller; > import org.eclipse.persistence.jaxb.MarshallerProperties; > import org.eclipse.persistence.oxm.MediaType; > import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult; >+import org.eclipse.persistence.oxm.json.JsonGeneratorResult; > import org.eclipse.persistence.testing.oxm.OXTestCase; > > public class JsonObjectInArrayBuilderTestCases extends OXTestCase { >@@ -64,6 +68,33 @@ public class JsonObjectInArrayBuilderTestCases extends OXTestCase { > String controlString = "[{\"name\":\"FOO\"},{\"name\":\"FOO2\"}]"; > assertEquals(controlString, sw.toString()); > } >+ >+ public void testMarshalToGeneratorResult() throws Exception{ >+ JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithoutXmlRootElementRoot.class}, null); >+ Marshaller jsonMarshaller = ctx.createMarshaller(); >+ jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); >+ jsonMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false); >+ >+ StringWriter sw = new StringWriter(); >+ JsonGenerator jsonGenerator = Json.createGenerator(sw); >+ JsonGeneratorResult result = new JsonGeneratorResult(jsonGenerator); >+ >+ WithoutXmlRootElementRoot foo = new WithoutXmlRootElementRoot(); >+ foo.setName("FOO"); >+ >+ WithoutXmlRootElementRoot foo2 = new WithoutXmlRootElementRoot(); >+ foo2.setName("FOO2"); >+ >+ List<WithoutXmlRootElementRoot> things = new ArrayList<WithoutXmlRootElementRoot>(); >+ things.add(foo); >+ things.add(foo2); >+ jsonMarshaller.marshal(things, result); >+ jsonGenerator.flush(); >+ >+ log(sw.toString()); >+ String controlString = "[{\"name\":\"FOO\"},{\"name\":\"FOO2\"}]"; >+ assertEquals(controlString, sw.toString()); >+ } > > public void testMarshalToArrayBuilderResultWithRoot() throws Exception{ > JAXBContext ctx = JAXBContextFactory.createContext(new Class[]{WithXmlRootElementRoot.class}, null); >diff --git a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlvariablenode/XmlVariableNodeNullValueTestCases.java b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlvariablenode/XmlVariableNodeNullValueTestCases.java >index 4301659..bdb4ffe 100644 >--- a/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlvariablenode/XmlVariableNodeNullValueTestCases.java >+++ b/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/xmlvariablenode/XmlVariableNodeNullValueTestCases.java >@@ -63,167 +63,12 @@ public class XmlVariableNodeNullValueTestCases extends JAXBWithJSONTestCases{ > return r; > } > >- public void objectToXMLStringWriter(Object objectToWrite) throws Exception { >- try{ >- super.objectToXMLStringWriter(objectToWrite); >- }catch(JAXBException e){ >- Throwable nested = e.getLinkedException(); >- assertTrue(nested instanceof XMLMarshalException); >- assertEquals(XMLMarshalException.NULL_VALUE_NOT_ALLOWED_FOR_VARIABLE,((XMLMarshalException)nested).getErrorCode()); >- return; >- } >- fail("An XMLMarshalException should have occurred"); >- } >- >- public void testJSONMarshalToOutputStream() throws Exception{ >- try{ >- super.testJSONMarshalToOutputStream(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- } >- >- public void testJSONMarshalToBuilderResult() throws Exception{ >- try{ >- super.testJSONMarshalToBuilderResult(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- } >- >- public void testJSONMarshalToStringWriter_FORMATTED() throws Exception{ >- try{ >- super.testJSONMarshalToStringWriter_FORMATTED(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- } >- >- public void testJSONMarshalToOutputStream_FORMATTED() throws Exception{ >- try{ >- super.testJSONMarshalToOutputStream_FORMATTED(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- } >- >- public void testJSONMarshalToStringWriter() throws Exception{ >- try{ >- super.testJSONMarshalToStringWriter(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- } >- >- public void testObjectToOutputStream() throws Exception { >- try{ >- super.testObjectToOutputStream(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- } >- >- private boolean verifyException(JAXBException e){ >- Throwable nested = e.getLinkedException(); >- assertTrue(nested instanceof XMLMarshalException); >- if(((XMLMarshalException)nested).getErrorCode() == XMLMarshalException.NULL_VALUE_NOT_ALLOWED_FOR_VARIABLE){ >- return true; >- } >- if(((XMLMarshalException)nested).getErrorCode() == XMLMarshalException.MARSHAL_EXCEPTION){ >- Throwable internalException = ((XMLMarshalException)nested).getInternalException(); >- assertTrue(internalException instanceof XMLMarshalException); >- if(((XMLMarshalException)internalException).getErrorCode() == XMLMarshalException.NULL_VALUE_NOT_ALLOWED_FOR_VARIABLE){ >- return true; >- } >- } >- return false; >- } >- public void testObjectToOutputStreamASCIIEncoding() throws Exception { >- try{ >- super.testObjectToOutputStreamASCIIEncoding(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- >- } >- >- public void testObjectToXMLDocument() throws Exception { >- try{ >- super.testObjectToXMLDocument(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- >- } >- >- public void testObjectToXMLStreamWriter() throws Exception { >- try{ >- super.testObjectToXMLStreamWriter(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- >- } >- >- public void testObjectToXMLStreamWriterRecord() throws Exception { >- try{ >- super.testObjectToXMLStreamWriterRecord(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- >- } >- >- public void testObjectToXMLEventWriter() throws Exception { >- try{ >- super.testObjectToXMLEventWriter(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- >+ public void assertMarshalException(Exception exception) throws Exception { >+ Throwable nested = exception.getCause();// getLinkedException(); >+ assertTrue(nested instanceof XMLMarshalException); >+ if(((XMLMarshalException)nested).getErrorCode() == XMLMarshalException.NULL_VALUE_NOT_ALLOWED_FOR_VARIABLE){ >+ return; >+ } >+ fail("Expected exception did not occur."); > } >- public void testObjectToContentHandler() throws Exception { >- try{ >- super.testObjectToContentHandler(); >- }catch(JAXBException e){ >- if(verifyException(e)){ >- return; >- } >- } >- fail("An XMLMarshalException should have occurred"); >- >- } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 411382
:
233765
| 233768 |
233805
|
233848