001package io.ebean.text.json;
002
003import com.fasterxml.jackson.core.JsonGenerator;
004import com.fasterxml.jackson.core.JsonParser;
005import com.fasterxml.jackson.core.JsonToken;
006import io.ebean.service.SpiJsonService;
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.Writer;
011import java.util.Collection;
012import java.util.Iterator;
013import java.util.List;
014import java.util.Map;
015import java.util.ServiceLoader;
016import java.util.Set;
017
018/**
019 * Utility that converts between JSON content and simple java Maps/Lists.
020 */
021public class EJson {
022
023  private static SpiJsonService plugin = init();
024
025  private static SpiJsonService init() {
026
027    Iterator<SpiJsonService> loader = ServiceLoader.load(SpiJsonService.class).iterator();
028    if (loader.hasNext()) {
029      return loader.next();
030    }
031    throw new IllegalStateException("No service implementation found for SpiJsonService?");
032  }
033
034  /**
035   * Write the nested Map/List as json.
036   */
037  public static String write(Object object) throws IOException {
038    return plugin.write(object);
039  }
040
041  /**
042   * Write the nested Map/List as json to the writer.
043   */
044  public static void write(Object object, Writer writer) throws IOException {
045    plugin.write(object, writer);
046  }
047
048  /**
049   * Write the nested Map/List as json to the jsonGenerator.
050   */
051  public static void write(Object object, JsonGenerator jsonGenerator) throws IOException {
052    plugin.write(object, jsonGenerator);
053  }
054
055  /**
056   * Write the collection as json array to the jsonGenerator.
057   */
058  public static void writeCollection(Collection<Object> collection, JsonGenerator jsonGenerator) throws IOException {
059    plugin.writeCollection(collection, jsonGenerator);
060  }
061
062  /**
063   * Parse the json and return as a Map additionally specifying if the returned map should
064   * be modify aware meaning that it can detect when it has been modified.
065   */
066  public static Map<String, Object> parseObject(String json, boolean modifyAware) throws IOException {
067    return plugin.parseObject(json, modifyAware);
068  }
069
070  /**
071   * Parse the json and return as a Map.
072   */
073  public static Map<String, Object> parseObject(String json) throws IOException {
074    return plugin.parseObject(json);
075  }
076
077  /**
078   * Parse the json and return as a Map taking a reader.
079   */
080  public static Map<String, Object> parseObject(Reader reader, boolean modifyAware) throws IOException {
081    return plugin.parseObject(reader, modifyAware);
082  }
083
084  /**
085   * Parse the json and return as a Map taking a reader.
086   */
087  public static Map<String, Object> parseObject(Reader reader) throws IOException {
088    return plugin.parseObject(reader);
089  }
090
091  /**
092   * Parse the json and return as a Map taking a JsonParser.
093   */
094  public static Map<String, Object> parseObject(JsonParser parser) throws IOException {
095    return plugin.parseObject(parser);
096  }
097
098  /**
099   * Parse the json and return as a Map taking a JsonParser and a starting token.
100   * <p>
101   * Used when the first token is checked to see if the value is null prior to calling this.
102   * </p>
103   */
104  public static Map<String, Object> parseObject(JsonParser parser, JsonToken token) throws IOException {
105    return plugin.parseObject(parser, token);
106  }
107
108  /**
109   * Parse the json and return as a modify aware List.
110   */
111  public static <T> List<T> parseList(String json, boolean modifyAware) throws IOException {
112    return plugin.parseList(json, modifyAware);
113  }
114
115  /**
116   * Parse the json and return as a List.
117   */
118  public static List<Object> parseList(String json) throws IOException {
119    return plugin.parseList(json);
120  }
121
122  /**
123   * Parse the json and return as a List taking a Reader.
124   */
125  public static List<Object> parseList(Reader reader) throws IOException {
126    return plugin.parseList(reader);
127  }
128
129  /**
130   * Parse the json and return as a List taking a JsonParser.
131   */
132  public static List<Object> parseList(JsonParser parser) throws IOException {
133    return plugin.parseList(parser);
134  }
135
136  /**
137   * Parse the json returning as a List taking into account the current token.
138   */
139  public static <T> List<T> parseList(JsonParser parser, JsonToken currentToken) throws IOException {
140    return plugin.parseList(parser, currentToken);
141  }
142
143  /**
144   * Parse the json and return as a List or Map.
145   */
146  public static Object parse(String json) throws IOException {
147    return plugin.parse(json);
148  }
149
150  /**
151   * Parse the json and return as a List or Map.
152   */
153  public static Object parse(Reader reader) throws IOException {
154    return plugin.parse(reader);
155  }
156
157  /**
158   * Parse the json and return as a List or Map.
159   */
160  public static Object parse(JsonParser parser) throws IOException {
161    return plugin.parse(parser);
162  }
163
164  /**
165   * Parse the json returning a Set that might be modify aware.
166   */
167  public static <T> Set<T> parseSet(String json, boolean modifyAware) throws IOException {
168    return plugin.parseSet(json, modifyAware);
169  }
170
171  /**
172   * Parse the json returning as a Set taking into account the current token.
173   */
174  public static <T> Set<T> parseSet(JsonParser parser, JsonToken currentToken) throws IOException {
175    return plugin.parseSet(parser, currentToken);
176  }
177}