Fixes #3385 Modernize jetty-util-ajax.

Added type parameter to methods that were using raw types.
Updated the implementation to use generics.
Deprecated static methods.
Updated usages.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2020-01-14 12:03:18 +01:00
parent 51b97d5170
commit 9b5aa73f59
26 changed files with 1180 additions and 1415 deletions

View File

@ -101,19 +101,19 @@ public class AbstractRestServlet extends HttpServlet
} }
} }
protected String generateThumbs(Queue<Map<String, String>> results) protected String generateThumbs(Queue<Map<String, Object>> results)
{ {
StringBuilder thumbs = new StringBuilder(); StringBuilder thumbs = new StringBuilder();
for (Map<String, String> m : results) for (Map<String, Object> m : results)
{ {
if (!m.containsKey("GalleryURL")) if (!m.containsKey("GalleryURL"))
continue; continue;
thumbs.append("<a href=\"" + m.get("ViewItemURLForNaturalSearch") + "\">"); thumbs.append("<a href=\"").append(m.get("ViewItemURLForNaturalSearch")).append("\">");
thumbs.append("<img class='thumb' border='1px' height='25px'" + thumbs.append("<img class='thumb' border='1px' height='25px' src='")
" src='" + m.get("GalleryURL") + "'" + .append(m.get("GalleryURL")).append("'")
" title='" + m.get("Title") + "'" + .append(" title='").append(m.get("Title")).append("'")
"/>"); .append("/>");
thumbs.append("</a>&nbsp;"); thumbs.append("</a>&nbsp;");
} }
return thumbs.toString(); return thumbs.toString();

View File

@ -77,17 +77,18 @@ public class AsyncRestServlet extends AbstractRestServlet
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ {
Long start = System.nanoTime(); long start = System.nanoTime();
// Do we have results yet? // Do we have results yet?
Queue<Map<String, String>> results = (Queue<Map<String, String>>)request.getAttribute(RESULTS_ATTR); @SuppressWarnings("unchecked")
Queue<Map<String, Object>> results = (Queue<Map<String, Object>>)request.getAttribute(RESULTS_ATTR);
// If no results, this must be the first dispatch, so send the REST request(s) // If no results, this must be the first dispatch, so send the REST request(s)
if (results == null) if (results == null)
{ {
// define results data structures // define results data structures
final Queue<Map<String, String>> resultsQueue = new ConcurrentLinkedQueue<>(); results = new ConcurrentLinkedQueue<>();
request.setAttribute(RESULTS_ATTR, results = resultsQueue); request.setAttribute(RESULTS_ATTR, results);
// suspend the request // suspend the request
// This is done before scheduling async handling to avoid race of // This is done before scheduling async handling to avoid race of
@ -100,13 +101,14 @@ public class AsyncRestServlet extends AbstractRestServlet
final AtomicInteger outstanding = new AtomicInteger(keywords.length); final AtomicInteger outstanding = new AtomicInteger(keywords.length);
// Send request each keyword // Send request each keyword
Queue<Map<String, Object>> resultsQueue = results;
for (final String item : keywords) for (final String item : keywords)
{ {
_client.newRequest(restURL(item)).method(HttpMethod.GET).send( _client.newRequest(restURL(item)).method(HttpMethod.GET).send(
new AsyncRestRequest() new AsyncRestRequest()
{ {
@Override @Override
void onAuctionFound(Map<String, String> auction) void onAuctionFound(Map<String, Object> auction)
{ {
resultsQueue.add(auction); resultsQueue.add(auction);
} }
@ -163,9 +165,9 @@ public class AsyncRestServlet extends AbstractRestServlet
out.close(); out.close();
} }
private abstract class AsyncRestRequest extends Response.Listener.Adapter private abstract static class AsyncRestRequest extends Response.Listener.Adapter
{ {
final Utf8StringBuilder _content = new Utf8StringBuilder(); private final Utf8StringBuilder _content = new Utf8StringBuilder();
AsyncRestRequest() AsyncRestRequest()
{ {
@ -182,13 +184,16 @@ public class AsyncRestServlet extends AbstractRestServlet
public void onComplete(Result result) public void onComplete(Result result)
{ {
// extract auctions from the results // extract auctions from the results
Map<String, ?> query = (Map<String, ?>)JSON.parse(_content.toString()); @SuppressWarnings("unchecked")
Map<String, Object> query = (Map<String, Object>)new JSON().fromJSON(_content.toString());
Object[] auctions = (Object[])query.get("Item"); Object[] auctions = (Object[])query.get("Item");
if (auctions != null) if (auctions != null)
{ {
for (Object o : auctions) for (Object o : auctions)
{ {
onAuctionFound((Map<String, String>)o); @SuppressWarnings("unchecked")
Map<String, Object> auction = (Map<String, Object>)o;
onAuctionFound(auction);
} }
} }
onComplete(); onComplete();
@ -196,8 +201,7 @@ public class AsyncRestServlet extends AbstractRestServlet
abstract void onComplete(); abstract void onComplete();
abstract void onAuctionFound(Map<String, String> details); abstract void onAuctionFound(Map<String, Object> details);
} }
@Override @Override

View File

@ -45,7 +45,7 @@ public class SerialRestServlet extends AbstractRestServlet
final long start = System.nanoTime(); final long start = System.nanoTime();
String[] keywords = sanitize(request.getParameter(ITEMS_PARAM)).split(","); String[] keywords = sanitize(request.getParameter(ITEMS_PARAM)).split(",");
Queue<Map<String, String>> results = new LinkedList<Map<String, String>>(); Queue<Map<String, Object>> results = new LinkedList<>();
// make all requests serially // make all requests serially
for (String itemName : keywords) for (String itemName : keywords)
@ -55,13 +55,16 @@ public class SerialRestServlet extends AbstractRestServlet
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
Map query = (Map)JSON.parse(new BufferedReader(new InputStreamReader(connection.getInputStream()))); @SuppressWarnings("unchecked")
Map<String, Object> query = (Map<String, Object>)new JSON().fromJSON(new BufferedReader(new InputStreamReader(connection.getInputStream())));
Object[] auctions = (Object[])query.get("Item"); Object[] auctions = (Object[])query.get("Item");
if (auctions != null) if (auctions != null)
{ {
for (Object o : auctions) for (Object o : auctions)
{ {
results.add((Map)o); @SuppressWarnings("unchecked")
Map<String, Object> auction = (Map<String, Object>)o;
results.add(auction);
} }
} }
} }

View File

@ -83,7 +83,7 @@ public class ManyHandlers
if (!params.isEmpty()) if (!params.isEmpty())
{ {
response.setContentType("text/plain"); response.setContentType("text/plain");
response.getWriter().println(JSON.toString(params)); response.getWriter().println(new JSON().toJSON(params));
baseRequest.setHandled(true); baseRequest.setHandled(true);
} }
} }

View File

@ -72,8 +72,9 @@ public class ManyHandlersTest extends AbstractEmbeddedTest
// test response content // test response content
String responseBody = response.getContentAsString(); String responseBody = response.getContentAsString();
Object jsonObj = JSON.parse(responseBody); Object jsonObj = new JSON().fromJSON(responseBody);
Map jsonMap = (Map)jsonObj; @SuppressWarnings("unchecked")
Map<String, Object> jsonMap = (Map<String, Object>)jsonObj;
assertThat("Response JSON keys.size", jsonMap.keySet().size(), is(2)); assertThat("Response JSON keys.size", jsonMap.keySet().size(), is(2));
} }

View File

@ -68,11 +68,14 @@ public class HpackPerfTest
assertNotNull(files); assertNotNull(files);
// Parse JSON // Parse JSON
Map[] stories = new Map[files.length]; @SuppressWarnings("unchecked")
Map<String, Object>[] stories = new Map[files.length];
int i = 0; int i = 0;
for (String story : files) for (String file : files)
{ {
stories[i++] = (Map)JSON.parse(new FileReader(new File(data, story))); @SuppressWarnings("unchecked")
var story = (Map<String, Object>)new JSON().fromJSON(new FileReader(new File(data, file)));
stories[i++] = story;
} }
ByteBuffer buffer = BufferUtil.allocate(256 * 1024); ByteBuffer buffer = BufferUtil.allocate(256 * 1024);
@ -88,27 +91,27 @@ public class HpackPerfTest
encodeStories(buffer, stories, "response"); encodeStories(buffer, stories, "response");
} }
private void encodeStories(ByteBuffer buffer, Map[] stories, String type) throws Exception private void encodeStories(ByteBuffer buffer, Map<String, Object>[] stories, String type) throws Exception
{ {
for (Map story : stories) for (Map<String, Object> story : stories)
{ {
if (type.equals(story.get("context"))) if (type.equals(story.get("context")))
{ {
HpackEncoder encoder = new HpackEncoder(_maxDynamicTableSize, _maxDynamicTableSize); HpackEncoder encoder = new HpackEncoder(_maxDynamicTableSize, _maxDynamicTableSize);
encoder.setValidateEncoding(false); encoder.setValidateEncoding(false);
// System.err.println(story);
Object[] cases = (Object[])story.get("cases"); Object[] cases = (Object[])story.get("cases");
for (Object c : cases) for (Object c : cases)
{ {
// System.err.println(" "+c); @SuppressWarnings("unchecked")
Object[] headers = (Object[])((Map)c).get("headers"); var kase = (Map<String, Object>)c;
Object[] headers = (Object[])kase.get("headers");
// System.err.println(" "+headers); // System.err.println(" "+headers);
HttpFields fields = new HttpFields(); HttpFields fields = new HttpFields();
for (Object header : headers) for (Object header : headers)
{ {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, String> h = (Map)header; var h = (Map<String, String>)header;
Map.Entry<String, String> e = h.entrySet().iterator().next(); Map.Entry<String, String> e = h.entrySet().iterator().next();
fields.add(e.getKey(), e.getValue()); fields.add(e.getKey(), e.getValue());
_unencodedSize += e.getKey().length() + e.getValue().length(); _unencodedSize += e.getKey().length() + e.getValue().length();

View File

@ -54,10 +54,13 @@ public class JwtDecoder
String jwtClaimString = new String(decoder.decode(padJWTSection(sections[1])), StandardCharsets.UTF_8); String jwtClaimString = new String(decoder.decode(padJWTSection(sections[1])), StandardCharsets.UTF_8);
String jwtSignature = sections[2]; String jwtSignature = sections[2];
Object parsedJwtHeader = JSON.parse(jwtHeaderString); JSON json = new JSON();
Object parsedJwtHeader = json.fromJSON(jwtHeaderString);
if (!(parsedJwtHeader instanceof Map)) if (!(parsedJwtHeader instanceof Map))
throw new IllegalStateException("Invalid JWT header"); throw new IllegalStateException("Invalid JWT header");
Map<String, Object> jwtHeader = (Map)parsedJwtHeader; @SuppressWarnings("unchecked")
Map<String, Object> jwtHeader = (Map<String, Object>)parsedJwtHeader;
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("JWT Header: {}", jwtHeader); LOG.debug("JWT Header: {}", jwtHeader);
@ -67,10 +70,10 @@ public class JwtDecoder
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("JWT signature not validated {}", jwtSignature); LOG.debug("JWT signature not validated {}", jwtSignature);
Object parsedClaims = JSON.parse(jwtClaimString); Object parsedClaims = json.fromJSON(jwtClaimString);
if (!(parsedClaims instanceof Map)) if (!(parsedClaims instanceof Map))
throw new IllegalStateException("Could not decode JSON for JWT claims."); throw new IllegalStateException("Could not decode JSON for JWT claims.");
return (Map)parsedClaims; return (Map<String, Object>)parsedClaims;
} }
static byte[] padJWTSection(String unpaddedEncodedJwtSection) static byte[] padJWTSection(String unpaddedEncodedJwtSection)

View File

@ -128,11 +128,11 @@ public class OpenIdConfiguration extends ContainerLifeCycle
Map<String, Object> result; Map<String, Object> result;
String responseBody = httpClient.GET(provider + CONFIG_PATH) String responseBody = httpClient.GET(provider + CONFIG_PATH)
.getContentAsString(); .getContentAsString();
Object parsedResult = JSON.parse(responseBody); Object parsedResult = new JSON().fromJSON(responseBody);
if (parsedResult instanceof Map) if (parsedResult instanceof Map)
{ {
Map<?, ?> rawResult = (Map)parsedResult; Map<?, ?> rawResult = (Map<?, ?>)parsedResult;
result = rawResult.entrySet().stream() result = rawResult.entrySet().stream()
.collect(Collectors.toMap(it -> it.getKey().toString(), Map.Entry::getValue)); .collect(Collectors.toMap(it -> it.getKey().toString(), Map.Entry::getValue));
} }

View File

@ -185,9 +185,9 @@ public class OpenIdCredentials implements Serializable
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("Authentication response: {}", responseBody); LOG.debug("Authentication response: {}", responseBody);
Object parsedResponse = JSON.parse(responseBody); Object parsedResponse = new JSON().fromJSON(responseBody);
if (!(parsedResponse instanceof Map)) if (!(parsedResponse instanceof Map))
throw new IllegalStateException("Malformed response from OpenID Provider"); throw new IllegalStateException("Malformed response from OpenID Provider");
return (Map)parsedResponse; return (Map<String, Object>)parsedResponse;
} }
} }

View File

@ -863,20 +863,21 @@ public class AsyncMiddleManServletTest
@Test @Test
public void testAfterContentTransformer() throws Exception public void testAfterContentTransformer() throws Exception
{ {
final String key0 = "id"; String key0 = "id";
long value0 = 1; long value0 = 1;
final String key1 = "channel"; String key1 = "channel";
String value1 = "foo"; String value1 = "foo";
final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }"; String jsonString = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
startServer(new HttpServlet() startServer(new HttpServlet()
{ {
@Override @Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
{ {
response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8)); response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
} }
}); });
final String key2 = "c"; String key2 = "c";
JSON json = new JSON();
startProxy(new AsyncMiddleManServlet() startProxy(new AsyncMiddleManServlet()
{ {
@Override @Override
@ -889,12 +890,12 @@ public class AsyncMiddleManServletTest
{ {
InputStream input = source.getInputStream(); InputStream input = source.getInputStream();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> obj = (Map<String, Object>)JSON.parse(new InputStreamReader(input, StandardCharsets.UTF_8)); Map<String, Object> obj = (Map<String, Object>)json.fromJSON(new InputStreamReader(input, StandardCharsets.UTF_8));
// Transform the object. // Transform the object.
obj.put(key2, obj.remove(key1)); obj.put(key2, obj.remove(key1));
try (OutputStream output = sink.getOutputStream()) try (OutputStream output = sink.getOutputStream())
{ {
output.write(JSON.toString(obj).getBytes(StandardCharsets.UTF_8)); output.write(json.toJSON(obj).getBytes(StandardCharsets.UTF_8));
return true; return true;
} }
} }
@ -909,7 +910,7 @@ public class AsyncMiddleManServletTest
assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> obj = (Map<String, Object>)JSON.parse(response.getContentAsString()); Map<String, Object> obj = (Map<String, Object>)json.fromJSON(response.getContentAsString());
assertNotNull(obj); assertNotNull(obj);
assertEquals(2, obj.size()); assertEquals(2, obj.size());
assertEquals(value0, obj.get(key0)); assertEquals(value0, obj.get(key0));
@ -984,24 +985,25 @@ public class AsyncMiddleManServletTest
public void testAfterContentTransformerOverflowingToDisk() throws Exception public void testAfterContentTransformerOverflowingToDisk() throws Exception
{ {
// Make sure the temporary directory we use exists and it's empty. // Make sure the temporary directory we use exists and it's empty.
final Path targetTestsDir = prepareTargetTestsDir(); Path targetTestsDir = prepareTargetTestsDir();
final String key0 = "id"; String key0 = "id";
long value0 = 1; long value0 = 1;
final String key1 = "channel"; String key1 = "channel";
String value1 = "foo"; String value1 = "foo";
final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }"; String jsonString = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
startServer(new HttpServlet() startServer(new HttpServlet()
{ {
@Override @Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
{ {
response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8)); response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
} }
}); });
final String inputPrefix = "in_"; String inputPrefix = "in_";
final String outputPrefix = "out_"; String outputPrefix = "out_";
final String key2 = "c"; String key2 = "c";
JSON json = new JSON();
startProxy(new AsyncMiddleManServlet() startProxy(new AsyncMiddleManServlet()
{ {
@Override @Override
@ -1014,18 +1016,18 @@ public class AsyncMiddleManServletTest
{ {
InputStream input = source.getInputStream(); InputStream input = source.getInputStream();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> obj = (Map<String, Object>)JSON.parse(new InputStreamReader(input, StandardCharsets.UTF_8)); Map<String, Object> obj = (Map<String, Object>)json.fromJSON(new InputStreamReader(input, StandardCharsets.UTF_8));
// Transform the object. // Transform the object.
obj.put(key2, obj.remove(key1)); obj.put(key2, obj.remove(key1));
try (OutputStream output = sink.getOutputStream()) try (OutputStream output = sink.getOutputStream())
{ {
output.write(JSON.toString(obj).getBytes(StandardCharsets.UTF_8)); output.write(json.toJSON(obj).getBytes(StandardCharsets.UTF_8));
return true; return true;
} }
} }
}; };
transformer.setOverflowDirectory(targetTestsDir); transformer.setOverflowDirectory(targetTestsDir);
int maxBufferSize = json.length() / 4; int maxBufferSize = jsonString.length() / 4;
transformer.setMaxInputBufferSize(maxBufferSize); transformer.setMaxInputBufferSize(maxBufferSize);
transformer.setInputFilePrefix(inputPrefix); transformer.setInputFilePrefix(inputPrefix);
transformer.setMaxOutputBufferSize(maxBufferSize); transformer.setMaxOutputBufferSize(maxBufferSize);
@ -1041,7 +1043,7 @@ public class AsyncMiddleManServletTest
assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> obj = (Map<String, Object>)JSON.parse(response.getContentAsString()); Map<String, Object> obj = (Map<String, Object>)json.fromJSON(response.getContentAsString());
assertNotNull(obj); assertNotNull(obj);
assertEquals(2, obj.size()); assertEquals(2, obj.size());
assertEquals(value0, obj.get(key0)); assertEquals(value0, obj.get(key0));
@ -1202,19 +1204,20 @@ public class AsyncMiddleManServletTest
private void testAfterContentTransformerDoNoTransform(final boolean readSource, final boolean useDisk) throws Exception private void testAfterContentTransformerDoNoTransform(final boolean readSource, final boolean useDisk) throws Exception
{ {
final String key0 = "id"; String key0 = "id";
long value0 = 1; long value0 = 1;
final String key1 = "channel"; String key1 = "channel";
String value1 = "foo"; String value1 = "foo";
final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }"; String jsonString = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
startServer(new HttpServlet() startServer(new HttpServlet()
{ {
@Override @Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
{ {
response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8)); response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8));
} }
}); });
JSON json = new JSON();
startProxy(new AsyncMiddleManServlet() startProxy(new AsyncMiddleManServlet()
{ {
@Override @Override
@ -1233,7 +1236,7 @@ public class AsyncMiddleManServletTest
if (readSource) if (readSource)
{ {
InputStream input = source.getInputStream(); InputStream input = source.getInputStream();
JSON.parse(new InputStreamReader(input, StandardCharsets.UTF_8)); json.fromJSON(new InputStreamReader(input, StandardCharsets.UTF_8));
} }
// No transformation. // No transformation.
return false; return false;
@ -1249,7 +1252,7 @@ public class AsyncMiddleManServletTest
assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> obj = (Map<String, Object>)JSON.parse(response.getContentAsString()); Map<String, Object> obj = (Map<String, Object>)json.fromJSON(response.getContentAsString());
assertNotNull(obj); assertNotNull(obj);
assertEquals(2, obj.size()); assertEquals(2, obj.size());
assertEquals(value0, obj.get(key0)); assertEquals(value0, obj.get(key0));

View File

@ -474,7 +474,8 @@ public class ErrorHandlerTest
} }
else if (contentType.contains("text/json")) else if (contentType.contains("text/json"))
{ {
Map jo = (Map)JSON.parse(response.getContent()); @SuppressWarnings("unchecked")
Map<String, Object> jo = (Map<String, Object>)new JSON().fromJSON(response.getContent());
Set<String> acceptableKeyNames = new HashSet<>(); Set<String> acceptableKeyNames = new HashSet<>();
acceptableKeyNames.add("url"); acceptableKeyNames.add("url");

View File

@ -30,16 +30,18 @@ public class JSONCollectionConvertor implements JSON.Convertor
public void toJSON(Object obj, JSON.Output out) public void toJSON(Object obj, JSON.Output out)
{ {
out.addClass(obj.getClass()); out.addClass(obj.getClass());
out.add("list", ((Collection)obj).toArray()); Collection<?> collection = (Collection<?>)obj;
out.add("list", collection.toArray());
} }
@Override @Override
public Object fromJSON(Map object) public Object fromJSON(Map<String, Object> object)
{ {
try try
{ {
Collection result = (Collection)Loader.loadClass((String)object.get("class")) Class<?> cls = Loader.loadClass((String)object.get("class"));
.getDeclaredConstructor().newInstance(); @SuppressWarnings("unchecked")
Collection<Object> result = (Collection<Object>)cls.getConstructor().newInstance();
Collections.addAll(result, (Object[])object.get("list")); Collections.addAll(result, (Object[])object.get("list"));
return result; return result;
} }

View File

@ -71,7 +71,7 @@ public class JSONDateConvertor implements JSON.Convertor
} }
@Override @Override
public Object fromJSON(Map map) public Object fromJSON(Map<String, Object> map)
{ {
if (!_fromJSON) if (!_fromJSON)
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();

View File

@ -18,7 +18,6 @@
package org.eclipse.jetty.util.ajax; package org.eclipse.jetty.util.ajax;
import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import org.eclipse.jetty.util.Loader; import org.eclipse.jetty.util.Loader;
@ -36,19 +35,6 @@ public class JSONEnumConvertor implements JSON.Convertor
{ {
private static final Logger LOG = Log.getLogger(JSONEnumConvertor.class); private static final Logger LOG = Log.getLogger(JSONEnumConvertor.class);
private boolean _fromJSON; private boolean _fromJSON;
private Method _valueOf;
{
try
{
Class<?> e = Loader.loadClass("java.lang.Enum");
_valueOf = e.getMethod("valueOf", Class.class, String.class);
}
catch (Exception e)
{
throw new RuntimeException("!Enums", e);
}
}
public JSONEnumConvertor() public JSONEnumConvertor()
{ {
@ -61,20 +47,21 @@ public class JSONEnumConvertor implements JSON.Convertor
} }
@Override @Override
public Object fromJSON(Map map) public Object fromJSON(Map<String, Object> map)
{ {
if (!_fromJSON) if (!_fromJSON)
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
try try
{ {
Class c = Loader.loadClass((String)map.get("class")); @SuppressWarnings({"rawtypes", "unchecked"})
return _valueOf.invoke(null, c, map.get("value")); Class<? extends Enum> type = Loader.loadClass((String)map.get("class"));
return Enum.valueOf(type, (String)map.get("value"));
} }
catch (Exception e) catch (Exception e)
{ {
LOG.warn(e); LOG.warn(e);
return null;
} }
return null;
} }
@Override @Override
@ -83,11 +70,11 @@ public class JSONEnumConvertor implements JSON.Convertor
if (_fromJSON) if (_fromJSON)
{ {
out.addClass(obj.getClass()); out.addClass(obj.getClass());
out.add("value", ((Enum)obj).name()); out.add("value", ((Enum<?>)obj).name());
} }
else else
{ {
out.add(((Enum)obj).name()); out.add(((Enum<?>)obj).name());
} }
} }
} }

View File

@ -20,8 +20,6 @@ package org.eclipse.jetty.util.ajax;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -29,36 +27,35 @@ import java.util.Set;
import org.eclipse.jetty.util.ajax.JSON.Output; import org.eclipse.jetty.util.ajax.JSON.Output;
/** /**
* Convert an Object to JSON using reflection on getters methods. * Converts an Object to JSON using reflection on getters methods.
*/ */
public class JSONObjectConvertor implements JSON.Convertor public class JSONObjectConvertor implements JSON.Convertor
{ {
private boolean _fromJSON; private final boolean _fromJSON;
private Set _excluded = null; private final Set<String> _excluded;
public JSONObjectConvertor() public JSONObjectConvertor()
{ {
_fromJSON = false; this(false);
} }
public JSONObjectConvertor(boolean fromJSON) public JSONObjectConvertor(boolean fromJSON)
{ {
_fromJSON = fromJSON; this(fromJSON, null);
} }
/** /**
* @param fromJSON true to convert from JSON * @param fromJSON true to convert from JSON
* @param excluded An array of field names to exclude from the conversion * @param excludedFieldNames An array of field names to exclude from the conversion
*/ */
public JSONObjectConvertor(boolean fromJSON, String[] excluded) public JSONObjectConvertor(boolean fromJSON, String[] excludedFieldNames)
{ {
_fromJSON = fromJSON; _fromJSON = fromJSON;
if (excluded != null) _excluded = excludedFieldNames == null ? Set.of() : Set.of(excludedFieldNames);
_excluded = new HashSet(Arrays.asList(excluded));
} }
@Override @Override
public Object fromJSON(Map map) public Object fromJSON(Map<String, Object> map)
{ {
if (_fromJSON) if (_fromJSON)
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -70,16 +67,13 @@ public class JSONObjectConvertor implements JSON.Convertor
{ {
try try
{ {
Class c = obj.getClass(); Class<?> c = obj.getClass();
if (_fromJSON) if (_fromJSON)
out.addClass(obj.getClass()); out.addClass(c);
Method[] methods = obj.getClass().getMethods(); for (Method m : c.getMethods())
for (int i = 0; i < methods.length; i++)
{ {
Method m = methods[i];
if (!Modifier.isStatic(m.getModifiers()) && if (!Modifier.isStatic(m.getModifiers()) &&
m.getParameterCount() == 0 && m.getParameterCount() == 0 &&
m.getReturnType() != null && m.getReturnType() != null &&
@ -92,7 +86,6 @@ public class JSONObjectConvertor implements JSON.Convertor
name = name.substring(3, 4).toLowerCase(Locale.ENGLISH) + name.substring(4); name = name.substring(3, 4).toLowerCase(Locale.ENGLISH) + name.substring(4);
else else
continue; continue;
if (includeField(name, obj, m)) if (includeField(name, obj, m))
out.add(name, m.invoke(obj, (Object[])null)); out.add(name, m.invoke(obj, (Object[])null));
} }
@ -106,6 +99,6 @@ public class JSONObjectConvertor implements JSON.Convertor
protected boolean includeField(String name, Object o, Method m) protected boolean includeField(String name, Object o, Method m)
{ {
return _excluded == null || !_excluded.contains(name); return !_excluded.contains(name);
} }
} }

View File

@ -19,13 +19,11 @@
package org.eclipse.jetty.util.ajax; package org.eclipse.jetty.util.ajax;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -35,18 +33,37 @@ import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
/** /**
* Converts POJOs to JSON and vice versa. * <p>Converts POJOs to JSON and vice versa.</p>
* The key difference: * <p>The key differences with respect to {@link JSONObjectConvertor} are:</p>
* - returns the actual object from Convertor.fromJSON (JSONObjectConverter returns a Map) * <ul>
* - the getters/setters are resolved at initialization (JSONObjectConverter resolves it at runtime) * <li>returns the actual object from Convertor.fromJSON (JSONObjectConverter returns a Map)</li>
* - correctly sets the number fields * <li>the getters/setters are resolved at initialization (JSONObjectConverter resolves it at runtime)</li>
* <li>correctly sets the number fields</li>
* </ul>
*/ */
public class JSONPojoConvertor implements JSON.Convertor public class JSONPojoConvertor implements JSON.Convertor
{ {
private static final Logger LOG = Log.getLogger(JSONPojoConvertor.class); private static final Logger LOG = Log.getLogger(JSONPojoConvertor.class);
public static final Object[] GETTER_ARG = new Object[]{};
public static final Object[] NULL_ARG = new Object[]{null};
private static final Map<Class<?>, NumberType> __numberTypes = new HashMap<>(); private static final Map<Class<?>, NumberType> __numberTypes = new HashMap<>();
private static final NumberType SHORT = Number::shortValue;
private static final NumberType INTEGER = Number::intValue;
private static final NumberType FLOAT = Number::floatValue;
private static final NumberType LONG = Number::longValue;
private static final NumberType DOUBLE = Number::doubleValue;
static
{
__numberTypes.put(Short.class, SHORT);
__numberTypes.put(Short.TYPE, SHORT);
__numberTypes.put(Integer.class, INTEGER);
__numberTypes.put(Integer.TYPE, INTEGER);
__numberTypes.put(Long.class, LONG);
__numberTypes.put(Long.TYPE, LONG);
__numberTypes.put(Float.class, FLOAT);
__numberTypes.put(Float.TYPE, FLOAT);
__numberTypes.put(Double.class, DOUBLE);
__numberTypes.put(Double.TYPE, DOUBLE);
}
public static NumberType getNumberType(Class<?> clazz) public static NumberType getNumberType(Class<?> clazz)
{ {
@ -55,8 +72,8 @@ public class JSONPojoConvertor implements JSON.Convertor
protected boolean _fromJSON; protected boolean _fromJSON;
protected Class<?> _pojoClass; protected Class<?> _pojoClass;
protected Map<String, Method> _getters = new HashMap<String, Method>(); protected Map<String, Method> _getters = new HashMap<>();
protected Map<String, Setter> _setters = new HashMap<String, Setter>(); protected Map<String, Setter> _setters = new HashMap<>();
protected Set<String> _excluded; protected Set<String> _excluded;
/** /**
@ -64,7 +81,16 @@ public class JSONPojoConvertor implements JSON.Convertor
*/ */
public JSONPojoConvertor(Class<?> pojoClass) public JSONPojoConvertor(Class<?> pojoClass)
{ {
this(pojoClass, (Set<String>)null, true); this(pojoClass, null, true);
}
/**
* @param pojoClass The class to convert
* @param fromJSON If true, add a class field to the JSON
*/
public JSONPojoConvertor(Class<?> pojoClass, boolean fromJSON)
{
this(pojoClass, null, fromJSON);
} }
/** /**
@ -73,7 +99,7 @@ public class JSONPojoConvertor implements JSON.Convertor
*/ */
public JSONPojoConvertor(Class<?> pojoClass, String[] excluded) public JSONPojoConvertor(Class<?> pojoClass, String[] excluded)
{ {
this(pojoClass, new HashSet<String>(Arrays.asList(excluded)), true); this(pojoClass, new HashSet<>(Arrays.asList(excluded)));
} }
/** /**
@ -98,21 +124,11 @@ public class JSONPojoConvertor implements JSON.Convertor
init(); init();
} }
/**
* @param pojoClass The class to convert
* @param fromJSON If true, add a class field to the JSON
*/
public JSONPojoConvertor(Class<?> pojoClass, boolean fromJSON)
{
this(pojoClass, (Set<String>)null, fromJSON);
}
protected void init() protected void init()
{ {
Method[] methods = _pojoClass.getMethods(); Method[] methods = _pojoClass.getMethods();
for (int i = 0; i < methods.length; i++) for (Method m : methods)
{ {
Method m = methods[i];
if (!Modifier.isStatic(m.getModifiers()) && m.getDeclaringClass() != Object.class) if (!Modifier.isStatic(m.getModifiers()) && m.getDeclaringClass() != Object.class)
{ {
String name = m.getName(); String name = m.getName();
@ -167,53 +183,45 @@ public class JSONPojoConvertor implements JSON.Convertor
return _excluded == null || !_excluded.contains(name); return _excluded == null || !_excluded.contains(name);
} }
protected int getExcludedCount()
{
return _excluded == null ? 0 : _excluded.size();
}
@Override @Override
public Object fromJSON(Map object) public Object fromJSON(Map<String, Object> object)
{ {
Object obj = null;
try try
{ {
obj = _pojoClass.getDeclaredConstructor().newInstance(); Object obj = _pojoClass.getConstructor().newInstance();
setProps(obj, object);
return obj;
} }
catch (Exception e) catch (Exception e)
{ {
// TODO return Map instead? // TODO return Map instead?
throw new RuntimeException(e); throw new RuntimeException(e);
} }
setProps(obj, object);
return obj;
} }
public int setProps(Object obj, Map<?, ?> props) private void setProps(Object obj, Map<String, Object> props)
{ {
int count = 0; for (Map.Entry<String, Object> entry : props.entrySet())
for (Iterator<?> iterator = props.entrySet().iterator(); iterator.hasNext(); )
{ {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)iterator.next(); Setter setter = getSetter(entry.getKey());
Setter setter = getSetter((String)entry.getKey());
if (setter != null) if (setter != null)
{ {
try try
{ {
setter.invoke(obj, entry.getValue()); setter.invoke(obj, entry.getValue());
count++;
} }
catch (Exception e) catch (Exception e)
{ {
// TODO throw exception? // TODO throw exception?
LOG.warn(_pojoClass.getName() + "#" + setter.getPropertyName() + " not set from " + LOG.warn("{}#{} not set from value {}={}: {}",
(entry.getValue().getClass().getName()) + "=" + entry.getValue().toString()); _pojoClass.getName(),
log(e); setter.getPropertyName(),
setter.getType().getName(),
entry.getValue(),
e.toString());
} }
} }
} }
return count;
} }
@Override @Override
@ -225,23 +233,19 @@ public class JSONPojoConvertor implements JSON.Convertor
{ {
try try
{ {
out.add(entry.getKey(), entry.getValue().invoke(obj, GETTER_ARG)); out.add(entry.getKey(), entry.getValue().invoke(obj));
} }
catch (Exception e) catch (Exception e)
{ {
// TODO throw exception? // TODO throw exception?
LOG.warn("{} property '{}' excluded. (errors)", _pojoClass.getName(), LOG.warn("{}#{} excluded: {}",
entry.getKey()); _pojoClass.getName(),
log(e); entry.getKey(),
e.toString());
} }
} }
} }
protected void log(Throwable t)
{
LOG.ignore(t);
}
public static class Setter public static class Setter
{ {
protected String _propertyName; protected String _propertyName;
@ -255,11 +259,11 @@ public class JSONPojoConvertor implements JSON.Convertor
_propertyName = propertyName; _propertyName = propertyName;
_setter = method; _setter = method;
_type = method.getParameterTypes()[0]; _type = method.getParameterTypes()[0];
_numberType = __numberTypes.get(_type); _numberType = JSONPojoConvertor.getNumberType(_type);
if (_numberType == null && _type.isArray()) if (_numberType == null && _type.isArray())
{ {
_componentType = _type.getComponentType(); _componentType = _type.getComponentType();
_numberType = __numberTypes.get(_componentType); _numberType = JSONPojoConvertor.getNumberType(_componentType);
} }
} }
@ -290,79 +294,84 @@ public class JSONPojoConvertor implements JSON.Convertor
public boolean isPropertyNumber() public boolean isPropertyNumber()
{ {
return _numberType != null; return getNumberType() != null;
} }
public void invoke(Object obj, Object value) throws IllegalArgumentException, public void invoke(Object obj, Object value) throws Exception
IllegalAccessException, InvocationTargetException
{ {
if (value == null) if (value == null)
_setter.invoke(obj, NULL_ARG); _setter.invoke(obj, value);
else else
invokeObject(obj, value); invokeObject(obj, value);
} }
protected void invokeObject(Object obj, Object value) throws IllegalArgumentException, protected void invokeObject(Object obj, Object value) throws Exception
IllegalAccessException, InvocationTargetException
{ {
if (_type.isEnum()) if (_type.isEnum())
{ {
if (value instanceof Enum) if (value instanceof Enum)
_setter.invoke(obj, new Object[]{value}); {
_setter.invoke(obj, value);
}
else else
_setter.invoke(obj, new Object[]{Enum.valueOf((Class<? extends Enum>)_type, value.toString())}); {
@SuppressWarnings({"rawtypes", "unchecked"})
Class<? extends Enum> enumType = (Class<? extends Enum>)_type;
@SuppressWarnings("unchecked")
Enum<?> enumValue = Enum.valueOf(enumType, value.toString());
_setter.invoke(obj, enumValue);
}
} }
else if (_numberType != null && value instanceof Number) else if (isPropertyNumber() && value instanceof Number)
{ {
_setter.invoke(obj, new Object[]{_numberType.getActualValue((Number)value)}); _setter.invoke(obj, _numberType.getActualValue((Number)value));
} }
else if (Character.TYPE.equals(_type) || Character.class.equals(_type)) else if (Character.TYPE.equals(_type) || Character.class.equals(_type))
{ {
_setter.invoke(obj, new Object[]{String.valueOf(value).charAt(0)}); _setter.invoke(obj, String.valueOf(value).charAt(0));
} }
else if (_componentType != null && value.getClass().isArray()) else if (_componentType != null && value.getClass().isArray())
{ {
if (_numberType == null) if (_numberType == null)
{ {
int len = Array.getLength(value); int len = Array.getLength(value);
Object array = Array.newInstance(_componentType, len); Object array = Array.newInstance(getComponentType(), len);
try try
{ {
System.arraycopy(value, 0, array, 0, len); System.arraycopy(value, 0, array, 0, len);
_setter.invoke(obj, array);
} }
catch (Exception e) catch (Exception e)
{ {
// unusual array with multiple types // Unusual array with multiple types.
LOG.ignore(e); LOG.ignore(e);
_setter.invoke(obj, new Object[]{value}); _setter.invoke(obj, value);
return;
} }
_setter.invoke(obj, new Object[]{array});
} }
else else
{ {
Object[] old = (Object[])value; Object[] old = (Object[])value;
Object array = Array.newInstance(_componentType, old.length); Object array = Array.newInstance(getComponentType(), old.length);
try try
{ {
for (int i = 0; i < old.length; i++) for (int i = 0; i < old.length; i++)
{ {
Array.set(array, i, _numberType.getActualValue((Number)old[i])); Array.set(array, i, _numberType.getActualValue((Number)old[i]));
} }
_setter.invoke(obj, array);
} }
catch (Exception e) catch (Exception e)
{ {
// unusual array with multiple types // unusual array with multiple types
LOG.ignore(e); LOG.ignore(e);
_setter.invoke(obj, new Object[]{value}); _setter.invoke(obj, value);
return;
} }
_setter.invoke(obj, new Object[]{array});
} }
} }
else else
_setter.invoke(obj, new Object[]{value}); {
_setter.invoke(obj, value);
}
} }
} }
@ -370,63 +379,4 @@ public class JSONPojoConvertor implements JSON.Convertor
{ {
public Object getActualValue(Number number); public Object getActualValue(Number number);
} }
public static final NumberType SHORT = new NumberType()
{
@Override
public Object getActualValue(Number number)
{
return number.shortValue();
}
};
public static final NumberType INTEGER = new NumberType()
{
@Override
public Object getActualValue(Number number)
{
return number.intValue();
}
};
public static final NumberType FLOAT = new NumberType()
{
@Override
public Object getActualValue(Number number)
{
return number.floatValue();
}
};
public static final NumberType LONG = new NumberType()
{
@Override
public Object getActualValue(Number number)
{
return number instanceof Long ? number : (Long)number.longValue();
}
};
public static final NumberType DOUBLE = new NumberType()
{
@Override
public Object getActualValue(Number number)
{
return number instanceof Double ? number : (Double)number.doubleValue();
}
};
static
{
__numberTypes.put(Short.class, SHORT);
__numberTypes.put(Short.TYPE, SHORT);
__numberTypes.put(Integer.class, INTEGER);
__numberTypes.put(Integer.TYPE, INTEGER);
__numberTypes.put(Long.class, LONG);
__numberTypes.put(Long.TYPE, LONG);
__numberTypes.put(Float.class, FLOAT);
__numberTypes.put(Float.TYPE, FLOAT);
__numberTypes.put(Double.class, DOUBLE);
__numberTypes.put(Double.TYPE, DOUBLE);
}
} }

View File

@ -31,12 +31,7 @@ public class JSONPojoConvertorFactory implements JSON.Convertor
public JSONPojoConvertorFactory(JSON json) public JSONPojoConvertorFactory(JSON json)
{ {
if (json == null) this(json, true);
{
throw new IllegalArgumentException();
}
_json = json;
_fromJson = true;
} }
/** /**
@ -48,9 +43,7 @@ public class JSONPojoConvertorFactory implements JSON.Convertor
public JSONPojoConvertorFactory(JSON json, boolean fromJSON) public JSONPojoConvertorFactory(JSON json, boolean fromJSON)
{ {
if (json == null) if (json == null)
{
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
_json = json; _json = json;
_fromJson = fromJSON; _fromJson = fromJSON;
} }
@ -58,31 +51,20 @@ public class JSONPojoConvertorFactory implements JSON.Convertor
@Override @Override
public void toJSON(Object obj, Output out) public void toJSON(Object obj, Output out)
{ {
String clsName = obj.getClass().getName(); Class<?> cls = obj.getClass();
String clsName = cls.getName();
Convertor convertor = _json.getConvertorFor(clsName); Convertor convertor = _json.getConvertorFor(clsName);
if (convertor == null) if (convertor == null)
{ {
try convertor = new JSONPojoConvertor(cls, _fromJson);
{ _json.addConvertorFor(clsName, convertor);
Class cls = Loader.loadClass(clsName);
convertor = new JSONPojoConvertor(cls, _fromJson);
_json.addConvertorFor(clsName, convertor);
}
catch (ClassNotFoundException e)
{
JSON.LOG.warn(e);
}
}
if (convertor != null)
{
convertor.toJSON(obj, out);
} }
convertor.toJSON(obj, out);
} }
@Override @Override
public Object fromJSON(Map object) public Object fromJSON(Map<String, Object> map)
{ {
Map map = object;
String clsName = (String)map.get("class"); String clsName = (String)map.get("class");
if (clsName != null) if (clsName != null)
{ {
@ -91,7 +73,7 @@ public class JSONPojoConvertorFactory implements JSON.Convertor
{ {
try try
{ {
Class cls = Loader.loadClass(clsName); Class<?> cls = Loader.loadClass(clsName);
convertor = new JSONPojoConvertor(cls, _fromJson); convertor = new JSONPojoConvertor(cls, _fromJson);
_json.addConvertorFor(clsName, convertor); _json.addConvertorFor(clsName, convertor);
} }
@ -101,9 +83,7 @@ public class JSONPojoConvertorFactory implements JSON.Convertor
} }
} }
if (convertor != null) if (convertor != null)
{ return convertor.fromJSON(map);
return convertor.fromJSON(object);
}
} }
return map; return map;
} }

View File

@ -0,0 +1,121 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.util.ajax;
public class Bar
{
private String _title;
private String _nullTest;
private Baz _baz;
private boolean _boolean1;
private Baz[] _bazs;
private Color _color;
public Bar()
{
// Required by the POJO convertor.
}
public Bar(String title, boolean boolean1, Baz baz)
{
this(title, boolean1, baz, null);
}
public Bar(String title, boolean boolean1, Baz baz, Baz[] bazs)
{
setTitle(title);
setBoolean1(boolean1);
setBaz(baz);
setBazs(bazs);
}
// Getters and setters required by the POJO convertor.
public void setTitle(String title)
{
_title = title;
}
public String getTitle()
{
return _title;
}
public void setNullTest(String nullTest)
{
assert (nullTest == null);
_nullTest = nullTest;
}
public String getNullTest()
{
return _nullTest;
}
public void setBaz(Baz baz)
{
_baz = baz;
}
public Baz getBaz()
{
return _baz;
}
public void setBoolean1(boolean boolean1)
{
_boolean1 = boolean1;
}
public boolean isBoolean1()
{
return _boolean1;
}
public void setBazs(Baz[] bazs)
{
_bazs = bazs;
}
public Baz[] getBazs()
{
return _bazs;
}
public Color getColor()
{
return _color;
}
public void setColor(Color color)
{
_color = color;
}
@Override
public String toString()
{
return "\n=== " + getClass().getSimpleName() + " ===" +
"\ntitle: " + getTitle() +
"\nboolean1: " + isBoolean1() +
"\nnullTest: " + getNullTest() +
"\nbaz: " + getBaz() +
"\ncolor: " + getColor();
}
}

View File

@ -0,0 +1,79 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.util.ajax;
public class Baz
{
private String _message;
private Foo _foo;
private Boolean _boolean2;
public Baz()
{
// Required by the POJO convertor.
}
public Baz(String message, Boolean boolean2, Foo foo)
{
setMessage(message);
setBoolean2(boolean2);
setFoo(foo);
}
// Getters and setters required by the POJO convertor.
public void setMessage(String message)
{
_message = message;
}
public String getMessage()
{
return _message;
}
public void setFoo(Foo foo)
{
_foo = foo;
}
public Foo getFoo()
{
return _foo;
}
public void setBoolean2(Boolean boolean2)
{
_boolean2 = boolean2;
}
public Boolean isBoolean2()
{
return _boolean2;
}
@Override
public String toString()
{
return "\n=== " + getClass().getSimpleName() + " ===" +
"\nmessage: " + getMessage() +
"\nboolean2: " + isBoolean2() +
"\nfoo: " + getFoo();
}
}

View File

@ -0,0 +1,24 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.util.ajax;
public enum Color
{
Green, Blue
}

View File

@ -0,0 +1,164 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.util.ajax;
public class Foo
{
private String _name;
private int _int1;
private Integer _int2;
private long _long1;
private Long _long2;
private float _float1;
private Float _float2;
private double _double1;
private Double _double2;
public Foo()
{
// Required by the POJO convertor.
}
// Getters and setters required by the POJO convertor.
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
public int getInt1()
{
return _int1;
}
public void setInt1(int int1)
{
_int1 = int1;
}
public Integer getInt2()
{
return _int2;
}
public void setInt2(Integer int2)
{
_int2 = int2;
}
public long getLong1()
{
return _long1;
}
public void setLong1(long long1)
{
_long1 = long1;
}
public Long getLong2()
{
return _long2;
}
public void setLong2(Long long2)
{
_long2 = long2;
}
public float getFloat1()
{
return _float1;
}
public void setFloat1(float float1)
{
_float1 = float1;
}
public Float getFloat2()
{
return _float2;
}
public void setFloat2(Float float2)
{
_float2 = float2;
}
public double getDouble1()
{
return _double1;
}
public void setDouble1(double double1)
{
_double1 = double1;
}
public Double getDouble2()
{
return _double2;
}
public void setDouble2(Double double2)
{
_double2 = double2;
}
@Override
public boolean equals(Object another)
{
if (another instanceof Foo)
{
Foo foo = (Foo)another;
return _name.equals(foo.getName()) &&
_int1 == foo.getInt1() &&
_int2.equals(foo.getInt2()) &&
_long1 == foo.getLong1() &&
_long2.equals(foo.getLong2()) &&
_float1 == foo.getFloat1() &&
_float2.equals(foo.getFloat2()) &&
_double1 == foo.getDouble1() &&
_double2.equals(foo.getDouble2());
}
return false;
}
@Override
public String toString()
{
return "\n=== " + getClass().getSimpleName() + " ===" +
"\nname: " + _name +
"\nint1: " + _int1 +
"\nint2: " + _int2 +
"\nlong1: " + _long1 +
"\nlong2: " + _long2 +
"\nfloat1: " + _float1 +
"\nfloat2: " + _float2 +
"\ndouble1: " + _double1 +
"\ndouble2: " + _double2;
}
}

View File

@ -39,35 +39,35 @@ import static org.junit.jupiter.api.Assertions.assertSame;
public class JSONCollectionConvertorTest public class JSONCollectionConvertorTest
{ {
@Test @Test
public void testArrayList() throws Exception public void testArrayList()
{ {
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<>();
Collections.addAll(list, "one", "two"); Collections.addAll(list, "one", "two");
testList(list); testList(list);
} }
@Test @Test
public void testLinkedList() throws Exception public void testLinkedList()
{ {
List<String> list = new LinkedList<String>(); List<String> list = new LinkedList<>();
Collections.addAll(list, "one", "two"); Collections.addAll(list, "one", "two");
testList(list); testList(list);
} }
@Test @Test
public void testCopyOnWriteArrayList() throws Exception public void testCopyOnWriteArrayList()
{ {
List<String> list = new CopyOnWriteArrayList<String>(); List<String> list = new CopyOnWriteArrayList<>();
Collections.addAll(list, "one", "two"); Collections.addAll(list, "one", "two");
testList(list); testList(list);
} }
private void testList(List<String> list1) throws Exception private void testList(List<String> list1)
{ {
JSON json = new JSON(); JSON json = new JSON();
json.addConvertor(List.class, new JSONCollectionConvertor()); json.addConvertor(List.class, new JSONCollectionConvertor());
Map<String, Object> object1 = new HashMap<String, Object>(); Map<String, Object> object1 = new HashMap<>();
String field = "field"; String field = "field";
object1.put(field, list1); object1.put(field, list1);
@ -75,7 +75,7 @@ public class JSONCollectionConvertorTest
assertThat(string, containsString(list1.getClass().getName())); assertThat(string, containsString(list1.getClass().getName()));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> object2 = (Map<String, Object>)json.parse(new JSON.StringSource(string)); Map<String, Object> object2 = (Map<String, Object>)json.fromJSON(string);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<String> list2 = (List<String>)object2.get(field); List<String> list2 = (List<String>)object2.get(field);
@ -84,17 +84,17 @@ public class JSONCollectionConvertorTest
} }
@Test @Test
public void testHashSet() throws Exception public void testHashSet()
{ {
Set<String> set = new HashSet<String>(); Set<String> set = new HashSet<>();
Collections.addAll(set, "one", "two", "three"); Collections.addAll(set, "one", "two", "three");
testSet(set); testSet(set);
} }
@Test @Test
public void testTreeSet() throws Exception public void testTreeSet()
{ {
Set<String> set = new TreeSet<String>(); Set<String> set = new TreeSet<>();
Collections.addAll(set, "one", "two", "three"); Collections.addAll(set, "one", "two", "three");
testSet(set); testSet(set);
} }
@ -108,7 +108,7 @@ public class JSONCollectionConvertorTest
assertThat(string, containsString(set1.getClass().getName())); assertThat(string, containsString(set1.getClass().getName()));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Set<String> set2 = (Set<String>)json.parse(new JSON.StringSource(string)); Set<String> set2 = (Set<String>)json.fromJSON(string);
assertSame(set1.getClass(), set2.getClass()); assertSame(set1.getClass(), set2.getClass());
assertEquals(set1, set2); assertEquals(set1, set2);

View File

@ -24,6 +24,8 @@ import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
@ -43,15 +45,15 @@ public class JSONPojoConvertorFactoryTest
jsonIn.addConvertor(Enum.class, new JSONEnumConvertor()); jsonIn.addConvertor(Enum.class, new JSONEnumConvertor());
Foo foo = new Foo(); Foo foo = new Foo();
foo._name = "Foo @ " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); foo.setName("Foo @ " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime()));
foo._int1 = 1; foo.setInt1(1);
foo._int2 = 2; foo.setInt2(2);
foo._long1 = 1000001L; foo.setLong1(1000001L);
foo._long2 = 1000002L; foo.setLong2(1000002L);
foo._float1 = 10.11f; foo.setFloat1(10.11f);
foo._float2 = 10.22f; foo.setFloat2(10.22f);
foo._double1 = 10000.11111d; foo.setDouble1(10000.11111d);
foo._double2 = 10000.22222d; foo.setDouble2(10000.22222d);
Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo), new Baz[]{ Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo), new Baz[]{
new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null) new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null)
@ -59,19 +61,16 @@ public class JSONPojoConvertorFactoryTest
bar.setColor(Color.Green); bar.setColor(Color.Green);
String s = jsonOut.toJSON(bar); String s = jsonOut.toJSON(bar);
Object obj = jsonIn.fromJSON(s);
Object obj = jsonIn.parse(new JSON.StringSource(s));
assertTrue(obj instanceof Bar); assertTrue(obj instanceof Bar);
Bar br = (Bar)obj; Bar br = (Bar)obj;
Baz bz = br.getBaz(); Baz bz = br.getBaz();
Foo f = bz.getFoo(); Foo f = bz.getFoo();
assertEquals(f, foo); assertEquals(f, foo);
assertTrue(br.getBazs().length == 2); assertEquals(2, br.getBazs().length);
assertEquals(br.getBazs()[0].getMessage(), "baz0"); assertEquals(br.getBazs()[0].getMessage(), "baz0");
assertEquals(br.getBazs()[1].getMessage(), "baz1"); assertEquals(br.getBazs()[1].getMessage(), "baz1");
assertEquals(Color.Green, br.getColor()); assertEquals(Color.Green, br.getColor());
@ -89,15 +88,15 @@ public class JSONPojoConvertorFactoryTest
jsonIn.addConvertor(Enum.class, new JSONEnumConvertor()); jsonIn.addConvertor(Enum.class, new JSONEnumConvertor());
Foo foo = new Foo(); Foo foo = new Foo();
foo._name = "Foo @ " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); foo.setName("Foo @ " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime()));
foo._int1 = 1; foo.setInt1(1);
foo._int2 = 2; foo.setInt2(2);
foo._long1 = 1000001L; foo.setLong1(1000001L);
foo._long2 = 1000002L; foo.setLong2(1000002L);
foo._float1 = 10.11f; foo.setFloat1(10.11f);
foo._float2 = 10.22f; foo.setFloat2(10.22f);
foo._double1 = 10000.11111d; foo.setDouble1(10000.11111d);
foo._double2 = 10000.22222d; foo.setDouble2(10000.22222d);
Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo), new Baz[]{ Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo), new Baz[]{
new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null) new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null)
@ -106,331 +105,28 @@ public class JSONPojoConvertorFactoryTest
String s = jsonOut.toJSON(bar); String s = jsonOut.toJSON(bar);
assertTrue(s.indexOf("class") < 0); assertFalse(s.contains("class"));
Object obj = jsonIn.parse(new JSON.StringSource(s)); Object obj = jsonIn.parse(new JSON.StringSource(s));
assertTrue(obj instanceof Map); assertTrue(obj instanceof Map);
@SuppressWarnings("unchecked")
Map<String, Object> br = (Map<String, Object>)obj; Map<String, Object> br = (Map<String, Object>)obj;
@SuppressWarnings("unchecked")
Map<String, Object> bz = (Map<String, Object>)br.get("baz"); Map<String, Object> bz = (Map<String, Object>)br.get("baz");
@SuppressWarnings("unchecked")
Map<String, Object> f = (Map<String, Object>)bz.get("foo"); Map<String, Object> f = (Map<String, Object>)bz.get("foo");
assertTrue(f != null);
assertNotNull(f);
Object[] bazs = (Object[])br.get("bazs"); Object[] bazs = (Object[])br.get("bazs");
assertTrue(bazs.length == 2); assertEquals(2, bazs.length);
assertEquals(((Map)bazs[0]).get("message"), "baz0"); @SuppressWarnings("unchecked")
assertEquals(((Map)bazs[1]).get("message"), "baz1"); Map<String, Object> map1 = (Map<String, Object>)bazs[0];
assertEquals(map1.get("message"), "baz0");
@SuppressWarnings("unchecked")
Map<String, Object> map2 = (Map<String, Object>)bazs[1];
assertEquals(map2.get("message"), "baz1");
assertEquals("Green", br.get("color")); assertEquals("Green", br.get("color"));
} }
enum Color
{
Red, Green, Blue
}
;
public static class Bar
{
private String _title;
private String _nullTest;
private Baz _baz;
private boolean _boolean1;
private Baz[] _bazs;
private Color _color;
public Bar()
{
}
public Bar(String title, boolean boolean1, Baz baz)
{
setTitle(title);
setBoolean1(boolean1);
setBaz(baz);
}
public Bar(String title, boolean boolean1, Baz baz, Baz[] bazs)
{
this(title, boolean1, baz);
setBazs(bazs);
}
@Override
public String toString()
{
return new StringBuffer().append("\n=== ").append(getClass().getSimpleName()).append(" ===")
.append("\ntitle: ").append(getTitle())
.append("\nboolean1: ").append(isBoolean1())
.append("\nnullTest: ").append(getNullTest())
.append("\nbaz: ").append(getBaz())
.append("\ncolor: ").append(_color).toString();
}
public void setTitle(String title)
{
_title = title;
}
public String getTitle()
{
return _title;
}
public void setNullTest(String nullTest)
{
assert (nullTest == null);
_nullTest = nullTest;
}
public String getNullTest()
{
return _nullTest;
}
public void setBaz(Baz baz)
{
_baz = baz;
}
public Baz getBaz()
{
return _baz;
}
public void setBoolean1(boolean boolean1)
{
_boolean1 = boolean1;
}
public boolean isBoolean1()
{
return _boolean1;
}
public void setBazs(Baz[] bazs)
{
_bazs = bazs;
}
public Baz[] getBazs()
{
return _bazs;
}
public Color getColor()
{
return _color;
}
public void setColor(Color color)
{
_color = color;
}
}
public static class Baz
{
private String _message;
private Foo _foo;
private Boolean _boolean2;
public Baz()
{
}
public Baz(String message, Boolean boolean2, Foo foo)
{
setMessage(message);
setBoolean2(boolean2);
setFoo(foo);
}
@Override
public String toString()
{
return new StringBuffer().append("\n=== ").append(getClass().getSimpleName()).append(" ===")
.append("\nmessage: ").append(getMessage())
.append("\nboolean2: ").append(isBoolean2())
.append("\nfoo: ").append(getFoo()).toString();
}
public void setMessage(String message)
{
_message = message;
}
public String getMessage()
{
return _message;
}
public void setFoo(Foo foo)
{
_foo = foo;
}
public Foo getFoo()
{
return _foo;
}
public void setBoolean2(Boolean boolean2)
{
_boolean2 = boolean2;
}
public Boolean isBoolean2()
{
return _boolean2;
}
}
public static class Foo
{
private String _name;
private int _int1;
private Integer _int2;
private long _long1;
private Long _long2;
private float _float1;
private Float _float2;
private double _double1;
private Double _double2;
public Foo()
{
}
@Override
public String toString()
{
return new StringBuffer().append("\n=== ").append(getClass().getSimpleName()).append(" ===")
.append("\nname: ").append(_name)
.append("\nint1: ").append(_int1)
.append("\nint2: ").append(_int2)
.append("\nlong1: ").append(_long1)
.append("\nlong2: ").append(_long2)
.append("\nfloat1: ").append(_float1)
.append("\nfloat2: ").append(_float2)
.append("\ndouble1: ").append(_double1)
.append("\ndouble2: ").append(_double2)
.toString();
}
@Override
public boolean equals(Object another)
{
if (another instanceof Foo)
{
Foo foo = (Foo)another;
return getName().equals(foo.getName()) &&
getInt1() == foo.getInt1() &&
getInt2().equals(foo.getInt2()) &&
getLong1() == foo.getLong1() &&
getLong2().equals(foo.getLong2()) &&
getFloat1() == foo.getFloat1() &&
getFloat2().equals(foo.getFloat2()) &&
getDouble1() == foo.getDouble1() &&
getDouble2().equals(foo.getDouble2());
}
return false;
}
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
public int getInt1()
{
return _int1;
}
public void setInt1(int int1)
{
_int1 = int1;
}
public Integer getInt2()
{
return _int2;
}
public void setInt2(Integer int2)
{
_int2 = int2;
}
public long getLong1()
{
return _long1;
}
public void setLong1(long long1)
{
_long1 = long1;
}
public Long getLong2()
{
return _long2;
}
public void setLong2(Long long2)
{
_long2 = long2;
}
public float getFloat1()
{
return _float1;
}
public void setFloat1(float float1)
{
_float1 = float1;
}
public Float getFloat2()
{
return _float2;
}
public void setFloat2(Float float2)
{
_float2 = float2;
}
public double getDouble1()
{
return _double1;
}
public void setDouble1(double double1)
{
_double1 = double1;
}
public Double getDouble2()
{
return _double2;
}
public void setDouble2(Double double2)
{
_double2 = double2;
}
}
} }

View File

@ -23,7 +23,7 @@ import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -39,20 +39,17 @@ public class JSONPojoConvertorTest
json.addConvertor(Foo.class, new JSONPojoConvertor(Foo.class)); json.addConvertor(Foo.class, new JSONPojoConvertor(Foo.class));
json.addConvertor(Bar.class, new JSONPojoConvertor(Bar.class)); json.addConvertor(Bar.class, new JSONPojoConvertor(Bar.class));
json.addConvertor(Baz.class, new JSONPojoConvertor(Baz.class)); json.addConvertor(Baz.class, new JSONPojoConvertor(Baz.class));
// json.addConvertor(Enum.class, new JSONEnumConvertor(true));
Foo foo = new Foo(); Foo foo = new Foo();
foo._name = "Foo @ " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); foo.setName("Foo @ " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime()));
foo._int1 = 1; foo.setInt1(1);
foo._int2 = 2; foo.setInt2(2);
foo._long1 = 1000001L; foo.setLong1(1000001L);
foo._long2 = 1000002L; foo.setLong2(1000002L);
foo._float1 = 10.11f; foo.setFloat1(10.11f);
foo._float2 = 10.22f; foo.setFloat2(10.22f);
foo._double1 = 10000.11111d; foo.setDouble1(10000.11111d);
foo._double2 = 10000.22222d; foo.setDouble2(10000.22222d);
foo._char1 = 'a';
foo._char2 = 'b';
Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo), new Baz[]{ Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo), new Baz[]{
new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null) new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null)
@ -60,19 +57,16 @@ public class JSONPojoConvertorTest
bar.setColor(Color.Green); bar.setColor(Color.Green);
String s = json.toJSON(bar); String s = json.toJSON(bar);
Object obj = json.parse(new JSON.StringSource(s)); Object obj = json.parse(new JSON.StringSource(s));
assertTrue(obj instanceof Bar); assertTrue(obj instanceof Bar);
Bar br = (Bar)obj; Bar br = (Bar)obj;
Baz bz = br.getBaz(); Baz bz = br.getBaz();
Foo f = bz.getFoo(); Foo f = bz.getFoo();
assertEquals(foo, f); assertEquals(foo, f);
assertTrue(br.getBazs().length == 2); assertEquals(2, br.getBazs().length);
assertEquals(br.getBazs()[0].getMessage(), "baz0"); assertEquals(br.getBazs()[0].getMessage(), "baz0");
assertEquals(br.getBazs()[1].getMessage(), "baz1"); assertEquals(br.getBazs()[1].getMessage(), "baz1");
assertEquals(Color.Green, br.getColor()); assertEquals(Color.Green, br.getColor());
@ -82,28 +76,22 @@ public class JSONPojoConvertorTest
public void testExclude() public void testExclude()
{ {
JSON json = new JSON(); JSON json = new JSON();
json.addConvertor(Foo.class, new JSONPojoConvertor(Foo.class, json.addConvertor(Foo.class, new JSONPojoConvertor(Foo.class, new String[]{"name", "long1", "int2"}));
new String[]{"name", "long1", "int2"})); json.addConvertor(Bar.class, new JSONPojoConvertor(Bar.class, new String[]{"title", "boolean1"}));
json.addConvertor(Bar.class, new JSONPojoConvertor(Bar.class, json.addConvertor(Baz.class, new JSONPojoConvertor(Baz.class, new String[]{"boolean2"}));
new String[]{"title", "boolean1"}));
json.addConvertor(Baz.class, new JSONPojoConvertor(Baz.class,
new String[]{"boolean2"}));
Foo foo = new Foo(); Foo foo = new Foo();
foo._name = "Foo @ " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); foo.setName("Foo @ " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime()));
foo._int1 = 1; foo.setInt1(1);
foo._int2 = 2; foo.setInt2(2);
foo._long1 = 1000001L; foo.setLong1(1000001L);
foo._long2 = 1000002L; foo.setLong2(1000002L);
foo._float1 = 10.11f; foo.setFloat1(10.11f);
foo._float2 = 10.22f; foo.setFloat2(10.22f);
foo._double1 = 10000.11111d; foo.setDouble1(10000.11111d);
foo._double2 = 10000.22222d; foo.setDouble2(10000.22222d);
foo._char1 = 'a';
foo._char2 = 'b';
Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo)); Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo));
// bar.setColor(Color.Blue);
String s = json.toJSON(bar); String s = json.toJSON(bar);
Object obj = json.parse(new JSON.StringSource(s)); Object obj = json.parse(new JSON.StringSource(s));
@ -115,350 +103,14 @@ public class JSONPojoConvertorTest
Foo f = bz.getFoo(); Foo f = bz.getFoo();
assertNull(br.getTitle()); assertNull(br.getTitle());
assertFalse(bar.getTitle().equals(br.getTitle())); assertNotEquals(bar.getTitle(), br.getTitle());
assertFalse(br.isBoolean1() == bar.isBoolean1()); assertNotEquals(br.isBoolean1(), bar.isBoolean1());
assertNull(bz.isBoolean2()); assertNull(bz.isBoolean2());
assertFalse(bar.getBaz().isBoolean2().equals(bz.isBoolean2())); assertNotEquals(bar.getBaz().isBoolean2(), bz.isBoolean2());
assertFalse(f.getLong1() == foo.getLong1()); assertNotEquals(f.getLong1(), foo.getLong1());
assertNull(f.getInt2()); assertNull(f.getInt2());
assertFalse(foo.getInt2().equals(f.getInt2())); assertNotEquals(foo.getInt2(), f.getInt2());
assertNull(f.getName()); assertNull(f.getName());
assertEquals(null, br.getColor()); assertNull(br.getColor());
}
enum Color
{
Red, Green, Blue
}
;
public static class Bar
{
private String _title;
private String _nullTest;
private Baz _baz;
private boolean _boolean1;
private Baz[] _bazs;
private Color _color;
public Bar()
{
}
public Bar(String title, boolean boolean1, Baz baz)
{
setTitle(title);
setBoolean1(boolean1);
setBaz(baz);
}
public Bar(String title, boolean boolean1, Baz baz, Baz[] bazs)
{
this(title, boolean1, baz);
setBazs(bazs);
}
@Override
public String toString()
{
return new StringBuffer()
.append("\n=== ").append(getClass().getSimpleName()).append(" ===")
.append("\ntitle: ").append(getTitle())
.append("\nboolean1: ").append(isBoolean1())
.append("\nnullTest: ").append(getNullTest())
.append("\nbaz: ").append(getBaz())
.append("\ncolor: ").append(_color).toString();
}
public void setTitle(String title)
{
_title = title;
}
public String getTitle()
{
return _title;
}
public void setNullTest(String nullTest)
{
assert (nullTest == null);
_nullTest = nullTest;
}
public String getNullTest()
{
return _nullTest;
}
public void setBaz(Baz baz)
{
_baz = baz;
}
public Baz getBaz()
{
return _baz;
}
public void setBoolean1(boolean boolean1)
{
_boolean1 = boolean1;
}
public boolean isBoolean1()
{
return _boolean1;
}
public void setBazs(Baz[] bazs)
{
_bazs = bazs;
}
public Baz[] getBazs()
{
return _bazs;
}
public Color getColor()
{
return _color;
}
public void setColor(Color color)
{
_color = color;
}
}
public static class Baz
{
private String _message;
private Foo _foo;
private Boolean _boolean2;
public Baz()
{
}
public Baz(String message, Boolean boolean2, Foo foo)
{
setMessage(message);
setBoolean2(boolean2);
setFoo(foo);
}
@Override
public String toString()
{
return new StringBuffer().append("\n=== ").append(getClass().getSimpleName()).append(" ===")
.append("\nmessage: ").append(getMessage())
.append("\nboolean2: ").append(isBoolean2())
.append("\nfoo: ").append(getFoo()).toString();
}
public void setMessage(String message)
{
_message = message;
}
public String getMessage()
{
return _message;
}
public void setFoo(Foo foo)
{
_foo = foo;
}
public Foo getFoo()
{
return _foo;
}
public void setBoolean2(Boolean boolean2)
{
_boolean2 = boolean2;
}
public Boolean isBoolean2()
{
return _boolean2;
}
}
public static class Foo
{
private String _name;
private int _int1;
private Integer _int2;
private long _long1;
private Long _long2;
private float _float1;
private Float _float2;
private double _double1;
private Double _double2;
private char _char1;
private Character _char2;
public Foo()
{
}
@Override
public String toString()
{
return new StringBuffer().append("\n=== ").append(getClass().getSimpleName()).append(" ===")
.append("\nname: ").append(_name)
.append("\nint1: ").append(_int1)
.append("\nint2: ").append(_int2)
.append("\nlong1: ").append(_long1)
.append("\nlong2: ").append(_long2)
.append("\nfloat1: ").append(_float1)
.append("\nfloat2: ").append(_float2)
.append("\ndouble1: ").append(_double1)
.append("\ndouble2: ").append(_double2)
.append("\nchar1: ").append(_char1)
.append("\nchar2: ").append(_char2)
.toString();
}
@Override
public boolean equals(Object another)
{
if (another instanceof Foo)
{
Foo foo = (Foo)another;
return getName().equals(foo.getName()) &&
getInt1() == foo.getInt1() &&
getInt2().equals(foo.getInt2()) &&
getLong1() == foo.getLong1() &&
getLong2().equals(foo.getLong2()) &&
getFloat1() == foo.getFloat1() &&
getFloat2().equals(foo.getFloat2()) &&
getDouble1() == foo.getDouble1() &&
getDouble2().equals(foo.getDouble2()) &&
getChar1() == foo.getChar1() &&
getChar2().equals(foo.getChar2());
}
return false;
}
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
public int getInt1()
{
return _int1;
}
public void setInt1(int int1)
{
_int1 = int1;
}
public Integer getInt2()
{
return _int2;
}
public void setInt2(Integer int2)
{
_int2 = int2;
}
public long getLong1()
{
return _long1;
}
public void setLong1(long long1)
{
_long1 = long1;
}
public Long getLong2()
{
return _long2;
}
public void setLong2(Long long2)
{
_long2 = long2;
}
public float getFloat1()
{
return _float1;
}
public void setFloat1(float float1)
{
_float1 = float1;
}
public Float getFloat2()
{
return _float2;
}
public void setFloat2(Float float2)
{
_float2 = float2;
}
public double getDouble1()
{
return _double1;
}
public void setDouble1(double double1)
{
_double1 = double1;
}
public Double getDouble2()
{
return _double2;
}
public void setDouble2(Double double2)
{
_double2 = double2;
}
public char getChar1()
{
return _char1;
}
public void setChar1(char char1)
{
_char1 = char1;
}
public Character getChar2()
{
return _char2;
}
public void setChar2(Character char2)
{
_char2 = char2;
}
} }
} }

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.util.ajax; package org.eclipse.jetty.util.ajax;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
@ -38,11 +37,14 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class JSONTest public class JSONTest
{ {
String test = "\n\n\n\t\t " + private JSON json;
private String test = "\n\n\n\t\t " +
"// ignore this ,a [ \" \n" + "// ignore this ,a [ \" \n" +
"/* and this \n" + "/* and this \n" +
"/* and * // this \n" + "/* and * // this \n" +
@ -62,17 +64,16 @@ public class JSONTest
@BeforeEach @BeforeEach
public void resetJSON() public void resetJSON()
{ {
// Reset JSON configuration to default with each testcase json = new JSON();
JSON.reset();
} }
@Test @Test
public void testToString() public void testToString()
{ {
JSON.registerConvertor(Gadget.class, new JSONObjectConvertor(false)); json.addConvertor(Gadget.class, new JSONObjectConvertor(false));
HashMap map = new HashMap(); Map<String, Object> map = new HashMap<>();
HashMap obj6 = new HashMap(); Map<String, Object> obj6 = new HashMap<>();
HashMap obj7 = new HashMap(); Map<String, Object> obj7 = new HashMap<>();
Woggle w0 = new Woggle(); Woggle w0 = new Woggle();
Woggle w1 = new Woggle(); Woggle w1 = new Woggle();
@ -97,26 +98,27 @@ public class JSONTest
obj7.put("x", "value"); obj7.put("x", "value");
String s = JSON.toString(map); String s = json.toJSON(map);
assertTrue(s.indexOf("\"n1\":null") >= 0); assertTrue(s.contains("\"n1\":null"));
assertTrue(s.indexOf("\"n2\":2") >= 0); assertTrue(s.contains("\"n2\":2"));
assertTrue(s.indexOf("\"n3\":-3.0E-11") >= 0); assertTrue(s.contains("\"n3\":-3.0E-11"));
assertTrue(s.indexOf("\"n4\":\"4\\n") >= 0); assertTrue(s.contains("\"n4\":\"4\\n"));
assertTrue(s.indexOf("\"n5\":[\"a\",\"b\",") >= 0); assertTrue(s.contains("\"n5\":[\"a\",\"b\","));
assertTrue(s.indexOf("\"n6\":{}") >= 0); assertTrue(s.contains("\"n6\":{}"));
assertTrue(s.indexOf("\"n7\":{\"x\":\"value\"}") >= 0); assertTrue(s.contains("\"n7\":{\"x\":\"value\"}"));
assertTrue(s.indexOf("\"n8\":[1,2,3,4]") >= 0); assertTrue(s.contains("\"n8\":[1,2,3,4]"));
assertTrue(s.indexOf("\"n9\":[{}, [], {}]") >= 0); assertTrue(s.contains("\"n9\":[{}, [], {}]"));
assertTrue(s.indexOf("\"w0\":{\"class\":\"org.eclipse.jetty.util.ajax.JSONTest$Woggle\",\"name\":\"woggle0\",\"nested\":{\"class\":\"org.eclipse.jetty.util.ajax.JSONTest$Woggle\",\"name\":\"woggle1\",\"nested\":null,\"number\":-101},\"number\":100}") >= 0); assertTrue(s.contains("\"w0\":{\"class\":\"org.eclipse.jetty.util.ajax.JSONTest$Woggle\",\"name\":\"woggle0\",\"nested\":{\"class\":\"org.eclipse.jetty.util.ajax.JSONTest$Woggle\",\"name\":\"woggle1\",\"nested\":null,\"number\":-101},\"number\":100}"));
Gadget gadget = new Gadget(); Gadget gadget = new Gadget();
gadget.setModulated(true);
gadget.setShields(42); gadget.setShields(42);
gadget.setWoggles(new Woggle[]{w0, w1}); gadget.setWoggles(new Woggle[]{w0, w1});
s = JSON.toString(new Gadget[]{gadget}); s = json.toJSON(new Gadget[]{gadget});
assertThat(s, startsWith("[")); assertThat(s, startsWith("["));
assertThat(s, containsString("\"modulated\":false")); assertThat(s, containsString("\"modulated\":" + gadget.isModulated()));
assertThat(s, containsString("\"shields\":42")); assertThat(s, containsString("\"shields\":" + gadget.getShields()));
assertThat(s, containsString("\"name\":\"woggle0\"")); assertThat(s, containsString("\"name\":\"woggle0\""));
assertThat(s, containsString("\"name\":\"woggle1\"")); assertThat(s, containsString("\"name\":\"woggle1\""));
} }
@ -124,7 +126,8 @@ public class JSONTest
@Test @Test
public void testParse() public void testParse()
{ {
Map map = (Map)JSON.parse(test); @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>)json.fromJSON(test);
assertEquals((long)100, map.get("onehundred")); assertEquals((long)100, map.get("onehundred"));
assertEquals("fred", map.get("name")); assertEquals("fred", map.get("name"));
assertEquals(-0.2, map.get("small")); assertEquals(-0.2, map.get("small"));
@ -133,12 +136,9 @@ public class JSONTest
assertTrue(((Woggle)map.get("w0")).nested instanceof Woggle); assertTrue(((Woggle)map.get("w0")).nested instanceof Woggle);
assertEquals(-101, ((Woggle)((Woggle)map.get("w0")).nested).number); assertEquals(-101, ((Woggle)((Woggle)map.get("w0")).nested).number);
assertTrue(map.containsKey("NaN")); assertTrue(map.containsKey("NaN"));
assertEquals(null, map.get("NaN")); assertNull(map.get("NaN"));
assertTrue(map.containsKey("undefined")); assertTrue(map.containsKey("undefined"));
assertEquals(null, map.get("undefined")); assertNull(map.get("undefined"));
test = "{\"data\":{\"source\":\"15831407eqdaawf7\",\"widgetId\":\"Magnet_8\"},\"channel\":\"/magnets/moveStart\",\"connectionId\":null,\"clientId\":\"15831407eqdaawf7\"}";
map = (Map)JSON.parse(test);
} }
@Test @Test
@ -146,7 +146,7 @@ public class JSONTest
{ {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("str", "line\nfeed"); map.put("str", "line\nfeed");
String jsonStr = JSON.toString(map); String jsonStr = json.toJSON(map);
assertThat(jsonStr, is("{\"str\":\"line\\nfeed\"}")); assertThat(jsonStr, is("{\"str\":\"line\\nfeed\"}"));
} }
@ -155,7 +155,7 @@ public class JSONTest
{ {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("str", "tab\tchar"); map.put("str", "tab\tchar");
String jsonStr = JSON.toString(map); String jsonStr = json.toJSON(map);
assertThat(jsonStr, is("{\"str\":\"tab\\tchar\"}")); assertThat(jsonStr, is("{\"str\":\"tab\\tchar\"}"));
} }
@ -164,7 +164,7 @@ public class JSONTest
{ {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("str", "ascii\u0007bel"); map.put("str", "ascii\u0007bel");
String jsonStr = JSON.toString(map); String jsonStr = json.toJSON(map);
assertThat(jsonStr, is("{\"str\":\"ascii\\u0007bel\"}")); assertThat(jsonStr, is("{\"str\":\"ascii\\u0007bel\"}"));
} }
@ -173,7 +173,7 @@ public class JSONTest
{ {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("str", "japanese: 桟橋"); map.put("str", "japanese: 桟橋");
String jsonStr = JSON.toString(map); String jsonStr = json.toJSON(map);
assertThat(jsonStr, is("{\"str\":\"japanese: 桟橋\"}")); assertThat(jsonStr, is("{\"str\":\"japanese: 桟橋\"}"));
} }
@ -199,7 +199,8 @@ public class JSONTest
public void testParse_Utf8_JsonEncoded() public void testParse_Utf8_JsonEncoded()
{ {
String jsonStr = "{\"str\": \"japanese: \\u685f\\u6a4b\"}"; String jsonStr = "{\"str\": \"japanese: \\u685f\\u6a4b\"}";
Map map = (Map)JSON.parse(jsonStr); @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>)json.fromJSON(jsonStr);
assertThat(map.get("str"), is("japanese: 桟橋")); assertThat(map.get("str"), is("japanese: 桟橋"));
} }
@ -207,7 +208,8 @@ public class JSONTest
public void testParse_Utf8_JavaEncoded() public void testParse_Utf8_JavaEncoded()
{ {
String jsonStr = "{\"str\": \"japanese: \u685f\u6a4b\"}"; String jsonStr = "{\"str\": \"japanese: \u685f\u6a4b\"}";
Map map = (Map)JSON.parse(jsonStr); @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>)json.fromJSON(jsonStr);
assertThat(map.get("str"), is("japanese: 桟橋")); assertThat(map.get("str"), is("japanese: 桟橋"));
} }
@ -215,23 +217,22 @@ public class JSONTest
public void testParse_Utf8_Raw() public void testParse_Utf8_Raw()
{ {
String jsonStr = "{\"str\": \"japanese: 桟橋\"}"; String jsonStr = "{\"str\": \"japanese: 桟橋\"}";
Map map = (Map)JSON.parse(jsonStr); @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>)json.fromJSON(jsonStr);
assertThat(map.get("str"), is("japanese: 桟橋")); assertThat(map.get("str"), is("japanese: 桟橋"));
} }
@Test @Test
public void testParseReader() throws Exception public void testParseReader()
{ {
Map map = (Map)JSON.parse(new StringReader(test)); @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>)json.fromJSON(test);
assertEquals((long)100, map.get("onehundred")); assertEquals((long)100, map.get("onehundred"));
assertEquals("fred", map.get("name")); assertEquals("fred", map.get("name"));
assertTrue(map.get("array").getClass().isArray()); assertTrue(map.get("array").getClass().isArray());
assertTrue(map.get("w0") instanceof Woggle); assertTrue(map.get("w0") instanceof Woggle);
assertTrue(((Woggle)map.get("w0")).nested instanceof Woggle); assertTrue(((Woggle)map.get("w0")).nested instanceof Woggle);
test = "{\"data\":{\"source\":\"15831407eqdaawf7\",\"widgetId\":\"Magnet_8\"},\"channel\":\"/magnets/moveStart\",\"connectionId\":null,\"clientId\":\"15831407eqdaawf7\"}";
map = (Map)JSON.parse(test);
} }
@Test @Test
@ -248,30 +249,31 @@ public class JSONTest
"\"array\" : [\"a\",-1.0e2,[],null,true,false] ," + "\"array\" : [\"a\",-1.0e2,[],null,true,false] ," +
"} */"; "} */";
Object o = JSON.parse(test, false); Object o = json.fromJSON(test);
assertTrue(o == null); assertNull(o);
o = JSON.parse(test, true); o = json.parse(new JSON.StringSource(test), true);
assertTrue(o instanceof Map); assertTrue(o instanceof Map);
assertEquals("fred", ((Map)o).get("name")); @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>)o;
assertEquals("fred", map.get("name"));
} }
@Test @Test
public void testQuote() public void testQuote()
{ {
String test = "\"abc123|\\\"|\\\\|\\/|\\b|\\f|\\n|\\r|\\t|\\uaaaa|\""; String test = "\"abc123|\\\"|\\\\|\\/|\\b|\\f|\\n|\\r|\\t|\\uaaaa|\"";
String result = (String)json.fromJSON(test);
String result = (String)JSON.parse(test, false);
assertEquals("abc123|\"|\\|/|\b|\f|\n|\r|\t|\uaaaa|", result); assertEquals("abc123|\"|\\|/|\b|\f|\n|\r|\t|\uaaaa|", result);
} }
@Test @Test
public void testBigDecimal() public void testBigDecimal()
{ {
Object obj = JSON.parse("1.0E7"); Object obj = json.fromJSON("1.0E7");
assertTrue(obj instanceof Double); assertTrue(obj instanceof Double);
BigDecimal bd = BigDecimal.valueOf(10000000d); BigDecimal bd = BigDecimal.valueOf(10000000d);
String string = JSON.toString(new Object[]{bd}); String string = json.toJSON(new Object[]{bd});
obj = Array.get(JSON.parse(string), 0); obj = Array.get(json.fromJSON(string), 0);
assertTrue(obj instanceof Double); assertTrue(obj instanceof Double);
} }
@ -279,7 +281,7 @@ public class JSONTest
public void testZeroByte() public void testZeroByte()
{ {
String withzero = "\u0000"; String withzero = "\u0000";
JSON.toString(withzero); assertEquals("\"\\u0000\"", json.toJSON(withzero));
} }
public static class Gadget public static class Gadget
@ -288,6 +290,8 @@ public class JSONTest
private long shields; private long shields;
private Woggle[] woggles; private Woggle[] woggles;
// Getters and setters required by the POJO convertor.
/** /**
* @return the modulated * @return the modulated
*/ */
@ -348,15 +352,15 @@ public class JSONTest
Woggle w0 = new Woggle(); Woggle w0 = new Woggle();
Gizmo g0 = new Gizmo(); Gizmo g0 = new Gizmo();
w0.name = "woggle0"; w0.setName("woggle0");
w0.nested = g0; w0.setNested(g0);
w0.number = 100; w0.setNumber(100);
g0.name = "woggle1"; g0.setName("woggle1");
g0.nested = null; g0.setNested(null);
g0.number = -101; g0.setNumber(-101);
g0.tested = true; g0.setTested(true);
HashMap map = new HashMap(); Map<String, Object> map = new HashMap<>();
Date dummyDate = new Date(1); Date dummyDate = new Date(1);
map.put("date", dummyDate); map.put("date", dummyDate);
map.put("w0", w0); map.put("w0", w0);
@ -365,29 +369,24 @@ public class JSONTest
json.append(buf, map); json.append(buf, map);
String js = buf.toString(); String js = buf.toString();
assertTrue(js.indexOf("\"date\":\"01/01/1970 00:00:00 GMT\"") >= 0); assertTrue(js.contains("\"date\":\"01/01/1970 00:00:00 GMT\""));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Woggle"));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertFalse(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Gizmo"));
assertTrue(js.indexOf("\"tested\":true") >= 0); assertTrue(js.contains("\"tested\":true"));
// test case#3 // test case#3
TimeZone tzone = TimeZone.getTimeZone("JST"); TimeZone tzone = TimeZone.getTimeZone("JST");
String tzone3Letter = tzone.getDisplayName(false, TimeZone.SHORT);
String format = "EEE MMMMM dd HH:mm:ss zzz yyyy"; String format = "EEE MMMMM dd HH:mm:ss zzz yyyy";
Locale l = new Locale("ja", "JP"); Locale l = new Locale("ja", "JP");
if (l != null) json.addConvertor(Date.class, new JSONDateConvertor(format, tzone, false, l));
{ buf = new StringBuffer();
json.addConvertor(Date.class, new JSONDateConvertor(format, tzone, false, l)); json.append(buf, map);
buf = new StringBuffer(); js = buf.toString();
json.append(buf, map); assertTrue(js.contains(" 01 09:00:00 JST 1970\""));
js = buf.toString(); assertTrue(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Woggle"));
//assertTrue(js.indexOf("\"date\":\"\u6728 1\u6708 01 09:00:00 JST 1970\"")>=0); assertFalse(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Gizmo"));
assertTrue(js.indexOf(" 01 09:00:00 JST 1970\"") >= 0); assertTrue(js.contains("\"tested\":true"));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
assertTrue(js.indexOf("\"tested\":true") >= 0);
}
// test case#4 // test case#4
json.addConvertor(Date.class, new JSONDateConvertor(true)); json.addConvertor(Date.class, new JSONDateConvertor(true));
@ -395,23 +394,17 @@ public class JSONTest
buf = new StringBuffer(); buf = new StringBuffer();
json.append(buf, map); json.append(buf, map);
js = buf.toString(); js = buf.toString();
assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0); assertFalse(js.contains("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\""));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Woggle"));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertFalse(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Gizmo"));
map = (HashMap)json.parse(new JSON.StringSource(js)); @SuppressWarnings("unchecked")
Map<String, Object> map1 = (Map<String, Object>)json.fromJSON(js);
assertTrue(map.get("date") instanceof Date); assertTrue(map1.get("date") instanceof Date);
assertTrue(map.get("w0") instanceof Woggle); assertTrue(map1.get("w0") instanceof Woggle);
} }
enum Color
{
Red, Green, Blue
}
;
@Test @Test
public void testEnumConvertor() public void testEnumConvertor()
{ {
@ -424,60 +417,66 @@ public class JSONTest
Woggle w0 = new Woggle(); Woggle w0 = new Woggle();
Gizmo g0 = new Gizmo(); Gizmo g0 = new Gizmo();
w0.name = "woggle0"; w0.setName("woggle0");
w0.nested = g0; w0.setNested(g0);
w0.number = 100; w0.setNumber(100);
w0.other = Color.Blue; w0.setOther(Color.Blue);
g0.name = "woggle1"; g0.setName("woggle1");
g0.nested = null; g0.setNested(null);
g0.number = -101; g0.setNumber(-101);
g0.tested = true; g0.setTested(true);
g0.other = Color.Green; g0.setOther(Color.Green);
HashMap map = new HashMap(); Map<String, Object> map = new HashMap<>();
map.put("date", new Date(1)); map.put("date", new Date(1));
map.put("w0", w0); map.put("w0", w0);
map.put("g0", g0); map.put("g0", g0);
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
json.append((Appendable)buf, map); json.append(buf, map);
String js = buf.toString(); String js = buf.toString();
assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") >= 0); assertTrue(js.contains("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\""));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Woggle"));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertFalse(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Gizmo"));
assertTrue(js.indexOf("\"tested\":true") >= 0); assertTrue(js.contains("\"tested\":true"));
assertTrue(js.indexOf("\"Green\"") >= 0); assertTrue(js.contains("\"Green\""));
assertTrue(js.indexOf("\"Blue\"") < 0); assertFalse(js.contains("\"Blue\""));
json.addConvertor(Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l)); json.addConvertor(Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l));
json.addConvertor(Enum.class, new JSONEnumConvertor(false)); json.addConvertor(Enum.class, new JSONEnumConvertor(false));
w0.nested = null; w0.nested = null;
buf = new StringBuffer(); buf = new StringBuffer();
json.append((Appendable)buf, map); json.append(buf, map);
js = buf.toString(); js = buf.toString();
assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0); assertFalse(js.contains("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\""));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Woggle"));
assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertFalse(js.contains("org.eclipse.jetty.util.ajax.JSONTest$Gizmo"));
Map map2 = (HashMap)json.parse(new JSON.StringSource(js)); @SuppressWarnings("unchecked")
Map<String, Object> map2 = (Map<String, Object>)json.fromJSON(js);
assertTrue(map2.get("date") instanceof Date); assertTrue(map2.get("date") instanceof Date);
assertTrue(map2.get("w0") instanceof Woggle); assertTrue(map2.get("w0") instanceof Woggle);
assertEquals(null, ((Woggle)map2.get("w0")).getOther()); assertNull(((Woggle)map2.get("w0")).other);
assertEquals(Color.Green.toString(), ((Map)map2.get("g0")).get("other")); @SuppressWarnings("unchecked")
Map<String, Object> map3 = (Map<String, Object>)map2.get("g0");
assertEquals(Color.Green.toString(), map3.get("other"));
json.addConvertor(Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l)); json.addConvertor(Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l));
json.addConvertor(Enum.class, new JSONEnumConvertor(true)); json.addConvertor(Enum.class, new JSONEnumConvertor(true));
buf = new StringBuffer(); buf = new StringBuffer();
json.append((Appendable)buf, map); json.append(buf, map);
js = buf.toString(); js = buf.toString();
map2 = (HashMap)json.parse(new JSON.StringSource(js)); @SuppressWarnings("unchecked")
Map<String, Object> map4 = (Map<String, Object>)json.fromJSON(js);
assertTrue(map2.get("date") instanceof Date); assertTrue(map4.get("date") instanceof Date);
assertTrue(map2.get("w0") instanceof Woggle); assertTrue(map4.get("w0") instanceof Woggle);
assertEquals(null, ((Woggle)map2.get("w0")).getOther()); assertNull(((Woggle)map4.get("w0")).other);
Object o = ((Map)map2.get("g0")).get("other"); @SuppressWarnings("unchecked")
Map<String, Object> map5 = (Map<String, Object>)map4.get("g0");
Object o = map5.get("other");
assertEquals(Color.Green, o); assertEquals(Color.Green, o);
} }
@ -489,41 +488,67 @@ public class JSONTest
boolean tested; boolean tested;
Object other; Object other;
// Getters and setters required by the POJO convertor.
public String getName() public String getName()
{ {
return name; return name;
} }
public void setName(String name)
{
this.name = name;
}
public Gizmo getNested() public Gizmo getNested()
{ {
return nested; return nested;
} }
public void setNested(Gizmo nested)
{
this.nested = nested;
}
public long getNumber() public long getNumber()
{ {
return number; return number;
} }
public void setNumber(long number)
{
this.number = number;
}
public boolean isTested() public boolean isTested()
{ {
return tested; return tested;
} }
public void setTested(boolean tested)
{
this.tested = tested;
}
public Object getOther() public Object getOther()
{ {
return other; return other;
} }
public void setOther(Object other)
{
this.other = other;
}
} }
public static class Woggle extends Gizmo implements JSON.Convertible public static class Woggle extends Gizmo implements JSON.Convertible
{ {
public Woggle() public Woggle()
{ {
} }
@Override @Override
public void fromJSON(Map object) public void fromJSON(Map<String, Object> object)
{ {
name = (String)object.get("name"); name = (String)object.get("name");
nested = (Gizmo)object.get("nested"); nested = (Gizmo)object.get("nested");