| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: Lazarev Nikita <[email protected]> |
| 5 |
*/ |
| 6 |
package org.h2.value; |
| 7 |
|
| 8 |
import java.io.ByteArrayOutputStream; |
| 9 |
import java.lang.ref.SoftReference; |
| 10 |
import java.math.BigDecimal; |
| 11 |
import java.nio.charset.StandardCharsets; |
| 12 |
import java.util.Arrays; |
| 13 |
|
| 14 |
import org.h2.api.ErrorCode; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.util.StringUtils; |
| 17 |
import org.h2.util.json.JSONBoolean; |
| 18 |
import org.h2.util.json.JSONByteArrayTarget; |
| 19 |
import org.h2.util.json.JSONBytesSource; |
| 20 |
import org.h2.util.json.JSONItemType; |
| 21 |
import org.h2.util.json.JSONNull; |
| 22 |
import org.h2.util.json.JSONNumber; |
| 23 |
import org.h2.util.json.JSONStringSource; |
| 24 |
import org.h2.util.json.JSONStringTarget; |
| 25 |
import org.h2.util.json.JSONValue; |
| 26 |
import org.h2.util.json.JSONValueTarget; |
| 27 |
|
| 28 |
/** |
| 29 |
* Implementation of the JSON data type. |
| 30 |
*/ |
| 31 |
public final class ValueJson extends ValueBytesBase { |
| 32 |
|
| 33 |
private static final byte[] NULL_BYTES = "null".getBytes(StandardCharsets.ISO_8859_1), |
| 34 |
TRUE_BYTES = "true".getBytes(StandardCharsets.ISO_8859_1), |
| 35 |
FALSE_BYTES = "false".getBytes(StandardCharsets.ISO_8859_1); |
| 36 |
|
| 37 |
/** |
| 38 |
* {@code null} JSON value. |
| 39 |
*/ |
| 40 |
public static final ValueJson NULL = new ValueJson(NULL_BYTES); |
| 41 |
|
| 42 |
/** |
| 43 |
* {@code true} JSON value. |
| 44 |
*/ |
| 45 |
public static final ValueJson TRUE = new ValueJson(TRUE_BYTES); |
| 46 |
|
| 47 |
/** |
| 48 |
* {@code false} JSON value. |
| 49 |
*/ |
| 50 |
public static final ValueJson FALSE = new ValueJson(FALSE_BYTES); |
| 51 |
|
| 52 |
/** |
| 53 |
* {@code 0} JSON value. |
| 54 |
*/ |
| 55 |
public static final ValueJson ZERO = new ValueJson(new byte[] { '0' }); |
| 56 |
|
| 57 |
private volatile SoftReference<JSONValue> decompositionRef; |
| 58 |
|
| 59 |
private ValueJson(byte[] value) { |
| 60 |
super(value); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 65 |
String s = JSONBytesSource.parse(value, new JSONStringTarget(true)); |
| 66 |
return builder.append("JSON '").append(s).append('\''); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public TypeInfo getType() { |
| 71 |
return TypeInfo.TYPE_JSON; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public int getValueType() { |
| 76 |
return Value.JSON; |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public String getString() { |
| 81 |
return new String(value, StandardCharsets.UTF_8); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns JSON item type. |
| 86 |
* |
| 87 |
* @return JSON item type |
| 88 |
*/ |
| 89 |
public JSONItemType getItemType() { |
| 90 |
switch (value[0]) { |
| 91 |
case '[': |
| 92 |
return JSONItemType.ARRAY; |
| 93 |
case '{': |
| 94 |
return JSONItemType.OBJECT; |
| 95 |
default: |
| 96 |
return JSONItemType.SCALAR; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns decomposed value. |
| 102 |
* |
| 103 |
* @return decomposed value. |
| 104 |
*/ |
| 105 |
public JSONValue getDecomposition() { |
| 106 |
SoftReference<JSONValue> decompositionRef = this.decompositionRef; |
| 107 |
JSONValue decomposition; |
| 108 |
if (decompositionRef == null || (decomposition = decompositionRef.get()) == null) { |
| 109 |
decomposition = JSONBytesSource.parse(value, new JSONValueTarget()); |
| 110 |
this.decompositionRef = new SoftReference<>(decomposition); |
| 111 |
} |
| 112 |
return decomposition; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns JSON value with the specified content. |
| 117 |
* |
| 118 |
* @param s |
| 119 |
* JSON representation, will be normalized |
| 120 |
* @return JSON value |
| 121 |
* @throws DbException |
| 122 |
* on invalid JSON |
| 123 |
*/ |
| 124 |
public static ValueJson fromJson(String s) { |
| 125 |
byte[] bytes; |
| 126 |
try { |
| 127 |
bytes = JSONStringSource.normalize(s); |
| 128 |
} catch (RuntimeException ex) { |
| 129 |
if (s.length() > 80) { |
| 130 |
s = new StringBuilder(83).append(s, 0, 80).append("...").toString(); |
| 131 |
} |
| 132 |
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, s); |
| 133 |
} |
| 134 |
return getInternal(bytes); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Returns JSON value with the specified content. |
| 139 |
* |
| 140 |
* @param bytes |
| 141 |
* JSON representation, will be normalized |
| 142 |
* @return JSON value |
| 143 |
* @throws DbException |
| 144 |
* on invalid JSON |
| 145 |
*/ |
| 146 |
public static ValueJson fromJson(byte[] bytes) { |
| 147 |
try { |
| 148 |
bytes = JSONBytesSource.normalize(bytes); |
| 149 |
} catch (RuntimeException ex) { |
| 150 |
StringBuilder builder = new StringBuilder().append("X'"); |
| 151 |
if (bytes.length > 40) { |
| 152 |
StringUtils.convertBytesToHex(builder, bytes, 40).append("..."); |
| 153 |
} else { |
| 154 |
StringUtils.convertBytesToHex(builder, bytes); |
| 155 |
} |
| 156 |
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, builder.append('\'').toString()); |
| 157 |
} |
| 158 |
return getInternal(bytes); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Returns JSON value with the specified content. |
| 163 |
* |
| 164 |
* @param value |
| 165 |
* JSON |
| 166 |
* @return JSON value |
| 167 |
* @throws DbException |
| 168 |
* on invalid JSON |
| 169 |
*/ |
| 170 |
public static ValueJson fromJson(JSONValue value) { |
| 171 |
if (value instanceof JSONNull) { |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
if (value instanceof JSONBoolean) { |
| 175 |
return ((JSONBoolean) value).getBoolean() ? TRUE : FALSE; |
| 176 |
} |
| 177 |
if (value instanceof JSONNumber) { |
| 178 |
// Use equals() to check both value and scale |
| 179 |
if (((JSONNumber) value).getBigDecimal().equals(BigDecimal.ZERO)) { |
| 180 |
return ZERO; |
| 181 |
} |
| 182 |
} |
| 183 |
JSONByteArrayTarget target = new JSONByteArrayTarget(); |
| 184 |
value.addTo(target); |
| 185 |
ValueJson v = new ValueJson(target.getResult()); |
| 186 |
v.decompositionRef = new SoftReference<>(value); |
| 187 |
return v; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Returns JSON value with the specified boolean content. |
| 192 |
* |
| 193 |
* @param bool |
| 194 |
* boolean value |
| 195 |
* @return JSON value |
| 196 |
*/ |
| 197 |
public static ValueJson get(boolean bool) { |
| 198 |
return bool ? TRUE : FALSE; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Returns JSON value with the specified numeric content. |
| 203 |
* |
| 204 |
* @param number |
| 205 |
* integer value |
| 206 |
* @return JSON value |
| 207 |
*/ |
| 208 |
public static ValueJson get(int number) { |
| 209 |
return number != 0 ? getNumber(Integer.toString(number)) : ZERO; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Returns JSON value with the specified numeric content. |
| 214 |
* |
| 215 |
* @param number |
| 216 |
* long value |
| 217 |
* @return JSON value |
| 218 |
*/ |
| 219 |
public static ValueJson get(long number) { |
| 220 |
return number != 0L ? getNumber(Long.toString(number)) : ZERO; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns JSON value with the specified numeric content. |
| 225 |
* |
| 226 |
* @param number |
| 227 |
* big decimal value |
| 228 |
* @return JSON value |
| 229 |
*/ |
| 230 |
public static ValueJson get(BigDecimal number) { |
| 231 |
if (number.signum() == 0 && number.scale() == 0) { |
| 232 |
return ZERO; |
| 233 |
} |
| 234 |
String s = number.toString(); |
| 235 |
int index = s.indexOf('E'); |
| 236 |
if (index >= 0 && s.charAt(++index) == '+') { |
| 237 |
int length = s.length(); |
| 238 |
s = new StringBuilder(length - 1).append(s, 0, index).append(s, index + 1, length).toString(); |
| 239 |
} |
| 240 |
return getNumber(s); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Returns JSON value with the specified string content. |
| 245 |
* |
| 246 |
* @param string |
| 247 |
* string value |
| 248 |
* @return JSON value |
| 249 |
*/ |
| 250 |
public static ValueJson get(String string) { |
| 251 |
return new ValueJson(JSONByteArrayTarget.encodeString( // |
| 252 |
new ByteArrayOutputStream(string.length() + 2), string).toByteArray()); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Returns JSON value with the specified content. |
| 257 |
* |
| 258 |
* @param bytes |
| 259 |
* normalized JSON representation |
| 260 |
* @return JSON value |
| 261 |
*/ |
| 262 |
public static ValueJson getInternal(byte[] bytes) { |
| 263 |
int l = bytes.length; |
| 264 |
switch (l) { |
| 265 |
case 1: |
| 266 |
if (bytes[0] == '0') { |
| 267 |
return ZERO; |
| 268 |
} |
| 269 |
break; |
| 270 |
case 4: |
| 271 |
if (Arrays.equals(TRUE_BYTES, bytes)) { |
| 272 |
return TRUE; |
| 273 |
} else if (Arrays.equals(NULL_BYTES, bytes)) { |
| 274 |
return NULL; |
| 275 |
} |
| 276 |
break; |
| 277 |
case 5: |
| 278 |
if (Arrays.equals(FALSE_BYTES, bytes)) { |
| 279 |
return FALSE; |
| 280 |
} |
| 281 |
} |
| 282 |
return new ValueJson(bytes); |
| 283 |
} |
| 284 |
|
| 285 |
private static ValueJson getNumber(String s) { |
| 286 |
return new ValueJson(s.getBytes(StandardCharsets.ISO_8859_1)); |
| 287 |
} |
| 288 |
|
| 289 |
@Override |
| 290 |
public int getMemory() { |
| 291 |
return value.length + 96; |
| 292 |
} |
| 293 |
|
| 294 |
} |