Skip to content

Commit

Permalink
Issue #6558 - Allow configuring return type in JSON array parsing.
Browse files Browse the repository at this point in the history
Updated JSON implementation to keep backward compatibility
by calling newArray(), now deprecated.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
(cherry picked from commit 1d542be)
  • Loading branch information
sbordet committed Sep 27, 2021
1 parent 52f125f commit d73535c
Showing 1 changed file with 10 additions and 1 deletion.
Expand Up @@ -27,6 +27,7 @@
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.IntStream;

import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.TypeUtil;
Expand Down Expand Up @@ -84,7 +85,7 @@ public class JSON

private final Map<String, Convertor> _convertors = new ConcurrentHashMap<>();
private int _stringBufferSize = 1024;
private Function<List<?>, Object> _arrayConverter = List::toArray;
private Function<List<?>, Object> _arrayConverter = this::defaultArrayConverter;

/**
* @return the initial stringBuffer size to use when creating JSON strings
Expand Down Expand Up @@ -937,6 +938,14 @@ protected Object parseObject(Source source)
return map;
}

private Object defaultArrayConverter(List<?> list)
{
// Call newArray() to keep backward compatibility.
Object[] objects = newArray(list.size());
IntStream.range(0, list.size()).forEach(i -> objects[i] = list.get(i));
return objects;
}

protected Object parseArray(Source source)
{
if (source.next() != '[')
Expand Down

0 comments on commit d73535c

Please sign in to comment.