| 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: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util.json; |
| 7 |
|
| 8 |
import java.math.BigDecimal; |
| 9 |
|
| 10 |
import org.h2.util.ByteStack; |
| 11 |
|
| 12 |
/** |
| 13 |
* JSON validation target without unique keys. |
| 14 |
*/ |
| 15 |
public final class JSONValidationTargetWithoutUniqueKeys extends JSONValidationTarget { |
| 16 |
|
| 17 |
private static final byte OBJECT = 1; |
| 18 |
|
| 19 |
private static final byte ARRAY = 2; |
| 20 |
|
| 21 |
private JSONItemType type; |
| 22 |
|
| 23 |
private final ByteStack stack; |
| 24 |
|
| 25 |
private boolean needSeparator; |
| 26 |
|
| 27 |
private boolean afterName; |
| 28 |
|
| 29 |
/** |
| 30 |
* Creates new instance of JSON validation target without unique keys. |
| 31 |
*/ |
| 32 |
public JSONValidationTargetWithoutUniqueKeys() { |
| 33 |
stack = new ByteStack(); |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public void startObject() { |
| 38 |
beforeValue(); |
| 39 |
afterName = false; |
| 40 |
stack.push(OBJECT); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void endObject() { |
| 45 |
if (afterName || stack.poll(-1) != OBJECT) { |
| 46 |
throw new IllegalStateException(); |
| 47 |
} |
| 48 |
afterValue(JSONItemType.OBJECT); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void startArray() { |
| 53 |
beforeValue(); |
| 54 |
afterName = false; |
| 55 |
stack.push(ARRAY); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void endArray() { |
| 60 |
if (stack.poll(-1) != ARRAY) { |
| 61 |
throw new IllegalStateException(); |
| 62 |
} |
| 63 |
afterValue(JSONItemType.ARRAY); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void member(String name) { |
| 68 |
if (afterName || stack.peek(-1) != OBJECT) { |
| 69 |
throw new IllegalStateException(); |
| 70 |
} |
| 71 |
afterName = true; |
| 72 |
beforeValue(); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void valueNull() { |
| 77 |
beforeValue(); |
| 78 |
afterValue(JSONItemType.SCALAR); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void valueFalse() { |
| 83 |
beforeValue(); |
| 84 |
afterValue(JSONItemType.SCALAR); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void valueTrue() { |
| 89 |
beforeValue(); |
| 90 |
afterValue(JSONItemType.SCALAR); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void valueNumber(BigDecimal number) { |
| 95 |
beforeValue(); |
| 96 |
afterValue(JSONItemType.SCALAR); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void valueString(String string) { |
| 101 |
beforeValue(); |
| 102 |
afterValue(JSONItemType.SCALAR); |
| 103 |
} |
| 104 |
|
| 105 |
private void beforeValue() { |
| 106 |
if (!afterName && stack.peek(-1) == OBJECT) { |
| 107 |
throw new IllegalStateException(); |
| 108 |
} |
| 109 |
if (needSeparator) { |
| 110 |
if (stack.isEmpty()) { |
| 111 |
throw new IllegalStateException(); |
| 112 |
} |
| 113 |
needSeparator = false; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
private void afterValue(JSONItemType type) { |
| 118 |
needSeparator = true; |
| 119 |
afterName = false; |
| 120 |
if (stack.isEmpty()) { |
| 121 |
this.type = type; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean isPropertyExpected() { |
| 127 |
return !afterName && stack.peek(-1) == OBJECT; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public boolean isValueSeparatorExpected() { |
| 132 |
return needSeparator; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public JSONItemType getResult() { |
| 137 |
if (!stack.isEmpty() || type == null) { |
| 138 |
throw new IllegalStateException(); |
| 139 |
} |
| 140 |
return type; |
| 141 |
} |
| 142 |
|
| 143 |
} |