001package squidpony; 002 003import com.badlogic.gdx.files.FileHandle; 004import com.badlogic.gdx.utils.JsonWriter; 005import squidpony.store.json.JsonCompressor; 006 007import java.io.InputStream; 008import java.io.Reader; 009import java.io.Writer; 010 011/** 012 * A variant of {@link DataConverter} (and an extension of libGDX's {@link com.badlogic.gdx.utils.Json} class) that 013 * compresses its JSON output and reads compressed input. Due to limits on the String compression library this uses 014 * (namely, it only compresses Strings, so input must be able to be interpreted as a String), this only allows String 015 * and FileHandle input formats, and throws exceptions if you try to deserialize a char array, InputStream, or Reader 016 * with fromJson() . Otherwise, it acts like DataConverter, so the same docs apply: 017 * <br> 018 * Augmented version of LibGDX's Json class that knows how to handle various data types common in SquidLib. 019 * This includes OrderedMap, which notably allows non-String keys (LibGDX's default Map serializer requires keys to be 020 * Strings), but does not currently allow the IHasher to be set (which only should affect OrderedMaps with array keys). 021 * It also makes significantly shorter serialized output for 2D char arrays, GreasedRegion and FakeLanguageGen objects, 022 * and various collections (IntDoubleOrderedMap, IntVLA, Arrangement, K2, and K2V1 at least). 023 * Created by Tommy Ettinger on 1/9/2017. 024 */ 025public class DataCompressor extends JsonCompressor { 026 public DataCompressor() { 027 super(); 028 } 029 030 public DataCompressor(JsonWriter.OutputType outputType) { 031 super(outputType); 032 } 033 034 /** 035 * @param object The object to serialize 036 * @param knownType May be null if the type is unknown. 037 * @param elementType May be null if the type is unknown. 038 */ 039 @Override 040 public String toJson(Object object, Class knownType, Class elementType) { 041 return super.toJson(object, knownType, elementType); 042 } 043 044 /** 045 * @param object The object to serialize 046 * @param knownType May be null if the type is unknown. 047 * @param elementType May be null if the type is unknown. 048 * @param file A LibGDX FileHandle that can be written to; overwrites, does not append 049 */ 050 @Override 051 public void toJson (Object object, Class knownType, Class elementType, FileHandle file) { 052 super.toJson(object, knownType, elementType, file); 053 } 054 055 /** 056 * Don't use this, please! This method doesn't compress its output. 057 * @param object The object to serialize 058 * @param knownType May be null if the type is unknown. 059 * @param elementType May be null if the type is unknown. 060 * @param writer A Writer that will be the recipient of this class' JSON output 061 */ 062 @Override 063 @Deprecated 064 public void toJson(Object object, Class knownType, Class elementType, Writer writer) { 065 super.toJson(object, knownType, elementType, writer); 066 } 067 068 /** 069 * @param type May be null if the type is unknown. 070 * @param reader 071 * @return May be null. 072 */ 073 @Override 074 public <T> T fromJson(Class<T> type, Reader reader) { 075 throw new UnsupportedOperationException("fromJson() given a char[], Reader or InputStream won't decompress;" + 076 "use the overloads that take a String or FileHandle instead"); 077 } 078 079 /** 080 * @param type May be null if the type is unknown. 081 * @param elementType May be null if the type is unknown. 082 * @param reader 083 * @return May be null. 084 */ 085 @Override 086 public <T> T fromJson(Class<T> type, Class elementType, Reader reader) { 087 throw new UnsupportedOperationException("fromJson() given a char[], Reader or InputStream won't decompress;" + 088 "use the overloads that take a String or FileHandle instead"); 089 } 090 091 /** 092 * @param type May be null if the type is unknown. 093 * @param input 094 * @return May be null. 095 */ 096 @Override 097 public <T> T fromJson(Class<T> type, InputStream input) { 098 throw new UnsupportedOperationException("fromJson() given a char[], Reader or InputStream won't decompress;" + 099 "use the overloads that take a String or FileHandle instead"); 100 } 101 102 /** 103 * @param type May be null if the type is unknown. 104 * @param elementType May be null if the type is unknown. 105 * @param input 106 * @return May be null. 107 */ 108 @Override 109 public <T> T fromJson(Class<T> type, Class elementType, InputStream input) { 110 throw new UnsupportedOperationException("fromJson() given a char[], Reader or InputStream won't decompress;" + 111 "use the overloads that take a String or FileHandle instead"); 112 } 113 114 /** 115 * @param type May be null if the type is unknown. 116 * @param file 117 * @return May be null. 118 */ 119 @Override 120 public <T> T fromJson(Class<T> type, FileHandle file) { 121 return super.fromJson(type, file); 122 } 123 124 /** 125 * @param type May be null if the type is unknown. 126 * @param elementType May be null if the type is unknown. 127 * @param file 128 * @return May be null. 129 */ 130 @Override 131 public <T> T fromJson(Class<T> type, Class elementType, FileHandle file) { 132 return super.fromJson(type, elementType, file); 133 } 134 135 /** 136 * @param type May be null if the type is unknown. 137 * @param data 138 * @param offset 139 * @param length 140 * @return May be null. 141 */ 142 @Override 143 public <T> T fromJson(Class<T> type, char[] data, int offset, int length) { 144 throw new UnsupportedOperationException("fromJson() given a char[], Reader or InputStream won't decompress;" + 145 "use the overloads that take a String or FileHandle instead"); 146 } 147 148 /** 149 * @param type May be null if the type is unknown. 150 * @param elementType May be null if the type is unknown. 151 * @param data 152 * @param offset 153 * @param length 154 * @return May be null. 155 */ 156 @Override 157 public <T> T fromJson(Class<T> type, Class elementType, char[] data, int offset, int length) { 158 throw new UnsupportedOperationException("fromJson() given a char[], Reader or InputStream won't decompress;" + 159 "use the overloads that take a String or FileHandle instead"); 160 } 161 162 /** 163 * @param type May be null if the type is unknown. 164 * @param json 165 * @return May be null. 166 */ 167 @Override 168 public <T> T fromJson(Class<T> type, String json) { 169 return super.fromJson(type, json); 170 } 171 172 /** 173 * @param type May be null if the type is unknown. 174 * @param elementType 175 * @param json 176 * @return May be null. 177 */ 178 @Override 179 public <T> T fromJson(Class<T> type, Class elementType, String json) { 180 return super.fromJson(type, elementType, json); 181 } 182}