none
Elegantly dealing with decimal types that need to be deserialized from exponent notation.

    Question

  • WSDL contracts specify a decimal datatype, and my svcutil.exe proxy generator generates decimal properties that are backed by decimal fields.  The problem is, decimals by default cannot parse strings from XML like "6E-7".

    The Decimal class itself supports an overload of Parse  that allows the user to specify the format of the source string. e.g. NumberStyles.Any or NumberStyles.AllowExponents.

    Is there a way I can override how WCF deserializes specific class members, or decimals in general?

    Thanks.

    Tuesday, May 08, 2012 11:27 PM

Answers

  • You could use XmlSerializer.CanDeserialize method to check whether it can deserialize that xml document contains exponent notation, if it cannot be deserialized, then you need to loop through the xml document and replace the string like "6E-7" to the correct decimal format.

    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    Thursday, May 10, 2012 2:48 AM
    Moderator
  • Hi Peter,

    We solved the issue a different way, but our "solution" was flaky.

    Seems like that should work, but where do I put that in my code?

    I have researched this issue further, and the root cause appears to be that the service I am consuming is running Java 6.  In Java 5, somebody at Sun decided to break backwards compatibility with the BigDecimal.ToString() method and return exponent notation instead for values with many decimal places.  Java frameworks like Castor, Axis and JiXB have received patches to handle this scenario, calling ToPlainString() instead:

    • https://issues.apache.org/jira/browse/AXIS-2639
    • https://issues.apache.org/jira/browse/AXIS2-4190
    • http://jira.codehaus.org/browse/CASTOR-2488
    • http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6846140
    • http://jira.codehaus.org/browse/JIBX-293

    

    Thursday, June 28, 2012 5:58 PM

All replies

  • You could use XmlSerializer.CanDeserialize method to check whether it can deserialize that xml document contains exponent notation, if it cannot be deserialized, then you need to loop through the xml document and replace the string like "6E-7" to the correct decimal format.

    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    Thursday, May 10, 2012 2:48 AM
    Moderator
  • Hi Peter,

    We solved the issue a different way, but our "solution" was flaky.

    Seems like that should work, but where do I put that in my code?

    I have researched this issue further, and the root cause appears to be that the service I am consuming is running Java 6.  In Java 5, somebody at Sun decided to break backwards compatibility with the BigDecimal.ToString() method and return exponent notation instead for values with many decimal places.  Java frameworks like Castor, Axis and JiXB have received patches to handle this scenario, calling ToPlainString() instead:

    • https://issues.apache.org/jira/browse/AXIS-2639
    • https://issues.apache.org/jira/browse/AXIS2-4190
    • http://jira.codehaus.org/browse/CASTOR-2488
    • http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6846140
    • http://jira.codehaus.org/browse/JIBX-293

    

    Thursday, June 28, 2012 5:58 PM