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