toml4j icon indicating copy to clipboard operation
toml4j copied to clipboard

Improper UUID resolution

Open Sxtanna opened this issue 8 years ago • 0 comments

When a writer serializes a UUID, it does not serialize it as a String, it gathers the fields mostSigBits and leastSigBits.

[identity]
  mostSigBits = -7519793959754578783
  leastSigBits = -5026103932034173063

This is in conflict with Gson's handling of UUID where it simply treats it as a String

UUID = new TypeAdapter<UUID>() {                                      
    public UUID read(JsonReader in) throws IOException {              
        if (in.peek() == JsonToken.NULL) {                            
            in.nextNull();                                            
            return null;                                              
        } else {                                                      
            return java.util.UUID.fromString(in.nextString());        
        }                                                             
    }                                                                 
                                                                      
    public void write(JsonWriter out, UUID value) throws IOException {
        out.value(value == null ? null : value.toString());           
    }                                                                 
};                                                                    

This causes the deserialization of a UUID to fail with: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT at path $.identity

A simple solution to this would be to add UUID to the writable objects checked in StringValueReaderWriter#canWrite(Object)

@Override                                                                                                                               
public boolean canWrite(Object value) {                                                                                                 
  return value instanceof String || value instanceof Character || value instanceof URL || value instanceof URI || value instanceof Enum;
}                                                                                                                                       

Sxtanna avatar Feb 11 '18 14:02 Sxtanna