Skip to content

Commit ec6a113

Browse files
committed
add some test
1 parent b3195a7 commit ec6a113

File tree

4 files changed

+110
-55
lines changed

4 files changed

+110
-55
lines changed

json-smart/src/main/java/net/minidev/json/JSONAwareEx.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author Uriel Chemouni <uchemouni@gmail.com>
2424
*/
2525

26-
public interface JSONAwareEx extends JSONAware{
26+
public interface JSONAwareEx extends JSONAware {
2727
/**
2828
* @return JSON text
2929
*/

json-smart/src/main/java/net/minidev/json/JSONObject.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,13 @@
2828
* @author FangYidong <fangyidong@yahoo.com.cn>
2929
* @author Uriel Chemouni <uchemouni@gmail.com>
3030
*/
31-
public class JSONObject extends HashMap<String, Object> implements JSONAware, JSONAwareEx, JSONStreamAwareEx {
31+
public class JSONObject extends HashMap<String, Object> implements JSONAwareEx, JSONStreamAwareEx {
3232
private static final long serialVersionUID = -503443796854799292L;
3333

3434
public JSONObject() {
3535
super();
3636
}
3737

38-
// /**
39-
// * Allow simply casting to Map<String, XXX>
40-
// */
41-
// @SuppressWarnings("unchecked")
42-
// public <T> T cast() {
43-
// return (T) this;
44-
// }
45-
4638
/**
4739
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters
4840
* (U+0000 through U+001F). It's the same as JSONValue.escape() only for
Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package net.minidev.json.testMapping;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
35
import java.io.IOException;
6+
import java.time.Instant;
47

58
import net.minidev.json.JSONStyle;
69
import net.minidev.json.JSONValue;
710
import net.minidev.json.parser.ParseException;
11+
import net.minidev.json.writer.JsonReaderI;
812

913
import org.junit.jupiter.api.Test;
1014

@@ -22,52 +26,49 @@ public class TestCustomMappingInstant {
2226
public void test_dummy() throws IOException {
2327
@SuppressWarnings("unused")
2428
ParseException e = null;
25-
2629
JSONValue.toJSONString(true, JSONStyle.MAX_COMPRESS);
27-
//Assert.assertEquals(true, true);
2830
}
2931

30-
// Need JDK 1.8
31-
// public void test_instant() {
32-
// JSONValue.registerWriter(java.time.Instant.class, new net.minidev.json.reader.JsonWriterI<java.time.Instant>() {
33-
// @Override
34-
// public void writeJSONString(java.time.Instant value, Appendable out, JSONStyle compression)
35-
// throws IOException {
36-
// if (value == null)
37-
// out.append("null");
38-
// else
39-
// out.append(Long.toString(value.toEpochMilli()));
40-
// }
41-
// });
42-
//
43-
// JSONValue.registerReader(RegularClass.class, new net.minidev.json.writer.JsonReaderI<RegularClass>(JSONValue.defaultReader) {
44-
// @Override
45-
// public void setValue(Object current, String key, Object value) throws ParseException, IOException {
46-
// if (key.equals("instant")) {
47-
// java.time.Instant inst = java.time.Instant.ofEpochMilli((((Number)value).longValue()));
48-
// ((RegularClass)current).setInstant(inst);
49-
// }
50-
// }
51-
// @Override
52-
// public Object createObject() {
53-
// return new RegularClass();
54-
// }
55-
// });
56-
// java.time.Instant instant = java.time.Instant.now();
57-
// RegularClass regularClass = new RegularClass();
58-
// regularClass.setInstant(instant);
59-
// String data = JSONValue.toJSONString(regularClass);
60-
// RegularClass result = JSONValue.parse(data, RegularClass.class);
61-
// Assert.assertEquals(result.getInstant(), instant);
62-
// }
63-
//
64-
// public static class RegularClass {
65-
// private java.time.Instant instant;
66-
// public java.time.Instant getInstant() {
67-
// return instant;
68-
// }
69-
// public void setInstant(java.time.Instant instant) {
70-
// this.instant = instant;
71-
// }
72-
// }
32+
public void test_instant() {
33+
JSONValue.registerWriter(java.time.Instant.class, new net.minidev.json.reader.JsonWriterI<java.time.Instant>() {
34+
@Override
35+
public void writeJSONString(java.time.Instant value, Appendable out, JSONStyle compression)
36+
throws IOException {
37+
if (value == null)
38+
out.append("null");
39+
else
40+
out.append(Long.toString(value.toEpochMilli()));
41+
}
42+
});
43+
44+
JSONValue.registerReader(RegularClass.class, new JsonReaderI<RegularClass>(JSONValue.defaultReader) {
45+
@Override
46+
public void setValue(Object current, String key, Object value) throws ParseException, IOException {
47+
if (key.equals("instant")) {
48+
Instant inst = Instant.ofEpochMilli((((Number)value).longValue()));
49+
((RegularClass)current).setInstant(inst);
50+
}
51+
}
52+
@Override
53+
public Object createObject() {
54+
return new RegularClass();
55+
}
56+
});
57+
Instant instant = Instant.now();
58+
RegularClass regularClass = new RegularClass();
59+
regularClass.setInstant(instant);
60+
String data = JSONValue.toJSONString(regularClass);
61+
RegularClass result = JSONValue.parse(data, RegularClass.class);
62+
assertEquals(result.getInstant(), instant);
63+
}
64+
65+
public static class RegularClass {
66+
private java.time.Instant instant;
67+
public java.time.Instant getInstant() {
68+
return instant;
69+
}
70+
public void setInstant(java.time.Instant instant) {
71+
this.instant = instant;
72+
}
73+
}
7374
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.minidev.json.testMapping;
2+
3+
import net.minidev.json.JSONObject;
4+
import net.minidev.json.JSONStyle;
5+
import net.minidev.json.JSONValue;
6+
import net.minidev.json.parser.ParseException;
7+
import net.minidev.json.reader.JsonWriterI;
8+
import net.minidev.json.writer.JsonReaderI;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
import java.io.IOException;
13+
import java.util.UUID;
14+
15+
import org.junit.jupiter.api.Test;
16+
17+
public class TestUUID {
18+
@Test
19+
void testUUID() throws ParseException {
20+
JSONObject obj = new JSONObject();
21+
UUID uuid = new UUID(123, 456);
22+
JSONValue.registerWriter(UUID.class, new JsonWriterI<UUID>() {
23+
@Override
24+
public void writeJSONString(UUID value, Appendable out, JSONStyle compression) throws IOException {
25+
out.append(value.toString());
26+
}
27+
});
28+
29+
JSONValue.registerReader(UUIDHolder.class, new JsonReaderI<UUIDHolder>(JSONValue.defaultReader) {
30+
@Override
31+
public void setValue(Object current, String key, Object value) throws ParseException, IOException {
32+
if ("v".equals(key)) {
33+
((UUIDHolder)current).setV(UUID.fromString((String)value));
34+
return;
35+
}
36+
super.setValue(current, key, value);
37+
}
38+
@Override
39+
public Object createObject() {
40+
return new UUIDHolder();
41+
}
42+
});
43+
44+
obj.put("v", uuid);
45+
String asText = obj.toJSONString();
46+
assertEquals("{\"v\":00000000-0000-007b-0000-0000000001c8}", asText);
47+
UUIDHolder rebuild = JSONValue.parseWithException(asText, UUIDHolder.class);
48+
assertEquals(uuid, rebuild.getV());
49+
}
50+
51+
public static class UUIDHolder {
52+
private UUID v;
53+
54+
public UUID getV() {
55+
return v;
56+
}
57+
58+
public void setV(UUID uuid) {
59+
this.v = uuid;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)