1

I have a collection where each document contains a UUID stored as Binary. I deserialize it using gson.fromJson(document.toJson(), Class.class);, but I get an exception of type JsonSyntaxException where the UUID is being deserialized. Is there a quick fix for this problem or do I have to change the way I store my UUID (Would it be efficient to store it as String)?

Edit: I changed the UuidRepresentation to STANDARD, and it works serializing it to a type 4, but when Document#toJson is called, it is deserialized back into a type 3.. Is it possible to set the UuidRepresentation for Document#toJson too?

Ferskfisk
  • 25
  • 3
  • Are you sure it's an `UUID`? MongoDB by default uses `ObjectId` – Benjamin M Mar 05 '21 at 23:16
  • Would [UuidRepresentation](http://mongodb.github.io/mongo-java-driver/3.6/javadoc/index.html?org/bson/UuidRepresentation.html) be useful here? – Joe Mar 05 '21 at 23:32
  • @Joe I tried changing from JAVA_LEGACY to STANDARD with no luck, and I know it's a UUID, I set it myself. – Ferskfisk Mar 06 '21 at 00:19
  • I don't think JSON has a UUID representation other than String, does it? – Joe Mar 06 '21 at 00:25
  • @Joe You're right, so I would probably want to find a way to convert from binary to string and then string to UUID perhaps – Ferskfisk Mar 06 '21 at 00:29

0 Answers0