-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
json-smart/src/test/java/net/minidev/json/testMapping/TestUUID.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.minidev.json.testMapping; | ||
|
||
import net.minidev.json.JSONObject; | ||
import net.minidev.json.JSONStyle; | ||
import net.minidev.json.JSONValue; | ||
import net.minidev.json.parser.ParseException; | ||
import net.minidev.json.reader.JsonWriterI; | ||
import net.minidev.json.writer.JsonReaderI; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestUUID { | ||
@Test | ||
void testUUID() throws ParseException { | ||
JSONObject obj = new JSONObject(); | ||
UUID uuid = new UUID(123, 456); | ||
JSONValue.registerWriter(UUID.class, new JsonWriterI<UUID>() { | ||
@Override | ||
public void writeJSONString(UUID value, Appendable out, JSONStyle compression) throws IOException { | ||
out.append(value.toString()); | ||
} | ||
}); | ||
|
||
JSONValue.registerReader(UUIDHolder.class, new JsonReaderI<UUIDHolder>(JSONValue.defaultReader) { | ||
@Override | ||
public void setValue(Object current, String key, Object value) throws ParseException, IOException { | ||
if ("v".equals(key)) { | ||
((UUIDHolder)current).setV(UUID.fromString((String)value)); | ||
return; | ||
} | ||
super.setValue(current, key, value); | ||
} | ||
@Override | ||
public Object createObject() { | ||
return new UUIDHolder(); | ||
} | ||
}); | ||
|
||
obj.put("v", uuid); | ||
String asText = obj.toJSONString(); | ||
assertEquals("{\"v\":00000000-0000-007b-0000-0000000001c8}", asText); | ||
UUIDHolder rebuild = JSONValue.parseWithException(asText, UUIDHolder.class); | ||
assertEquals(uuid, rebuild.getV()); | ||
} | ||
|
||
public static class UUIDHolder { | ||
private UUID v; | ||
|
||
public UUID getV() { | ||
return v; | ||
} | ||
|
||
public void setV(UUID uuid) { | ||
this.v = uuid; | ||
} | ||
} | ||
} |