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