Menu

#143 Support java.util.UUID

closed
None
5
2021-03-16
2020-12-22
No

Support for UUID type without custom converter that uses java.util.UUID.fromString and toString to read/write values.

Current result: com.opencsv.exceptions.CsvDataTypeMismatchException: Conversion of 7F804F85-0064-4E96-8260-3FD47EA6A8BB to java.util.UUID failed

Expected result: successful parsing of valid UUID

Related

Feature Requests: #143

Discussion

  • Scott Conway

    Scott Conway - 2020-12-23
    • assigned_to: Scott Conway
     
  • Scott Conway

    Scott Conway - 2020-12-23

    If you have written a custom editor could you please send the code for your custom converter because quite honestly if we create our own UUID annotation it would probably work the same way.

    That and I wrote the following test using your value just to see if the string may be an incorrect format and it passed. I had to do an equalsIgnoreCase because the toString method makes it lowercase.

    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.Test;
    
    import java.util.UUID;
    
    import static org.junit.jupiter.api.Assertions.assertTrue;
    
    public class UUIDTest {
        public static String TEST_ID_STRING = "7F804F85-0064-4E96-8260-3FD47EA6A8BB";
    
        @DisplayName("Convert String to UUID and back to String")
        @Test
        public void convert() {
            UUID uuid = UUID.fromString(TEST_ID_STRING);
            assertTrue(TEST_ID_STRING.equalsIgnoreCase(uuid.toString()));
        }
    }
    

    What I am wondering is if the string you are passing in has whitespace at the beginning or end which causes an NumberFormatException to be thrown which then would be converted to the exception you are seeing. Try adding a trim() to the string before you pass it into the fromString().

    That said if you did not then just let me know because I will look at adding an converter for UUID.

     

    Last edit: Scott Conway 2020-12-23
    • Andrew Rucker Jones

      … I think you might be missing something, Scott. This conversion is not supported, and the exception thrown is expected. The converter would be just as easy to write as Elliot suggests: it would probably be ConverterUUID alongside ConverterPrimitive and the rest. ConverterEnum would be the closest to the required level of complexity. It would not require a new annotation.

       
      👍
      1
      • Scott Conway

        Scott Conway - 2020-12-23

        Yeah I sort of thought that as I was ending up the email.

        I had already decided to create a converter along the lines of your suggestion. Will definitely look at ConverterEnum, I was looking at ConverterCurrency.

         
  • Elliot Korte

    Elliot Korte - 2020-12-23

    My custom converter works fine, UUID seems common enough that Open CSV can support it without the need for an annotation. Taking a quick look at the source code (AbstractMappingStrategy.determineConverter) it looks like Currency and Enums are supported without annotations. Perhaps we can add similar functionality for UUID?

    Here is my custom converter

    import com.opencsv.bean.AbstractBeanField;
    import java.util.UUID;
    
    public class UUIDCsvConverter extends AbstractBeanField<UUID, String> {
    
        @Override
        protected UUID convert(String value) {
            return value == null ? null : UUID.fromString(value);
        }
    }
    
     
  • Scott Conway

    Scott Conway - 2020-12-27
    • status: open --> pending
     
    • Elliot Korte

      Elliot Korte - 2020-12-28

      Great! Thank you

      On Sun, Dec 27, 2020 at 5:50 PM Scott Conway sconway@users.sourceforge.net
      wrote:

      • status: open --> pending
      • Comment:

      Code has been merged in and will go out with the 5.4 release.

      Status: pending
      Group: Next Release (example)
      Created: Tue Dec 22, 2020 09:07 PM UTC by Elliot Korte
      Last Updated: Wed Dec 23, 2020 11:19 PM UTC
      Owner: Scott Conway

      Support for UUID type without custom converter that uses
      java.util.UUID.fromString and toString to read/write values.

      Current result: com.opencsv.exceptions.CsvDataTypeMismatchException:
      Conversion of 7F804F85-0064-4E96-8260-3FD47EA6A8BB to java.util.UUID failed

      Expected result: successful parsing of valid UUID

      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/opencsv/feature-requests/143/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       

      Related

      Feature Requests: #143

  • Scott Conway

    Scott Conway - 2020-12-27

    Code has been merged in and will go out with the 5.4 release.

     
  • Scott Conway

    Scott Conway - 2021-03-16
    • status: pending --> closed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.