1
1
package net .minidev .json .testMapping ;
2
2
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+
3
5
import java .io .IOException ;
6
+ import java .time .Instant ;
4
7
5
8
import net .minidev .json .JSONStyle ;
6
9
import net .minidev .json .JSONValue ;
7
10
import net .minidev .json .parser .ParseException ;
11
+ import net .minidev .json .writer .JsonReaderI ;
8
12
9
13
import org .junit .jupiter .api .Test ;
10
14
@@ -22,52 +26,49 @@ public class TestCustomMappingInstant {
22
26
public void test_dummy () throws IOException {
23
27
@ SuppressWarnings ("unused" )
24
28
ParseException e = null ;
25
-
26
29
JSONValue .toJSONString (true , JSONStyle .MAX_COMPRESS );
27
- //Assert.assertEquals(true, true);
28
30
}
29
31
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
+ }
73
74
}
0 commit comments