From e0c3340f5dc190522b8e54a2a2bfe3f02c8d1bfa Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 22 Jun 2016 10:34:46 +0200 Subject: [PATCH] Moved all non-static methods in a separate class SimpleMapperService The methods in SimpleMapper can be used static as they are, if the input is presented in form of an instance of JsonParser. An instance of SimpleMapperService can be configured, to use convenient methods that take the input in several forms. The SimpleMapperService has to be configured with an instance of JsonFactory, that is used internally, to construct the JsonParser from the given input. --- .../java/de/juplo/jackson/SimpleMapper.java | 550 +---------------- .../de/juplo/jackson/SimpleMapperService.java | 559 ++++++++++++++++++ 2 files changed, 563 insertions(+), 546 deletions(-) create mode 100644 src/main/java/de/juplo/jackson/SimpleMapperService.java diff --git a/src/main/java/de/juplo/jackson/SimpleMapper.java b/src/main/java/de/juplo/jackson/SimpleMapper.java index 0426720..f15bf25 100644 --- a/src/main/java/de/juplo/jackson/SimpleMapper.java +++ b/src/main/java/de/juplo/jackson/SimpleMapper.java @@ -1,15 +1,10 @@ package de.juplo.jackson; -import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonLocation; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; -import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.net.URL; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; @@ -33,401 +28,12 @@ import org.slf4j.LoggerFactory; * * @author kai */ -public class SimpleMapper +public abstract class SimpleMapper { private static final Logger LOG = LoggerFactory.getLogger(SimpleMapper.class); - private final JsonFactory factory; - - - public SimpleMapper(JsonFactory factory) - { - this.factory = factory; - } - - - public Spliterator getArraySpliterator(File file) - throws - IOException - { - return getArraySpliterator(factory.createParser(file)); - } - - public Spliterator getArraySpliterator(InputStream is) - throws - IOException - { - return getArraySpliterator(factory.createParser(is)); - } - - public Spliterator getArraySpliterator(Reader r) - throws - IOException - { - return getArraySpliterator(factory.createParser(r)); - } - - public Spliterator getArraySpliterator(String content) - throws - IOException - { - return getArraySpliterator(factory.createParser(content)); - } - - public Spliterator getArraySpliterator(URL url) - throws - IOException - { - return getArraySpliterator(factory.createParser(url)); - } - - public Spliterator getArraySpliterator(byte[] data) - throws - IOException - { - return getArraySpliterator(factory.createParser(data)); - } - - public Spliterator getArraySpliterator(char[] content) - throws - IOException - { - return getArraySpliterator(factory.createParser(content)); - } - - public Spliterator getArraySpliterator(byte[] data, int offset, int len) - throws - IOException - { - return getArraySpliterator(factory.createParser(data, offset, len)); - } - - public Spliterator getArraySpliterator(char[] data, int offset, int len) - throws - IOException - { - return getArraySpliterator(factory.createParser(data, offset, len)); - } - - public Stream getArrayStream(File file) - throws - IOException - { - return getArrayStream(factory.createParser(file)); - } - - public Stream getArrayStream(InputStream is) - throws - IOException - { - return getArrayStream(factory.createParser(is)); - } - - public Stream getArrayStream(Reader r) - throws - IOException - { - return getArrayStream(factory.createParser(r)); - } - - public Stream getArrayStream(String content) - throws - IOException - { - return getArrayStream(factory.createParser(content)); - } - - public Stream getArrayStream(URL url) - throws - IOException - { - return getArrayStream(factory.createParser(url)); - } - - public Stream getArrayStream(byte[] data) - throws - IOException - { - return getArrayStream(factory.createParser(data)); - } - - public Stream getArrayStream(char[] content) - throws - IOException - { - return getArrayStream(factory.createParser(content)); - } - - public Stream getArrayStream(byte[] data, int offset, int len) - throws - IOException - { - return getArrayStream(factory.createParser(data, offset, len)); - } - - public Stream getArrayStream(char[] data, int offset, int len) - throws - IOException - { - return getArrayStream(factory.createParser(data, offset, len)); - } - - public Iterator getArrayIterator(File file) - throws - IOException - { - return getArrayIterator(factory.createParser(file)); - } - - public Iterator getArrayIterator(InputStream is) - throws - IOException - { - return getArrayIterator(factory.createParser(is)); - } - - public Iterator getArrayIterator(Reader r) - throws - IOException - { - return getArrayIterator(factory.createParser(r)); - } - - public Iterator getArrayIterator(String content) - throws - IOException - { - return getArrayIterator(factory.createParser(content)); - } - - public Iterator getArrayIterator(URL url) - throws - IOException - { - return getArrayIterator(factory.createParser(url)); - } - - public Iterator getArrayIterator(byte[] data) - throws - IOException - { - return getArrayIterator(factory.createParser(data)); - } - - public Iterator getArrayIterator(char[] content) - throws - IOException - { - return getArrayIterator(factory.createParser(content)); - } - - public Iterator getArrayIterator(byte[] data, int offset, int len) - throws - IOException - { - return getArrayIterator(factory.createParser(data, offset, len)); - } - - public Iterator getArrayIterator(char[] data, int offset, int len) - throws - IOException - { - return getArrayIterator(factory.createParser(data, offset, len)); - } - - - public Spliterator> getObjectSpliterator(File file) - throws - IOException - { - return getObjectSpliterator(factory.createParser(file)); - } - - public Spliterator> getObjectSpliterator(InputStream is) - throws - IOException - { - return getObjectSpliterator(factory.createParser(is)); - } - - public Spliterator> getObjectSpliterator(Reader r) - throws - IOException - { - return getObjectSpliterator(factory.createParser(r)); - } - - public Spliterator> getObjectSpliterator(String content) - throws - IOException - { - return getObjectSpliterator(factory.createParser(content)); - } - - public Spliterator> getObjectSpliterator(URL url) - throws - IOException - { - return getObjectSpliterator(factory.createParser(url)); - } - - public Spliterator> getObjectSpliterator(byte[] data) - throws - IOException - { - return getObjectSpliterator(factory.createParser(data)); - } - - public Spliterator> getObjectSpliterator(char[] content) - throws - IOException - { - return getObjectSpliterator(factory.createParser(content)); - } - - public Spliterator> getObjectSpliterator(byte[] data, int offset, int len) - throws - IOException - { - return getObjectSpliterator(factory.createParser(data, offset, len)); - } - - public Spliterator> getObjectSpliterator(char[] data, int offset, int len) - throws - IOException - { - return getObjectSpliterator(factory.createParser(data, offset, len)); - } - - public Stream> getObjectStream(File file) - throws - IOException - { - return getObjectStream(factory.createParser(file)); - } - - public Stream> getObjectStream(InputStream is) - throws - IOException - { - return getObjectStream(factory.createParser(is)); - } - - public Stream> getObjectStream(Reader r) - throws - IOException - { - return getObjectStream(factory.createParser(r)); - } - - public Stream> getObjectStream(String content) - throws - IOException - { - return getObjectStream(factory.createParser(content)); - } - - public Stream> getObjectStream(URL url) - throws - IOException - { - return getObjectStream(factory.createParser(url)); - } - - public Stream> getObjectStream(byte[] data) - throws - IOException - { - return getObjectStream(factory.createParser(data)); - } - - public Stream> getObjectStream(char[] content) - throws - IOException - { - return getObjectStream(factory.createParser(content)); - } - - public Stream> getObjectStream(byte[] data, int offset, int len) - throws - IOException - { - return getObjectStream(factory.createParser(data, offset, len)); - } - - public Stream> getObjectStream(char[] data, int offset, int len) - throws - IOException - { - return getObjectStream(factory.createParser(data, offset, len)); - } - - public Iterator> getObjectIterator(File file) - throws - IOException - { - return getObjectIterator(factory.createParser(file)); - } - - public Iterator> getObjectIterator(InputStream is) - throws - IOException - { - return getObjectIterator(factory.createParser(is)); - } - - public Iterator> getObjectIterator(Reader r) - throws - IOException - { - return getObjectIterator(factory.createParser(r)); - } - - public Iterator> getObjectIterator(String content) - throws - IOException - { - return getObjectIterator(factory.createParser(content)); - } - - public Iterator> getObjectIterator(URL url) - throws - IOException - { - return getObjectIterator(factory.createParser(url)); - } - - public Iterator> getObjectIterator(byte[] data) - throws - IOException - { - return getObjectIterator(factory.createParser(data)); - } - - public Iterator> getObjectIterator(char[] content) - throws - IOException - { - return getObjectIterator(factory.createParser(content)); - } - - public Iterator> getObjectIterator(byte[] data, int offset, int len) - throws - IOException - { - return getObjectIterator(factory.createParser(data, offset, len)); - } - - public Iterator> getObjectIterator(char[] data, int offset, int len) - throws - IOException - { - return getObjectIterator(factory.createParser(data, offset, len)); - } - - public static Spliterator getArraySpliterator(final JsonParser parser) throws IOException @@ -666,155 +272,7 @@ public class SimpleMapper } - public List convertArray(File file) throws Exception - { - return convertArray(factory.createParser(file)); - } - - public List convertArray(InputStream is) throws Exception - { - return convertArray(factory.createParser(is)); - } - - public List convertArray(Reader r) throws Exception - { - return convertArray(factory.createParser(r)); - } - - public List convertArray(String content) throws Exception - { - return convertArray(factory.createParser(content)); - } - - public List convertArray(URL url) throws Exception - { - return convertArray(factory.createParser(url)); - } - - public List convertArray(byte[] data) throws Exception - { - return convertArray(factory.createParser(data)); - } - - public List convertArray(char[] content) throws Exception - { - return convertArray(factory.createParser(content)); - } - - public List convertArray(byte[] data, int offset, int len) - throws - Exception - { - return convertArray(factory.createParser(data, offset, len)); - } - - public List convertArray(char[] content, int offset, int len) - throws - Exception - { - return convertArray(factory.createParser(content, offset, len)); - } - - public Map convertObject(File file) throws Exception - { - return convertObject(factory.createParser(file)); - } - - public Map convertObject(InputStream is) throws Exception - { - return convertObject(factory.createParser(is)); - } - - public Map convertObject(Reader r) throws Exception - { - return convertObject(factory.createParser(r)); - } - - public Map convertObject(String content) throws Exception - { - return convertObject(factory.createParser(content)); - } - - public Map convertObject(URL url) throws Exception - { - return convertObject(factory.createParser(url)); - } - - public Map convertObject(byte[] data) throws Exception - { - return convertObject(factory.createParser(data)); - } - - public Map convertObject(char[] content) throws Exception - { - return convertObject(factory.createParser(content)); - } - - public Map convertObject(byte[] data, int offset, int len) - throws - Exception - { - return convertObject(factory.createParser(data, offset, len)); - } - - public Map convertObject(char[] content, int offset, int len) - throws - Exception - { - return convertObject(factory.createParser(content, offset, len)); - } - - public Object convert(File file) throws Exception - { - return convert(factory.createParser(file)); - } - - public Object convert(InputStream is) throws Exception - { - return convert(factory.createParser(is)); - } - - public Object convert(Reader r) throws Exception - { - return convert(factory.createParser(r)); - } - - public Object convert(String content) throws Exception - { - return convert(factory.createParser(content)); - } - - public Object convert(URL url) throws Exception - { - return convert(factory.createParser(url)); - } - - public Object convert(byte[] data) throws Exception - { - return convert(factory.createParser(data)); - } - - public Object convert(char[] content) throws Exception - { - return convert(factory.createParser(content)); - } - - public Object convert(byte[] data, int offset, int len) - throws - Exception - { - return convert(factory.createParser(data, offset, len)); - } - - public Object convert(char[] content, int offset, int len) - throws - Exception - { - return convert(factory.createParser(content, offset, len)); - } - - - static List convertArray(JsonParser parser) throws IOException + public static List convertArray(JsonParser parser) throws IOException { JsonToken token = parser.nextToken(); @@ -835,7 +293,7 @@ public class SimpleMapper return array; } - static Map convertObject(JsonParser parser) throws IOException + public static Map convertObject(JsonParser parser) throws IOException { JsonToken token = parser.nextToken(); @@ -856,7 +314,7 @@ public class SimpleMapper return object; } - static Object convert(JsonParser parser) throws IOException + public static Object convert(JsonParser parser) throws IOException { JsonToken token = parser.nextToken(); diff --git a/src/main/java/de/juplo/jackson/SimpleMapperService.java b/src/main/java/de/juplo/jackson/SimpleMapperService.java new file mode 100644 index 0000000..3c1e197 --- /dev/null +++ b/src/main/java/de/juplo/jackson/SimpleMapperService.java @@ -0,0 +1,559 @@ +package de.juplo.jackson; + + +import com.fasterxml.jackson.core.JsonFactory; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.net.URL; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Spliterator; +import java.util.stream.Stream; + + + +/** + * + * @author kai + */ +public class SimpleMapperService extends SimpleMapper +{ + private final JsonFactory factory; + + + public SimpleMapperService(JsonFactory factory) + { + this.factory = factory; + } + + + public Spliterator getArraySpliterator(File file) + throws + IOException + { + return getArraySpliterator(factory.createParser(file)); + } + + public Spliterator getArraySpliterator(InputStream is) + throws + IOException + { + return getArraySpliterator(factory.createParser(is)); + } + + public Spliterator getArraySpliterator(Reader r) + throws + IOException + { + return getArraySpliterator(factory.createParser(r)); + } + + public Spliterator getArraySpliterator(String content) + throws + IOException + { + return getArraySpliterator(factory.createParser(content)); + } + + public Spliterator getArraySpliterator(URL url) + throws + IOException + { + return getArraySpliterator(factory.createParser(url)); + } + + public Spliterator getArraySpliterator(byte[] data) + throws + IOException + { + return getArraySpliterator(factory.createParser(data)); + } + + public Spliterator getArraySpliterator(char[] content) + throws + IOException + { + return getArraySpliterator(factory.createParser(content)); + } + + public Spliterator getArraySpliterator(byte[] data, int offset, int len) + throws + IOException + { + return getArraySpliterator(factory.createParser(data, offset, len)); + } + + public Spliterator getArraySpliterator(char[] data, int offset, int len) + throws + IOException + { + return getArraySpliterator(factory.createParser(data, offset, len)); + } + + public Stream getArrayStream(File file) + throws + IOException + { + return getArrayStream(factory.createParser(file)); + } + + public Stream getArrayStream(InputStream is) + throws + IOException + { + return getArrayStream(factory.createParser(is)); + } + + public Stream getArrayStream(Reader r) + throws + IOException + { + return getArrayStream(factory.createParser(r)); + } + + public Stream getArrayStream(String content) + throws + IOException + { + return getArrayStream(factory.createParser(content)); + } + + public Stream getArrayStream(URL url) + throws + IOException + { + return getArrayStream(factory.createParser(url)); + } + + public Stream getArrayStream(byte[] data) + throws + IOException + { + return getArrayStream(factory.createParser(data)); + } + + public Stream getArrayStream(char[] content) + throws + IOException + { + return getArrayStream(factory.createParser(content)); + } + + public Stream getArrayStream(byte[] data, int offset, int len) + throws + IOException + { + return getArrayStream(factory.createParser(data, offset, len)); + } + + public Stream getArrayStream(char[] data, int offset, int len) + throws + IOException + { + return getArrayStream(factory.createParser(data, offset, len)); + } + + public Iterator getArrayIterator(File file) + throws + IOException + { + return getArrayIterator(factory.createParser(file)); + } + + public Iterator getArrayIterator(InputStream is) + throws + IOException + { + return getArrayIterator(factory.createParser(is)); + } + + public Iterator getArrayIterator(Reader r) + throws + IOException + { + return getArrayIterator(factory.createParser(r)); + } + + public Iterator getArrayIterator(String content) + throws + IOException + { + return getArrayIterator(factory.createParser(content)); + } + + public Iterator getArrayIterator(URL url) + throws + IOException + { + return getArrayIterator(factory.createParser(url)); + } + + public Iterator getArrayIterator(byte[] data) + throws + IOException + { + return getArrayIterator(factory.createParser(data)); + } + + public Iterator getArrayIterator(char[] content) + throws + IOException + { + return getArrayIterator(factory.createParser(content)); + } + + public Iterator getArrayIterator(byte[] data, int offset, int len) + throws + IOException + { + return getArrayIterator(factory.createParser(data, offset, len)); + } + + public Iterator getArrayIterator(char[] data, int offset, int len) + throws + IOException + { + return getArrayIterator(factory.createParser(data, offset, len)); + } + + + public Spliterator> getObjectSpliterator(File file) + throws + IOException + { + return getObjectSpliterator(factory.createParser(file)); + } + + public Spliterator> getObjectSpliterator(InputStream is) + throws + IOException + { + return getObjectSpliterator(factory.createParser(is)); + } + + public Spliterator> getObjectSpliterator(Reader r) + throws + IOException + { + return getObjectSpliterator(factory.createParser(r)); + } + + public Spliterator> getObjectSpliterator(String content) + throws + IOException + { + return getObjectSpliterator(factory.createParser(content)); + } + + public Spliterator> getObjectSpliterator(URL url) + throws + IOException + { + return getObjectSpliterator(factory.createParser(url)); + } + + public Spliterator> getObjectSpliterator(byte[] data) + throws + IOException + { + return getObjectSpliterator(factory.createParser(data)); + } + + public Spliterator> getObjectSpliterator(char[] content) + throws + IOException + { + return getObjectSpliterator(factory.createParser(content)); + } + + public Spliterator> getObjectSpliterator(byte[] data, int offset, int len) + throws + IOException + { + return getObjectSpliterator(factory.createParser(data, offset, len)); + } + + public Spliterator> getObjectSpliterator(char[] data, int offset, int len) + throws + IOException + { + return getObjectSpliterator(factory.createParser(data, offset, len)); + } + + public Stream> getObjectStream(File file) + throws + IOException + { + return getObjectStream(factory.createParser(file)); + } + + public Stream> getObjectStream(InputStream is) + throws + IOException + { + return getObjectStream(factory.createParser(is)); + } + + public Stream> getObjectStream(Reader r) + throws + IOException + { + return getObjectStream(factory.createParser(r)); + } + + public Stream> getObjectStream(String content) + throws + IOException + { + return getObjectStream(factory.createParser(content)); + } + + public Stream> getObjectStream(URL url) + throws + IOException + { + return getObjectStream(factory.createParser(url)); + } + + public Stream> getObjectStream(byte[] data) + throws + IOException + { + return getObjectStream(factory.createParser(data)); + } + + public Stream> getObjectStream(char[] content) + throws + IOException + { + return getObjectStream(factory.createParser(content)); + } + + public Stream> getObjectStream(byte[] data, int offset, int len) + throws + IOException + { + return getObjectStream(factory.createParser(data, offset, len)); + } + + public Stream> getObjectStream(char[] data, int offset, int len) + throws + IOException + { + return getObjectStream(factory.createParser(data, offset, len)); + } + + public Iterator> getObjectIterator(File file) + throws + IOException + { + return getObjectIterator(factory.createParser(file)); + } + + public Iterator> getObjectIterator(InputStream is) + throws + IOException + { + return getObjectIterator(factory.createParser(is)); + } + + public Iterator> getObjectIterator(Reader r) + throws + IOException + { + return getObjectIterator(factory.createParser(r)); + } + + public Iterator> getObjectIterator(String content) + throws + IOException + { + return getObjectIterator(factory.createParser(content)); + } + + public Iterator> getObjectIterator(URL url) + throws + IOException + { + return getObjectIterator(factory.createParser(url)); + } + + public Iterator> getObjectIterator(byte[] data) + throws + IOException + { + return getObjectIterator(factory.createParser(data)); + } + + public Iterator> getObjectIterator(char[] content) + throws + IOException + { + return getObjectIterator(factory.createParser(content)); + } + + public Iterator> getObjectIterator(byte[] data, int offset, int len) + throws + IOException + { + return getObjectIterator(factory.createParser(data, offset, len)); + } + + public Iterator> getObjectIterator(char[] data, int offset, int len) + throws + IOException + { + return getObjectIterator(factory.createParser(data, offset, len)); + } + + + public List convertArray(File file) throws Exception + { + return convertArray(factory.createParser(file)); + } + + public List convertArray(InputStream is) throws Exception + { + return convertArray(factory.createParser(is)); + } + + public List convertArray(Reader r) throws Exception + { + return convertArray(factory.createParser(r)); + } + + public List convertArray(String content) throws Exception + { + return convertArray(factory.createParser(content)); + } + + public List convertArray(URL url) throws Exception + { + return convertArray(factory.createParser(url)); + } + + public List convertArray(byte[] data) throws Exception + { + return convertArray(factory.createParser(data)); + } + + public List convertArray(char[] content) throws Exception + { + return convertArray(factory.createParser(content)); + } + + public List convertArray(byte[] data, int offset, int len) + throws + Exception + { + return convertArray(factory.createParser(data, offset, len)); + } + + public List convertArray(char[] content, int offset, int len) + throws + Exception + { + return convertArray(factory.createParser(content, offset, len)); + } + + public Map convertObject(File file) throws Exception + { + return convertObject(factory.createParser(file)); + } + + public Map convertObject(InputStream is) throws Exception + { + return convertObject(factory.createParser(is)); + } + + public Map convertObject(Reader r) throws Exception + { + return convertObject(factory.createParser(r)); + } + + public Map convertObject(String content) throws Exception + { + return convertObject(factory.createParser(content)); + } + + public Map convertObject(URL url) throws Exception + { + return convertObject(factory.createParser(url)); + } + + public Map convertObject(byte[] data) throws Exception + { + return convertObject(factory.createParser(data)); + } + + public Map convertObject(char[] content) throws Exception + { + return convertObject(factory.createParser(content)); + } + + public Map convertObject(byte[] data, int offset, int len) + throws + Exception + { + return convertObject(factory.createParser(data, offset, len)); + } + + public Map convertObject(char[] content, int offset, int len) + throws + Exception + { + return convertObject(factory.createParser(content, offset, len)); + } + + public Object convert(File file) throws Exception + { + return convert(factory.createParser(file)); + } + + public Object convert(InputStream is) throws Exception + { + return convert(factory.createParser(is)); + } + + public Object convert(Reader r) throws Exception + { + return convert(factory.createParser(r)); + } + + public Object convert(String content) throws Exception + { + return convert(factory.createParser(content)); + } + + public Object convert(URL url) throws Exception + { + return convert(factory.createParser(url)); + } + + public Object convert(byte[] data) throws Exception + { + return convert(factory.createParser(data)); + } + + public Object convert(char[] content) throws Exception + { + return convert(factory.createParser(content)); + } + + public Object convert(byte[] data, int offset, int len) + throws + Exception + { + return convert(factory.createParser(data, offset, len)); + } + + public Object convert(char[] content, int offset, int len) + throws + Exception + { + return convert(factory.createParser(content, offset, len)); + } +} -- 2.20.1