|
| 1 | +package com.fasterxml.jackson.jr.ob.impl; |
| 2 | + |
| 3 | +import static com.fasterxml.jackson.jr.ob.impl.ValueWriterLocator.*; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.URI; |
| 8 | +import java.net.URL; |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.core.JsonParser; |
| 12 | +import com.fasterxml.jackson.core.JsonToken; |
| 13 | +import com.fasterxml.jackson.jr.ob.JSONObjectException; |
| 14 | +import com.fasterxml.jackson.jr.ob.api.ValueReader; |
| 15 | + |
| 16 | +/** |
| 17 | + * Default {@link ValueReader} used for simple scalar types and related, |
| 18 | + * not including POJO-, {@link java.util.Map} and {@link java.util.Collection} |
| 19 | + * types. |
| 20 | + */ |
| 21 | +public class SimpleValueReader extends ValueReader |
| 22 | +{ |
| 23 | + protected final int _typeId; |
| 24 | + |
| 25 | + public SimpleValueReader(Class<?> raw, int typeId) { |
| 26 | + super(raw); |
| 27 | + _typeId = typeId; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public Object readNext(JSONReader reader, JsonParser p) throws IOException |
| 32 | + { |
| 33 | + // NOTE: only cases where we can optimize |
| 34 | + switch (_typeId) { |
| 35 | + // Textual types, related: |
| 36 | + case SER_STRING: |
| 37 | + case SER_CHARACTER_SEQUENCE: |
| 38 | + return _nextString(p); |
| 39 | + |
| 40 | + case SER_CHAR_ARRAY: |
| 41 | + String str = _nextString(p); |
| 42 | + return (str == null) ? null : str.toCharArray(); |
| 43 | + |
| 44 | + // Number types: |
| 45 | + |
| 46 | + case SER_NUMBER_SHORT: // fall through |
| 47 | + return Short.valueOf((short) _nextInt(p)); |
| 48 | + |
| 49 | + case SER_NUMBER_INTEGER: |
| 50 | + return Integer.valueOf(_nextInt(p)); |
| 51 | + |
| 52 | + case SER_NUMBER_LONG: |
| 53 | + return Long.valueOf(_nextLong(p)); |
| 54 | + |
| 55 | + // Other scalar types: |
| 56 | + |
| 57 | + case SER_BOOLEAN: |
| 58 | + { |
| 59 | + Boolean b = p.nextBooleanValue(); |
| 60 | + if (b != null) { |
| 61 | + return b; |
| 62 | + } |
| 63 | + return p.getValueAsBoolean(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + p.nextToken(); |
| 68 | + return read(reader, p); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Object read(JSONReader reader, JsonParser p) throws IOException |
| 73 | + { |
| 74 | + switch (_typeId) { |
| 75 | + |
| 76 | + case SER_MAP: |
| 77 | + case SER_LIST: |
| 78 | + case SER_COLLECTION: |
| 79 | + case SER_OBJECT_ARRAY: |
| 80 | + // should never get here: we have dedicated readers |
| 81 | + break; |
| 82 | + |
| 83 | + case SER_INT_ARRAY: |
| 84 | + return _readIntArray(p); |
| 85 | + |
| 86 | + case SER_TREE_NODE: |
| 87 | + return reader._treeCodec().readTree(p); |
| 88 | + |
| 89 | + // Textual types, related: |
| 90 | + case SER_STRING: |
| 91 | + case SER_CHARACTER_SEQUENCE: |
| 92 | + return p.getValueAsString(); |
| 93 | + case SER_CHAR_ARRAY: |
| 94 | + return p.getValueAsString().toCharArray(); |
| 95 | + case SER_BYTE_ARRAY: |
| 96 | + return _readBinary(p); |
| 97 | + |
| 98 | + // Number types: |
| 99 | + |
| 100 | + case SER_NUMBER_FLOAT: // fall through |
| 101 | + return Float.valueOf((float) p.getValueAsDouble()); |
| 102 | + case SER_NUMBER_DOUBLE: |
| 103 | + return p.getValueAsDouble(); |
| 104 | + |
| 105 | + case SER_NUMBER_BYTE: // fall through |
| 106 | + return (byte) p.getValueAsInt(); |
| 107 | + |
| 108 | + case SER_NUMBER_SHORT: // fall through |
| 109 | + return (short) p.getValueAsInt(); |
| 110 | + case SER_NUMBER_INTEGER: |
| 111 | + return p.getValueAsInt(); |
| 112 | + case SER_NUMBER_LONG: |
| 113 | + return p.getValueAsLong(); |
| 114 | + |
| 115 | + case SER_NUMBER_BIG_DECIMAL: |
| 116 | + return p.getDecimalValue(); |
| 117 | + |
| 118 | + case SER_NUMBER_BIG_INTEGER: |
| 119 | + return p.getBigIntegerValue(); |
| 120 | + |
| 121 | + // Other scalar types: |
| 122 | + |
| 123 | + case SER_BOOLEAN: |
| 124 | + return p.getValueAsBoolean(); |
| 125 | + |
| 126 | + case SER_CHAR: |
| 127 | + { |
| 128 | + String str = p.getValueAsString(); |
| 129 | + return (str == null || str.isEmpty()) ? ' ' : str.charAt(0); |
| 130 | + } |
| 131 | + |
| 132 | + case SER_CALENDAR: |
| 133 | + { |
| 134 | + long l = _fetchLong(p); |
| 135 | + Calendar cal = Calendar.getInstance(); |
| 136 | + cal.setTimeInMillis(l); |
| 137 | + return cal; |
| 138 | + } |
| 139 | + |
| 140 | + case SER_DATE: |
| 141 | + return new Date(_fetchLong(p)); |
| 142 | + |
| 143 | + case SER_CLASS: |
| 144 | + { |
| 145 | + String v = p.getValueAsString(); |
| 146 | + try { |
| 147 | + return Class.forName(v); |
| 148 | + } catch (Exception e) { |
| 149 | + throw new JSONObjectException("Failed to bind java.lang.Class from value '"+v+"'"); |
| 150 | + } |
| 151 | + } |
| 152 | + case SER_FILE: |
| 153 | + return new File(p.getValueAsString()); |
| 154 | + case SER_UUID: |
| 155 | + return UUID.fromString(p.getValueAsString()); |
| 156 | + case SER_URL: |
| 157 | + return new URL(p.getValueAsString()); |
| 158 | + case SER_URI: |
| 159 | + |
| 160 | + return URI.create(p.getValueAsString()); |
| 161 | + |
| 162 | + default: // types that shouldn't get here |
| 163 | + //case SER_ENUM: |
| 164 | + } |
| 165 | + |
| 166 | + throw JSONObjectException.from(p, |
| 167 | + "Can not create a "+_valueType.getName()+" instance out of "+_tokenDesc(p)); |
| 168 | + } |
| 169 | + |
| 170 | + /* |
| 171 | + /********************************************************************** |
| 172 | + /* Read methods for scalars |
| 173 | + /********************************************************************** |
| 174 | + */ |
| 175 | + |
| 176 | + protected byte[] _readBinary(JsonParser p) throws IOException { |
| 177 | + return p.getBinaryValue(); |
| 178 | + } |
| 179 | + |
| 180 | + protected int[] _readIntArray(JsonParser p) throws IOException |
| 181 | + { |
| 182 | + // !!! TODO |
| 183 | + throw new JSONObjectException("Reading of int[] not yet implemented"); |
| 184 | + } |
| 185 | + |
| 186 | + protected long _fetchLong(JsonParser p) throws IOException |
| 187 | + { |
| 188 | + JsonToken t = p.getCurrentToken(); |
| 189 | + if (t == JsonToken.VALUE_NUMBER_INT) { |
| 190 | + return p.getLongValue(); |
| 191 | + } |
| 192 | + throw JSONObjectException.from(p, "Can not get long numeric value from JSON (to construct " |
| 193 | + +_valueType.getName()+") from "+_tokenDesc(p, t)); |
| 194 | + } |
| 195 | + |
| 196 | + private final String _nextString(JsonParser p) throws IOException { |
| 197 | + String str = p.nextTextValue(); |
| 198 | + return (str == null) ? p.getValueAsString() : str; |
| 199 | + } |
| 200 | + |
| 201 | + private final int _nextInt(JsonParser p) throws IOException { |
| 202 | + int i = p.nextIntValue(-2); |
| 203 | + if (i != -2) { |
| 204 | + return i; |
| 205 | + } |
| 206 | + return p.getValueAsInt(); |
| 207 | + } |
| 208 | + |
| 209 | + private final long _nextLong(JsonParser p) throws IOException { |
| 210 | + long l = p.nextLongValue(-2L); |
| 211 | + if (l != -2L) { |
| 212 | + return l; |
| 213 | + } |
| 214 | + return p.getValueAsLong(); |
| 215 | + } |
| 216 | +} |
0 commit comments