Skip to content

Commit

Permalink
add some test
Browse files Browse the repository at this point in the history
  • Loading branch information
UrielCh committed Apr 4, 2021
1 parent b3195a7 commit ec6a113
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 55 deletions.
2 changes: 1 addition & 1 deletion json-smart/src/main/java/net/minidev/json/JSONAwareEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Uriel Chemouni <uchemouni@gmail.com>
*/

public interface JSONAwareEx extends JSONAware{
public interface JSONAwareEx extends JSONAware {
/**
* @return JSON text
*/
Expand Down
10 changes: 1 addition & 9 deletions json-smart/src/main/java/net/minidev/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,13 @@
* @author FangYidong <fangyidong@yahoo.com.cn>
* @author Uriel Chemouni <uchemouni@gmail.com>
*/
public class JSONObject extends HashMap<String, Object> implements JSONAware, JSONAwareEx, JSONStreamAwareEx {
public class JSONObject extends HashMap<String, Object> implements JSONAwareEx, JSONStreamAwareEx {
private static final long serialVersionUID = -503443796854799292L;

public JSONObject() {
super();
}

// /**
// * Allow simply casting to Map<String, XXX>
// */
// @SuppressWarnings("unchecked")
// public <T> T cast() {
// return (T) this;
// }

/**
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters
* (U+0000 through U+001F). It's the same as JSONValue.escape() only for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package net.minidev.json.testMapping;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.time.Instant;

import net.minidev.json.JSONStyle;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.ParseException;
import net.minidev.json.writer.JsonReaderI;

import org.junit.jupiter.api.Test;

Expand All @@ -22,52 +26,49 @@ public class TestCustomMappingInstant {
public void test_dummy() throws IOException {
@SuppressWarnings("unused")
ParseException e = null;

JSONValue.toJSONString(true, JSONStyle.MAX_COMPRESS);
//Assert.assertEquals(true, true);
}

// Need JDK 1.8
// public void test_instant() {
// JSONValue.registerWriter(java.time.Instant.class, new net.minidev.json.reader.JsonWriterI<java.time.Instant>() {
// @Override
// public void writeJSONString(java.time.Instant value, Appendable out, JSONStyle compression)
// throws IOException {
// if (value == null)
// out.append("null");
// else
// out.append(Long.toString(value.toEpochMilli()));
// }
// });
//
// JSONValue.registerReader(RegularClass.class, new net.minidev.json.writer.JsonReaderI<RegularClass>(JSONValue.defaultReader) {
// @Override
// public void setValue(Object current, String key, Object value) throws ParseException, IOException {
// if (key.equals("instant")) {
// java.time.Instant inst = java.time.Instant.ofEpochMilli((((Number)value).longValue()));
// ((RegularClass)current).setInstant(inst);
// }
// }
// @Override
// public Object createObject() {
// return new RegularClass();
// }
// });
// java.time.Instant instant = java.time.Instant.now();
// RegularClass regularClass = new RegularClass();
// regularClass.setInstant(instant);
// String data = JSONValue.toJSONString(regularClass);
// RegularClass result = JSONValue.parse(data, RegularClass.class);
// Assert.assertEquals(result.getInstant(), instant);
// }
//
// public static class RegularClass {
// private java.time.Instant instant;
// public java.time.Instant getInstant() {
// return instant;
// }
// public void setInstant(java.time.Instant instant) {
// this.instant = instant;
// }
// }
public void test_instant() {
JSONValue.registerWriter(java.time.Instant.class, new net.minidev.json.reader.JsonWriterI<java.time.Instant>() {
@Override
public void writeJSONString(java.time.Instant value, Appendable out, JSONStyle compression)
throws IOException {
if (value == null)
out.append("null");
else
out.append(Long.toString(value.toEpochMilli()));
}
});

JSONValue.registerReader(RegularClass.class, new JsonReaderI<RegularClass>(JSONValue.defaultReader) {
@Override
public void setValue(Object current, String key, Object value) throws ParseException, IOException {
if (key.equals("instant")) {
Instant inst = Instant.ofEpochMilli((((Number)value).longValue()));
((RegularClass)current).setInstant(inst);
}
}
@Override
public Object createObject() {
return new RegularClass();
}
});
Instant instant = Instant.now();
RegularClass regularClass = new RegularClass();
regularClass.setInstant(instant);
String data = JSONValue.toJSONString(regularClass);
RegularClass result = JSONValue.parse(data, RegularClass.class);
assertEquals(result.getInstant(), instant);
}

public static class RegularClass {
private java.time.Instant instant;
public java.time.Instant getInstant() {
return instant;
}
public void setInstant(java.time.Instant instant) {
this.instant = instant;
}
}
}
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;
}
}
}

0 comments on commit ec6a113

Please sign in to comment.